willis1776/seo-nova-field

一个Laravel Nova字段,它将所有SEO相关元字段添加到资源中。


README

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

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

如何安装

要安装该软件包,请运行以下安装命令

composer require willis1776/seo-meta-nova-field

然后发布配置和迁移

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

然后运行迁移

php artisan migrate

如何使用字段

找到你想要添加SEO字段的模型,例如可以是App\Models\Page,然后添加SeoMetaTrait特性

...
use Willis1776\SeoMeta\Traits\SeoMetaTrait;

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

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

...
use Willis1776\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 Willis1776\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特性添加到模型中

当您想要在Sitemap中显示eloquent模型时,您需要向它添加SeoSitemapTrait特性

...
use Willis1776\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

在Laravel Nova中看起来如何

如果字段在资源的索引视图中显示,您应该看到一个带有点的列:alt text

在详细视图中,如果没有设置SEO,您将看到一条文本信息“您需要一些SEO数据”。但是如果您有,您将获得一个切换按钮,它会显示在Google和Facebook上的外观示例:alt text alt text

在表单视图中,您应该看到所有SEO输入字段:alt text