dominicbachmann/seo-meta-nova-field

基于 gwd/seo-meta-nova-field 的分支,已翻译成德语。


README

这是从 https://github.com/AndreasGJ/seo-meta-field-nova 分支出来的。

SEO meta field nova

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

如何安装

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

composer require gwd/seo-meta-nova-field

然后运行迁移

php artisan migrate

然后发布配置

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

如何使用字段

找到您希望添加SEO字段的模型,例如 App\Models\Page,然后添加 SeoMetaTrait 特性

...
use Gwd\SeoMeta\Traits\SeoMetaTrait;

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

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

...
use Gwd\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 Gwd\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值每次都应该具有相同的结构,那么您可以使用特性中的以下方法来设置

// Return the SEO title for the model
public function getSeoTitleDefault(): string

// Return the SEO description for the model
public function getSeoDescriptionDefault(): string

// Return the SEO keywords for the model
public function getSeoKeywordsDefault(): string

// Return the SEO image for the model
public function getSeoImageDefault(): string

// Return the SEO follow type for the model
public function getSeoFollowDefault(): string

设置Sitemap功能

如果您想启用Sitemap功能,则将 seo.sitemap_status 配置更改为 true。然后添加具有 SeoSitemapTrait 特性的模型到 seo.sitemap_models 数组,如下所示

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

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

将Sitemap特性添加到模型中

当您希望Eloquent模型在Sitemap中显示时,需要将 SeoSitemapTrait 特性添加到其中

...
use Gwd\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中的样子

如果字段在资源的索引视图中显示,则您应该看到一个带有点的列:[图片链接](https://github.com/dominicbachmann/seo-meta-field-nova/blob/HEAD//assets/images/seo-field-index.jpg)

在详细视图中,如果您还没有设置SEO,则您将看到一个显示“您需要一些SEO数据”的文本。但是如果您有,则您将得到一个切换按钮,它将显示在Google和Facebook上的样子:[图片链接](https://github.com/dominicbachmann/seo-meta-field-nova/blob/HEAD//assets/images/seo-field-detail-hidden.jpg) [图片链接](https://github.com/dominicbachmann/seo-meta-field-nova/blob/HEAD//assets/images/seo-field-detail-show.jpg)

在表单视图中,您应该看到所有SEO输入字段:[图片链接](https://github.com/dominicbachmann/seo-meta-field-nova/blob/HEAD//assets/images/seo-field-form.jpg)