marshmallow / seoable
A Laravel Nova 字段,向资源添加所有SEO相关的元字段。
Requires
- php: ^8.0
- laravel/nova: ^4.0
- marshmallow/commands: ^1.0
- marshmallow/mr-mallow: ^1.0
- marshmallow/nova-advanced-image: 2.0
- marshmallow/nova-charcounted-fields: ^2.0
- marshmallow/nova-fields-help: ^v2.0
- marshmallow/nova-flexible: ^2.0
- marshmallow/nova-multiselect-field: ^5.0
- marshmallow/nova-tinymce: ^2.1
- marshmallow/sluggable: ^1.0
- marshmallow/translatable: ^2.0
- dev-master
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.24.4
- v2.24.3
- v2.24.2
- v2.24.1
- v2.24.0
- v2.23.0
- v2.22.1
- v2.22.0
- v2.21.2
- v2.21.1
- v2.21.0
- v2.20.0
- v2.19.0
- v2.18.4
- v2.18.3
- v2.18.2
- v2.18.1
- v2.18.0
- v2.17.1
- v2.17.0
- v2.16.1
- v2.16.0
- v2.15.1
- v2.15.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.0
- v2.9.11
- v2.9.10
- v2.9.9
- v2.9.8
- v2.9.7
- v2.9.6
- v2.9.5
- v2.9.4
- v2.9.3
- v2.9.2
- v2.9.1
- v2.9.0
- v2.8.2
- v2.8.1
- v2.8.0
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- dev-87-make-all-models-in-seophp-configurable
- dev-stef/mm-9293-seobable-creation-of-dynamic-property-deprecated
- dev-dependabot/github_actions/actions/checkout-3.1.0
- dev-dependabot/github_actions/actions/checkout-3
- dev-tessa
- dev-dependabot/composer/marshmallow/nova-advanced-image-2.0.2
- dev-nova4
- dev-development
This package is auto-updated.
Last update: 2024-09-26 13:00:11 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中看起来如何
如果字段在资源的索引视图中显示,那么你应该看到一个带点的列:
在详细视图中,如果尚未设置SEO,你会看到一个文本说明 你需要一些SEO数据
。但是如果你已经有了,你会得到一个切换按钮,它会显示在Google和Facebook上看起来会是什么样子: