vinicius73/seotools

包含SEO辅助工具的包。

v1.5.beta 2016-10-02 19:35 UTC

This package is auto-updated.

Last update: 2024-09-13 03:19:08 UTC


README

Total Downloads

警告! 此包仍需要优化测试。

此包是 https://github.com/Calotype/SEO 的分支。

SEOTools 是一个用于 Laravel 4 的包,提供了一些常用SEO技术的辅助工具。

##对于 Laravel 5,请使用 artesaos/seotools

功能

  • 轻松设置标题和元标签
  • 友好的界面
  • 易于设置和定制
  • 支持OpenGraph
  • 支持网站地图和网站地图索引
  • 支持网站地图中的图像

安装

Composer / Packagist

在您的 composer.json 中要求此包。

"vinicius73/seotools": "dev-master"

运行 composer install 或 update 下载包。

$ composer update

服务提供者

只需在 app/config/app.php 中注册服务提供者和外观,您就可以开始使用了。

// Service provider
'Vinicius73\SEO\Providers\SEOServiceProvider',

// Facades (can customize if preferred)
'SEOMeta'     => 'Vinicius73\SEO\Facades\Meta',
'SEOSitemap'  => 'Vinicius73\SEO\Facades\Sitemap',
'OpenGraph'   => 'Vinicius73\SEO\Facades\OpenGraphHelper',

配置

运行您的终端: php artisan config:publish "vinicius73/seotools"
配置文件可在以下位置找到: app/config/packages/vinicius73/seotools

使用

使用 SEOTools 非常简单且友好。
建议使用 barryvdh / laravel-ide-helper,如果您使用 NetBeans 或 PhpStorm 等IDE,这将使开发更加容易。

MetaGenerator e OpenGraph

class CommomController extends BaseController
{

	/**
	 * @return \Illuminate\View\View
	 */
	public function index()
	{
		SEOMeta::setTitle('Home');
        SEOMeta::setDescription('Isto é a minha descrição de página'); // is automatically limited to 160 characters
        OpenGraph::addImage('full-url-to-image-1.png');
        OpenGraph::addImage('full-url-to-image-2.png');
        
		$posts = Post::all();

        return View::make('myindex', compact('posts'));
	}
    
    /**
     * @return \Illuminate\View\View
	 */
    publicc function show($id)
    {
        $post = Post::find($id);
        
        SEOMeta::setTitle($post->title);
        SEOMeta::setDescription($post->resume);
        SEOMeta::addMeta('article:published_time', $post->published_date->toW3CString(), 'property');
        SEOMeta::addMeta('article:section', $post->category, 'property');
        // Vinicius73\SEO\Generators\MetaGenerator::addMeta($meta, $value, $name);
        SEOMeta::setKeywords($post->tags);
        // Vinicius73\SEO\Generators\MetaGenerator::setKeywords(['key1','key2','key3']);
        // Vinicius73\SEO\Generators\MetaGenerator::setKeywords('key1, key2, key3');
        OpenGraph::addImage($post->thumbnail_url);
        
        return View::make('myshow', compact('post'));
    }
}

网站地图生成器

默认情况下,网站地图生成器控制器使用一个旨在简化网站地图创建的 模型
它的使用不是强制性的,可以自由使用任何路由或控制器,您可以在配置文件中禁用它。

创建网站地图控制器

app/config/packages/vinicius73/seotools 中的 classrun 修改为 'classrun' => 'SitemapRun',
您可以将任何自己的类映射上去。

请记住,通过 routes.php 映射您创建的任何附加网站地图。

class SitemapRun
{

    /**
	 * @var \Vinicius73\SEO\Generators\SitemapGenerator
	 */
	public $generator;

	public function __construct($generator)
	{
		$this->generator = $generator;
	}

	/**
	 * Run generator commands
	 */
	public function run()
	{
    	return $this->index();
	}
    
    public function index()
    {
        $this->generator->addRaw(
    		array(
				  'location'         => '/sitemap-posts.xml',
				  'last_modified'    => '2013-12-28',
				  'change_frequency' => 'weekly',
				  'priority'         => '0.95'
			)
		);
        
        return $this->response($this->generator->generate());
    }
    
    public function posts()
    {
        $posts = Post::all();
        
        foreach($posts as $post)
        {
            $images = $post->images;
            
            $element = array(
        			  'location'         => route('route.to.post.show', $post->id),
    				  'last_modified'    => $post->published_date->toW3CString(),
    				  'change_frequency' => 'weekly',
    				  'priority'         => '0.90'
    			);
                
            if ($images):
    			$element['images'] = array();
				foreach ($images as $image):
					$element['images'][] = $image->url();
				endforeach;
			endif;
            
            $this->generator->addRaw($element);
        }
        
        return $this->response($this->generator->generate());
    }
    
    /**
     * @param $sitemap
	 *
	 * @return \Illuminate\Http\Response
	 */
	private function response($sitemap)
	{
		return Response::make($sitemap, 200, array('Content-Type' => 'text/xml'));
	}
}

在您的视图中

<html>
<head>
	{{SEOMeta::generate()}}
	{{OpenGraph::generate()}}
</head>
<body>

</body>
</html>
<html>
<head>
	<title>Title | SubTitle</title>
	<meta name='description' itemprop='description' content='description...' />
	<meta name='keywords' content='key1, key2, key3' />
	<meta property='article:published_time' content='2014-01-31T20:30:11-02:00' />
	<meta property='article:section' content='news' />
	<meta property="og:title" content="title" />
	<meta property="og:description" content="description..." />
	<meta property="og:url" content="curent_or_custom_url" />
	<meta property="og:image" content="full_url_to_image.jpg" />
	<meta property="og:site_name" content="Site name from config" />
</head>
<body>

</body>
</html>