34ml / laravel-seo
此包可以帮助您轻松管理网站SEO。
v1.5
2024-05-30 11:27 UTC
Requires
- php: >=8.1
- illuminate/contracts: ^10.0 | ^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8 | ^8.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
此包可以帮助您轻松管理网站SEO。
安装
您可以通过composer安装此包
composer require 34ml/laravel-seo
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="seo-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="seo-config"
这是已发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | SEO status |-------------------------------------------------------------------------- | | Set SEO status, if its set to false then all pages will have | the 'noindex, nofollow' follow type and also removed the meta tags except the title tag. | */ 'seo_status' => env('SEO_STATUS', true), /* |-------------------------------------------------------------------------- | Sitemap status |-------------------------------------------------------------------------- | | Should there be a sitemap available | */ 'sitemap_status' => env('SITEMAP_STATUS', false), /* |-------------------------------------------------------------------------- | SEO title formatter |-------------------------------------------------------------------------- | | If you want a specific default format for your SEO titles, then you can | specify it here. Example could be ':text - Test site', then all pages would have | the ' - Test site' appended to the actual SEO title. | */ 'title_formatter' => ':text', /* |-------------------------------------------------------------------------- | Follow type options |-------------------------------------------------------------------------- | | Here is all the possible follow types shown in the admin panel | which is an array with key -> value. | */ 'follow_type_options' => [ 'index, follow' => 'Index and follow', 'noindex, follow' => 'No index and follow', 'index, nofollow' => 'Index and no follow', 'noindex, nofollow' => 'No index and no follow', ], /* |-------------------------------------------------------------------------- | Default follow type |-------------------------------------------------------------------------- | | Set the default follow type. | */ 'default_follow_type' => env('SEO_DEFAULT_FOLLOW_TYPE', 'index, follow'), /* * SEO default title */ 'default_seo_title' => config('app.name'), /* * SEO default description */ 'default_seo_description' => null, /* * SEO default keywords * ex : keyword1, keyword2, keyword3 */ 'default_seo_keywords' => null, /* * */ /* |-------------------------------------------------------------------------- | Sitemap models |-------------------------------------------------------------------------- | | Insert all the laravel models which should be in the sitemap | */ 'sitemap_models' => [], /* |-------------------------------------------------------------------------- | Sitemap url |-------------------------------------------------------------------------- | | Set the path of the sitemap | */ 'sitemap_path' => '/sitemap', /* |-------------------------------------------------------------------------- | Available Locales |-------------------------------------------------------------------------- | | Set the available locales in your project and fallback locale | */ 'available_locales' => ['en'], 'fallback_locale' => 'en', ];
使用方法
找到您想要添加SEO字段的模型,例如App\Models\Page,然后添加SeoTrait特质
use _34ML\SEO\Traits\SeoTrait; class Page extends Model { use SeoTrait; ... }
当您想要让Eloquent模型显示在网站地图中时,需要向其添加SeoSiteMapTrait特质
use _34ML\SEO\Traits\SeoTrait; use _34ML\SEO\Traits\SeoSiteMapTrait; class Page extends Model { use SeoTrait, 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(); } }
然后进入您的布局blade文件顶部,默认为resources/views/welcome.blade.php
... <head> @include('laravel-seo::seo') ... </head>
变更日志
请参阅CHANGELOG以获取最近更改的更多信息。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。