mad-web/laravel-seoable
轻松地将eloquent字段映射到SEO属性
该软件包的官方仓库似乎已丢失,因此已冻结该软件包。
Requires
- php: ^7.2.5
- ext-json: *
- artesaos/seotools: ^0.19.0
- illuminate/database: ^6.0 || ^7.0 || ^8.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- mockery/mockery: ^1.3
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0
- phpunit/phpunit: ^8.0 || ^9.3
README
此软件包提供以下功能:
- 将Eloquent属性映射到SEO元标签
- 在lang文件中设置
和 的模板 - 为应用中的任何模型保存自定义SEO数据
与以下内容一起工作:
- 元标签
- Open Graph
- Twitter Card
基于artesaos/seotools的软件包,可在模板中设置元标签。
Postcardware
您可以使用此软件包(它MIT授权),但如果它进入了您的生产环境,我们非常感谢您从家乡寄给我们一张明信片,说明您正在使用我们的哪个软件包。
安装
您可以通过composer安装此软件包
对于Laravel <= 5.4
composer require mad-web/laravel-seoable:1.0.0
对于Laravel 5.5
composer require mad-web/laravel-seoable
对于Laravel <= 5.4 - 现在在config/app.php文件中添加服务提供者
'providers' => [ // ... MadWeb\Seoable\SeoableServiceProvider::class, ];
您可以使用以下命令发布迁移:
$ php artisan vendor:publish --provider="MadWeb\Seoable\SeoableServiceProvider" --tag="migrations"
您可以使用以下命令发布配置文件:
$ php artisan vendor:publish --provider="MadWeb\Seoable\SeoableServiceProvider" --tag="config"
这是发布的config/laravel-seoable.php配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Seo Data Table |-------------------------------------------------------------------------- | | You can customize seo data storing table for your models */ 'seo_data_table' => 'seo_data', /* |-------------------------------------------------------------------------- | Seo Data Templates Path |-------------------------------------------------------------------------- | | Path to lang file where you can set property template | | Supported properties: "title", "description" */ 'templates_path' => 'seoable::seo', /* |-------------------------------------------------------------------------- | Seo Data Model |-------------------------------------------------------------------------- | | Model name for seo data table */ 'model' => \MadWeb\Seoable\Models\SeoData::class ];
要设置
$ php artisan vendor:publish --provider="MadWeb\Seoable\SeoableServiceProvider" --tag="lang"
或者在templates_path
配置属性中设置自己的模板
/* |-------------------------------------------------------------------------- | Seo Data Templates Path |-------------------------------------------------------------------------- | | Path to lang file where you can set property template | | Supported properties: "title", "description" */ 'templates_path' => 'seoable::seo',
使用
下一步,您需要通过实现接口、使用特性和实现seoable()
方法来准备您的模型,如下所示
class User implements Seoable { use SeoableTrait; ... public function seoable() { } }
标签设置
使用seo()
方法,并通过fluent API设置字段
public function seoable() { $this->seo() ->setTitle('full_name') ->setDescription('full_name'); }
然后设置模板,如下例所示
return [ \App\User::class => [ 'title' => 'This is page title for user profile :full_name', 'description' => 'This is page description for user profile :full_name', 'twitter_card' => [ 'title' => 'Page title for twitter card :full_name', 'description' => 'Page description for twitter card :full_name' ], 'open_graph' => [ 'title' => 'Page title for open graph :full_name', 'description' => 'Page description for open graph :full_name' ] ] ];
如果您没有声明它,将默认使用字段值
您还可以通过在方法后添加Raw后缀来设置原始属性
public function seoable() { $this->seo() ->setTitleRaw('Some awesome title') ->setDescriptionRaw('Some awesome description'); }
您可以通过放置关联数组来传递多个属性并设置自定义名称
public function seoable() { $this->seo() ->setTitle(['name' => 'full_name', 'address' => 'email']) ->setDescription('full_name'); }
您可以使用seoData()
关系将附加到模型的SEO元标签保存
$user = User::find(1) $user->seoData->update(['meta' => ['title' => 'some title']]);
存储的标签优先于在seoable()
方法中设置的标签 *
填充标签
在您的控制器中,您可以像这样调用seoable()
方法
public function show($post) { $post->seoable() ... }
如果您想覆盖一些元标签
public function show($post) { $post->seoable()->meta() ->setTitleRaw('Some Post Title'); ... }
如果您需要忽略模型在数据库中存储的标签
public function show($post) { $post->seoable()->meta()->ignoreStored() ->setTitleRaw('Some Post Title'); ... }
标签生成
将以下行放入<head>
标签内
<head> ... {!! resolve('seotools')->generate() !!} ... </head>
或者您可以将Facade添加到app.php
配置中
'aliases' => [ // other Facades ommited 'SEO' => Artesaos\SEOTools\Facades\SEOTools::class, ]
并使用它而不是resolve('seotools')
<head> ... {!! SEO::generate() !!} ... </head>
要设置默认元标签值,只需发布SEOTools配置
php artisan vendor:publish --provider="Artesaos\SEOTools\Providers\SEOToolsServiceProvider"
您可以在SEOTools Readme上找到完整的用法文档
完整的fluent API
public function seoable() { return $this->seo() ->setTitle(['name', 'email']) ->setDescription('name') ->setCanonical('url') ->setPrev('link') ->setNext('link') ->setKeywords('keywords') ->setLanguages([ [ 'lang' => 'ru', 'url' => 'lang_url' // Resolving by model attribute ] ]) ->addLanguage('en', 'lang_url') ->addMeta('foo', 'bar') ->setMeta([ [ 'meta' => 'some', 'value' => 'name' ], [ 'meta' => 'another', 'value' => 'tag' ] ]) ->twitter() ->setTitle('name') ->setDescription('name') ->setUrl('url') ->setSite('site_name') ->setType('type') ->setImages(['avatar', 'image']) ->addValue('foo', ['name', 'name']) ->setValues([ [ 'key' => 'foo', 'value' => 'attribute' ], [ 'key' => 'another', 'value' => 'another_attribute' ] ]) ->opengraph() ->setTitle('name') ->setDescription(['name', 'email']) ->setUrl('url') ->setSiteName('site_name') ->setImages(['avatar', 'image']) ->setProperties([ [ 'key' => 'foo', 'value' => 'attribute' ], [ 'key' => 'another', 'value' => 'another_attribute' ] ]) ->addProperty('foo', ['name', 'email']); }
更新日志
请参阅CHANGELOG以获取有关最近更改的更多信息
测试
$ composer test
贡献
请参阅CONTRIBUTING和CONDUCT以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件madweb.dev@gmail.com而不是使用问题跟踪器。
致谢
许可证
MIT许可证。请参阅许可证文件获取更多信息。