spescina/seorules

用于在 Laravel 项目中构建 SEO 规则的工具

2.0.3 2017-01-10 15:34 UTC

README

一个用于在 Laravel 项目中构建 SEO 规则的包。轻松管理页面 SEO 元标签(标题、描述、关键词、noindex)。

安装

要在 Laravel 5.1 中安装 Seorules,只需运行 composer require spescina\seorules
要在 Laravel 4 中安装 Seorules,只需运行 composer require spescina\seorules 1.* 并遵循 这些说明

安装后,您需要注册服务提供者。在 app/config/app.php 中,将以下代码行添加到 providers 数组中
Spescina\Seorules\SeorulesServiceProvider::class.

如果要在 app/config/app.php 中添加,将以下代码行添加到 aliases 数组中
'Seo' => Spescina\Seorules\Facades\Seo::class.

注册路由中间件,将以下行添加到 app/Http/Kernel.php 文件中
'seorules.before' => \Spescina\Seorules\Init::class.

然后,使用 php artisan vendor:publish --provider="Spescina\Seorules\SeorulesServiceProvider" --tag="config" 发布配置文件。
然后,使用 php artisan vendor:publish --provider="Spescina\Seorules\SeorulesServiceProvider" --tag="migrations" 发布迁移文件。

然后运行迁移,使用 php artisan migrate

使用方法

通过在 seorules 数据库表中创建条目来定义您的规则

  • 别名:规则的系统标识名称
  • 路由:目标路由的名称
  • 模式:匹配页面 URL 的正则表达式(用于针对同一路由上的不同页面进行定位)
  • 优先级:值最高的先执行
  • 标题:标题标签
  • 描述:描述元标签
  • 关键词:关键词元标签
  • noindex:noindex 元标签
{
  alias: 'first',
  route: 'first.route',
  pattern: '',
  priority: 0,
  title: 'the first route title is [#_first_placeholder]',
  description: 'my first route description is [#_second_placeholder]',
  keywords: '[#_first_placeholder], [#_second_placeholder], laravel',
  noindex: 0
},
{
  alias: 'second',
  route: 'second.route',
  pattern: '',
  priority: 0,
  title: 'the second route title is [#_second_placeholder]',
  description: 'my second route description is empty',
  keywords: '[#_first_placeholder]',
  noindex: 1
}

seorules.before 中间件附加到您的目标命名路由(建议使用路由组)

Route::group(['middleware' => 'seorules.before'], function()
{
    Route::get('/first', array('as' => 'first.route', function(){
        //do things
    }));

    Route::get('/second', array('as' => 'second.route', function(){
        //do things
    }));
});

在控制器或闭包中管理您的规则

Seo::addPlaceholder('first_placeholder','Foo');
Seo::addPlaceholder('second_placeholder','Bar');

在视图中显示准备好的字段

<title>{{ Seo::get('title') }}</title>
<meta name="description" content="{{ Seo::get('description') }}" />
<meta name="keywords" content="{{ Seo::get('keywords') }}" />
@if (Seo::get('noindex'))
<meta name="robots" content="noindex" />
@endif

现在,当访问 /first 时(假设两个路由都准备好了相同的数据占位符)

<title>the first route title is Foo</title>
<meta name="description" content="my first route description is Bar" />
<meta name="keywords" content="Foo, Bar, laravel" />

并且当访问 /second

<title>the second route title is Bar</title>
<meta name="description" content="my second route description is empty" />
<meta name="keywords" content="Foo" />
<meta name="robots" content="noindex" />