astroanu / laravel-seo-gen
此包已被废弃,不再维护。未建议替代包。
针对 Laravel 的 SEO 友好型元标签生成器
1.2.0
2016-11-03 20:40 UTC
Requires
- laravel/framework: 5.x.x
This package is auto-updated.
Last update: 2024-05-18 02:58:26 UTC
README
针对 Laravel 的 SEO 友好型元标签生成器
使用方法
将提供者添加到 config\app.php
'Astroanu\SEOGen\SEOGenProvider'
运行 php artisan vendor:publish 以复制配置文件。
'meta' => [
'default_title' => 'My App', // default title for application
'concat_default_title' => true, // wheatehr to concat the default title with the provided title. if this is true and if a title if provided in the controller the result would be "provided title : My App"
'concat_with' => ' : ' // the string to concat the titles
],
'social' => [
'og' => [
'render' => true, // wheather to render facebook og tags or not
'additional_og_tags' => [] // additinal tags to consider when rendering og tags
],
'twitter' => [
'render' => true // wheather to render twitter meta tags or not
]
]
在基础控制器中
abstract class Controller extends BaseController {
protected $metaData;
public function __construct()
{
$this->metaData = new \Astroanu\SEOGen\Data();
}
}
在控制器中设置标签属性并将它们传递给视图
$this->metaData->title = 'Welcome';
$this->metaData->description = 'Some demo text here for you to see';
$this->metaData->robots = 'noindex,nofollow';
$this->metaData->image = 'http://www.sample.com/image.jpg';
return view('welcome', ['metaData' => $this->metaData]);
在视图中渲染所有内容
<?php \Astroanu\SEOGen\Renderer::render($metaData); ?>
示例
// config
return array(
'meta' => [
'default_title' => 'My App',
'concat_default_title' => true,
'concat_with' => ' : '
],
'social' => [
'og' => [
'render' => true,
'additional_og_tags' => ['fb__app_id', 'test_tag']
],
'twitter' => [
'render' => true
]
]
);
// controller
$this->metaData->title = 'Welcome';
$this->metaData->description = 'Some demo text here for you to see';
$this->metaData->robots = 'noindex,nofollow';
$this->metaData->image = 'http://www.sample.com/image.jpg';
$this->metaData->fb__app_id = '456456456456'; // double underscore will be treated as a namespace
$this->metaData->test_tag = 'test text'; // this will still render as a meta tag
return view('welcome', ['metaData' => $this->metaData]);
将产生
<title>Welcome : My App</title>
<meta name="description" content="Some demo text here for you to see" />
<meta name="robots" content="noindex,nofollow" />
<meta name="image" content="http://www.sample.com/image.jpg" />
<meta property="og:title" content="Welcome" />
<meta property="og:description" content="Some demo text here for you to see" />
<meta property="og:image" content="http://www.sample.com/image.jpg" />
<meta property="fb:app_id" content="456456456456" />
<meta property="test_tag" content="test text" />
<meta property="twitter:title" content="Welcome" />
<meta property="twitter:description" content="Some demo text here for you to see" />
<meta property="twitter:image" content="http://www.sample.com/image.jpg" />