marshmallow/seoable

A Laravel Nova 字段,向资源添加所有SEO相关的元字段。

v3.1.4 2024-03-19 12:39 UTC

README

这个自定义的nova字段,可以通过一个特性行为,在单个模型中添加SEO相关的字段。

如何安装

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

composer require marshmallow/seoable

然后运行迁移

php artisan migrate

然后发布配置

php artisan vendor:publish --provider="Marshmallow\Seoable\ServiceProvider"

请注意

如果您正在使用路由缓存,请确保您有正在运行的 queue:work。如果您更改了路由,我们将自动重新缓存您的路由,但这是通过队列完成的。

手动

您可以使用以下方法更改SEO数据。

use Marshmallow\Seoable\Facades\Seo;

Seo::setTitle(string $title);
Seo::setDescription(string $description);
Seo::setKeywords(array $keywords);
Seo::setImage(string $image);
Seo::setFollowType(string $follow_type);

如何使用字段

找到您想要添加SEO字段的模型,例如 App\Models\Page,然后添加 Seoable 特性

...
use Marshmallow\Seoable\Traits\Seoable;

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

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

...
use Marshmallow\Seoable\Seoable;

class Page extends Resource
{
  ...
  public function fields(Request $request)
  {
    return [
      ...,
      Seoable::make('Seo'),
    ];
  }
}

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

...
<head>
    {{ Seo::generate() }}
    ...
</head>

最后一步!告诉SEO外观它可以使用哪个模型来设置SEO数据。

use Marshmallow\Seoable\Facades\Seo;

class ExampleController extends Controller
{
    public function show (Product $product)
    {
        $product->useForSeo();

        return view('product')->with([
            'product' => $product
        ])
    }
}

发布Nova资源

php artisan marshmallow:resource Route Seoable
php artisan marshmallow:resource PrettyUrl Seoable

使用sluggable

此软件包默认包含 marshmallow/sluggable。我们这样做是为了确保所有SEO驱动的网站都会使用相同的逻辑来构建短标题。此软件包本身不使用 marshmallow/sluggable,因此如果您愿意,可以选择其他任何可伸缩的软件包。

class YourEloquentModel extends Model
{
    use HasSlug;

    /**
     * Get the options for generating the slug.
     */
    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }
}

使用路由

php artisan marshmallow:resource Route Seoable

将以下内容添加到您的 routes/web.php

use Marshmallow\Seoable\Seoable;

Seoable::routes();

使用漂亮的URL

如果您希望使用漂亮的URL模块,您需要在配置中激活它。默认设置为 false。此模块将为您注册一个中间件。因为此功能目前处于测试阶段,所以默认禁用。如果您发现任何问题,请告诉我们。

// config/seo.php

return [
    'use_pretty_urls' => true,
];

为模型设置默认值

您可以使用以下方法覆盖每个模型SEO默认值的处理方式。

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

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

// Return the SEO keywords for the model
public function setSeoKeywords(): ?array

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

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

设置Sitemap功能

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

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

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

向模型添加Sitemap特性

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

...
use Marshmallow\Seoable\Traits\SeoSitemapTrait;

class Page extends Model
{
    use SeoableTrait, 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

结构化数据

$faq = \Marshmallow\Seoable\Helpers\Schemas\SchemaFaqPage::make();
$faq->addQuestionAndAnswer('What is the name of this company?', 'Marshmallow');

\Marshmallow\Seoable\Facades\Seo::addSchema($faq);

robots.txt

此软件包还允许您使用自定义的robots.txt。您应该在助手类中添加您的robots.txt内容。例如,在 app/Helpers/RobotTxt.php 中创建一个类。在这个类中,您应该实现一个 handle 方法。此软件包将为您调用此方法并输出结果。

# app/Helpers/RobotTxt.php

namespace App\Helpers;

use Marshmallow\Seoable\Objects\Robots;
use Marshmallow\Seoable\Contracts\RobotTxtInterface;

class RobotTxt implements RobotTxtInterface
{
    public function handle(Robots $robots): Robots
    {
        return $robots->userAgent('*')
            ->allow('/')
            ->disallow('/login');
    }
}

实现robots

创建助手类后,您应该让seoable配置知道如何找到此助手。

return [

    /*
    |--------------------------------------------------------------------------
    | Robots.txt
    |--------------------------------------------------------------------------
    |
    | Override the class which builds the robots.txt file. If you are using this,
    | do not forget to delete the original public/robots.txt file.
    |
    */
    'robots_resolver' => App\Helpers\DefaultRobotsTxt::class,
];

要完成实现,您只需删除默认的 public/robots.txt 文件即可!

在Laravel Nova中看起来如何

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

在详细视图中,如果尚未设置SEO,你会看到一个文本说明 你需要一些SEO数据。但是如果你已经有了,你会得到一个切换按钮,它会显示在Google和Facebook上看起来会是什么样子:alt text alt text

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