iki/seo-meta

Laravel Nova 字段。

1.1.0 2023-07-15 16:36 UTC

This package is auto-updated.

Last update: 2024-09-15 19:09:46 UTC


README

这是一个从原始包的分支,但具有更多功能和更新到 Nova4。

这个自定义 nova 字段,可以通过一个单一特质中的形态关系将 SEO 相关字段添加到任何模型。

如果你是从 v1 升级,请阅读升级指南

如何安装

要安装此包,请运行以下安装命令

composer require ikiEN/nova-seo-field

然后发布配置和迁移

php artisan vendor:publish --provider="Iki\SeoMeta\FieldServiceProvider"

然后运行迁移

php artisan migrate

如何使用字段

找到你想要添加 SEO 字段的对象模型,例如 App\Models\Page,然后添加 SeoMetaTrait 特质

...
use Iki\SeoMeta\Traits\SeoMetaTrait;

class Page extends Model
{
    use SeoMetaTrait;
    ...
}

然后在 nova 资源 App\Nova\Page 中使用该字段

...
use Iki\SeoMeta\SeoMeta;

class Page extends Resource
{
  ...
  public function fields(Request $request)
  {
    return [
      ...,
      SeoMeta::make('SEO', 'seo_meta')
        ->disk('s3-public') //disk to store seo image, default is public
    ];
  }
}

然后进入布局 blade 文件的顶部,默认为 resources/views/welcome.blade.php

...
<head>
    @include('seo-meta::seo')
    ...
</head>

其中 @include('seo-meta::seo', ['page' => $page]),应该有与 SeoMetaTrait 特质相关的模型实例。

如果你在当前页面上没有选择任何模型/资源,则可以像这样获取页面的 SEO 数据

use Iki\SeoMeta\Helper\Seo;
...
Route::get('/tester', function(){
    return view('page', [
        'seo' => Seo::renderAttributes('SEO title', 'SEO description', 'SEO keywords', 'SEO image', 'index, follow'), // Builds the seo array
    ]);
});

以下是 Seo::renderAttributes 静态方法的样子

本地化

  • 要本地化,只需更新配置文件,包括可用的区域设置和回退区域设置

设置模型的默认值

如果 SEO 值应该每次都有相同的结构,那么你可以使用特质中的以下方法设置它

    /**
     * REGISTERING THE DEFAULT VALUES IF EXISTS
     */
    public function registerDefaultValues(): void
    {
        
        // add default SEO title for the model 
        $this->addTitleDefault(string $value = null, string $locale = null): void
        
        // add default SEO description for the model 
        $this->addDescriptionDefault(string $value = null, string $locale = null): void
        
        // add default SEO keywords for the model 
        $this->addKeywordsDefault(string $value = null, string $locale = null): void
        
        // add default SEO image for the model 
        $this->addImageDefault(string $value = null): void
        
         // add default SEO follow for the model 
        $this->addFollowDefault(string $value): void
    }

设置 Sitemap 功能

如果你想要 Sitemap 功能,则通过将 seo.sitemap_status 配置更改为 true 激活 Sitemap。然后将具有 SeoSitemapTrait 特质的模型添加到 seo.sitemap_models 数组中,如下所示

    ...
    'sitemap_status' => env('SITEMAP_STATUS', true),

    ...
    'sitemap_models' => [
        App\Models\Page::class
    ],

将 Sitemap 特质添加到模型

当你想要 eloquent 模型在 Sitemap 中显示时,需要将 SeoSitemapTrait 特质添加到它

...
use Iki\SeoMeta\Traits\SeoSitemapTrait;

class Page extends Model
{
    use SeoMetaTrait, SeoSitemapTrait;
    ...

    /**
     * Get the Page url by item
     *
     * @return string
     */
    public function getSitemapItemUrl()
    {
        return url($this->slug);
    }

    /**
     * Query all the Page items which should be
     * part of the sitemap (crawlable for google).
     *
     * @return Builder
     */
    public static function getSitemapItems()
    {
        return static::all();
    }
}

现在你应该能够访问 seo.sitemap_path,默认为 /sitemap。然后你应该会得到一个正确的 Sitemap 结构的 xml,用于Google Search Console