Laravel 和 Lumen 的 SEO 工具

0.12.3 2019-07-02 07:16 UTC

README

SEOTools 是一个为 Laravel 5+Lumen 提供一些常见 SEO 技巧辅助函数的包。

当前构建状态

Build Status Code Climate Codacy Badge

统计数据

Latest Stable Version Total Downloads Latest Unstable Version License

技巧

Powered by ZenHub

功能

  • 友好的界面
  • 轻松设置标题和元标签
  • 轻松设置 Twitter 和 Opengraph 的元标签

安装

1 - 依赖关系

第一步是使用 composer 安装该包并自动更新您的 composer.json 文件,您可以通过运行以下命令来完成此操作:

composer require artesaos/seotools

注意:如果您正在使用 Laravel 5.5,则步骤 2 和 3(提供者和别名)是不必要的。SEOTools 支持 Laravel 新的 包发现

2 - 提供者

您需要更新应用程序配置以注册该包,以便它可以被 Laravel 加载,只需更新您的 config/app.php 文件,在 'providers' 部分的末尾添加以下代码

config/app.php

// file START ommited
    'providers' => [
        // other providers ommited
        Artesaos\SEOTools\Providers\SEOToolsServiceProvider::class,
    ],
// file END ommited

Lumen

转到 /bootstrap/app.php 文件并添加以下行

// file START ommited
	$app->register(Artesaos\SEOTools\Providers\SEOToolsServiceProvider::class);
// file END ommited

3 - Facade

Lumen 不支持 Facade。

为了使用 SEOMeta Facade,您需要在 config/app.php 文件中注册它,您可以通过以下方式完成此操作

// file START ommited
    'aliases' => [
        // other Facades ommited
        'SEOMeta'   => Artesaos\SEOTools\Facades\SEOMeta::class,
        'OpenGraph' => Artesaos\SEOTools\Facades\OpenGraph::class,
        'Twitter'   => Artesaos\SEOTools\Facades\TwitterCard::class,
        // or
        'SEO' => Artesaos\SEOTools\Facades\SEOTools::class,
    ],
// file END ommited

4 配置

发布配置

在您的终端中键入

php artisan vendor:publish

php artisan vendor:publish --provider="Artesaos\SEOTools\Providers\SEOToolsServiceProvider"

Lumen 不支持此命令,对于此操作,您应该将文件 src/resources/config/seotools.php 复制到您的项目的 config/seotools.php

seotools.php 配置文件中,您可以确定默认值的属性和一些行为。

seotools.php

  • meta
  • defaults - 如果没有指定页面显示的任何值,则显示什么值。如果值为 false,则不显示任何内容。
  • webmaster - 是否显示主要网站管理工具的标签值。如果您设置为 null,则不显示任何内容。
  • opengraph
  • defaults - 将始终显示的属性,当未设置其他值时。您可以添加原始配置文件中未包含的额外标签。
  • twitter
  • defaults - 将始终显示的属性,当未设置其他值时。您可以添加原始配置文件中未包含的额外标签。

5 - 使用方法

Lumen 不支持 Facade。

Lumen 使用方法

$seotools = app('seotools');
$metatags = app('seotools.metatags');
$twitter = app('seotools.twitter');
$opengraph = app('seotools.opengraph');

// The behavior is the same as the facade
// --------

echo app('seotools')->generate();

元标签生成器

使用 SEOMeta,您可以创建用于 head 的元标签

Opengraph 标签生成器

使用 OpenGraph,您可以创建用于 head 的 Opengraph 标签

Twitter Cards 标签生成器

使用 Twitter,您可以创建用于 head 的 Opengraph 标签

在您的控制器中

use SEOMeta;
use OpenGraph;
use Twitter;
## or
use SEO;

class CommomController extends Controller
{

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        SEOMeta::setTitle('Home');
        SEOMeta::setDescription('This is my page description');
        SEOMeta::setCanonical('https://codecasts.com.br/lesson');

        OpenGraph::setDescription('This is my page description');
        OpenGraph::setTitle('Home');
        OpenGraph::setUrl('http://current.url.com');
        OpenGraph::addProperty('type', 'articles');

        Twitter::setTitle('Homepage');
        Twitter::setSite('@LuizVinicius73');

        ## Or

        SEO::setTitle('Home');
        SEO::setDescription('This is my page description');
        SEO::opengraph()->setUrl('http://current.url.com');
        SEO::setCanonical('https://codecasts.com.br/lesson');
        SEO::opengraph()->addProperty('type', 'articles');
        SEO::twitter()->setSite('@LuizVinicius73');

        $posts = Post::all();

        return view('myindex', compact('posts'));
    }

    /**
     * @return \Illuminate\View\View
     */
    public 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');
        SEOMeta::addKeyword(['key1', 'key2', 'key3']);

        OpenGraph::setDescription($post->resume);
        OpenGraph::setTitle($post->title);
        OpenGraph::setUrl('http://current.url.com');
        OpenGraph::addProperty('type', 'article');
        OpenGraph::addProperty('locale', 'pt-br');
        OpenGraph::addProperty('locale:alternate', ['pt-pt', 'en-us']);

        OpenGraph::addImage($post->cover->url);
        OpenGraph::addImage($post->images->list('url'));
        OpenGraph::addImage(['url' => 'http://image.url.com/cover.jpg', 'size' => 300]);
        OpenGraph::addImage('http://image.url.com/cover.jpg', ['height' => 300, 'width' => 300]);

        // Namespace URI: http://ogp.me/ns/article#
        // article
        OpenGraph::setTitle('Article')
            ->setDescription('Some Article')
            ->setType('article')
            ->setArticle([
                'published_time' => 'datetime',
                'modified_time' => 'datetime',
                'expiration_time' => 'datetime',
                'author' => 'profile / array',
                'section' => 'string',
                'tag' => 'string / array'
            ]);

        // Namespace URI: http://ogp.me/ns/book#
        // book
        OpenGraph::setTitle('Book')
            ->setDescription('Some Book')
            ->setType('book')
            ->setBook([
                'author' => 'profile / array',
                'isbn' => 'string',
                'release_date' => 'datetime',
                'tag' => 'string / array'
            ]);

        // Namespace URI: http://ogp.me/ns/profile#
        // profile
        OpenGraph::setTitle('Profile')
             ->setDescription('Some Person')
            ->setType('profile')
            ->setProfile([
                'first_name' => 'string',
                'last_name' => 'string',
                'username' => 'string',
                'gender' => 'enum(male, female)'
            ]);

        // Namespace URI: http://ogp.me/ns/music#
        // music.song
        OpenGraph::setType('music.song')
            ->setMusicSong([
                'duration' => 'integer',
                'album' => 'array',
                'album:disc' => 'integer',
                'album:track' => 'integer',
                'musician' => 'array'
            ]);

        // music.album
        OpenGraph::setType('music.album')
            ->setMusicAlbum([
                'song' => 'music.song',
                'song:disc' => 'integer',
                'song:track' => 'integer',
                'musician' => 'profile',
                'release_date' => 'datetime'
            ]);

         //music.playlist
        OpenGraph::setType('music.playlist')
            ->setMusicPlaylist([
                'song' => 'music.song',
                'song:disc' => 'integer',
                'song:track' => 'integer',
                'creator' => 'profile'
            ]);

        // music.radio_station
        OpenGraph::setType('music.radio_station')
            ->setMusicRadioStation([
                'creator' => 'profile'
            ]);

        // Namespace URI: http://ogp.me/ns/video#
        // video.movie
        OpenGraph::setType('video.movie')
            ->setVideoMovie([
                'actor' => 'profile / array',
                'actor:role' => 'string',
                'director' => 'profile /array',
                'writer' => 'profile / array',
                'duration' => 'integer',
                'release_date' => 'datetime',
                'tag' => 'string / array'
            ]);

        // video.episode
        OpenGraph::setType('video.episode')
            ->setVideoEpisode([
                'actor' => 'profile / array',
                'actor:role' => 'string',
                'director' => 'profile /array',
                'writer' => 'profile / array',
                'duration' => 'integer',
                'release_date' => 'datetime',
                'tag' => 'string / array',
                'series' => 'video.tv_show'
            ]);

        // video.tv_show
        OpenGraph::setType('video.tv_show')
            ->setVideoTVShow([
                'actor' => 'profile / array',
                'actor:role' => 'string',
                'director' => 'profile /array',
                'writer' => 'profile / array',
                'duration' => 'integer',
                'release_date' => 'datetime',
                'tag' => 'string / array'
            ]);

        // video.other
        OpenGraph::setType('video.other')
            ->setVideoOther([
                'actor' => 'profile / array',
                'actor:role' => 'string',
                'director' => 'profile /array',
                'writer' => 'profile / array',
                'duration' => 'integer',
                'release_date' => 'datetime',
                'tag' => 'string / array'
            ]);

        // og:video
        OpenGraph::addVideo('http://example.com/movie.swf', [
                'secure_url' => 'https://example.com/movie.swf',
                'type' => 'application/x-shockwave-flash',
                'width' => 400,
                'height' => 300
            ]);

        // og:audio
        OpenGraph::addAudio('http://example.com/sound.mp3', [
                'secure_url' => 'https://secure.example.com/sound.mp3',
                'type' => 'audio/mpeg'
            ]);

        return view('myshow', compact('post'));
    }
}

SEOTrait

use Artesaos\SEOTools\Traits\SEOTools as SEOToolsTrait;

class CommomController extends Controller
{
    use SEOToolsTrait;

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $this->seo()->setTitle('Home');
        $this->seo()->setDescription('This is my page description');
        $this->seo()->opengraph()->setUrl('http://current.url.com');
        $this->seo()->opengraph()->addProperty('type', 'articles');
        $this->seo()->twitter()->setSite('@LuizVinicius73');

        $posts = Post::all();

        return view('myindex', compact('posts'));
    }
}

在您的视图中

技巧:传递参数 true 以获取压缩代码并减小文件大小。

<html>
<head>
	{!! SEOMeta::generate() !!}
	{!! OpenGraph::generate() !!}
	{!! Twitter::generate() !!}
	    <!-- OR -->
	{!! SEO::generate() !!}
	
	  <!-- MINIFIED -->
	{!! SEO::generate(true) !!}
	

	    <!-- LUMEN -->
	{!! app('seotools')->generate() !!}
</head>
<body>

</body>
</html>
<html>
<head>
    <title>Title - Over 9000 Thousand!</title>
    <meta name='description' itemprop='description' content='description...' />
    <meta name='keywords' content='key1, key2, key3' />
    <meta property='article:published_time' content='2015-01-31T20:30:11-02:00' />
    <meta property='article:section' content='news' />

    <meta property="og:description"content="description..." />
    <meta property="og:title"content="Title" />
    <meta property="og:url"content="http://current.url.com" />
    <meta property="og:type"content="article" />
    <meta property="og:locale"content="pt-br" />
    <meta property="og:locale:alternate"content="pt-pt" />
    <meta property="og:locale:alternate"content="en-us" />
    <meta property="og:site_name"content="name" />
    <meta property="og:image"content="http://image.url.com/cover.jpg" />
    <meta property="og:image"content="http://image.url.com/img1.jpg" />
    <meta property="og:image"content="http://image.url.com/img2.jpg" />
    <meta property="og:image"content="http://image.url.com/img3.jpg" />
    <meta property="og:image:url"content="http://image.url.com/cover.jpg" />
    <meta property="og:image:size"content="300" />

    <meta name="twitter:card"content="summary" />
    <meta name="twitter:title"content="Title" />
    <meta name="twitter:site"content="@LuizVinicius73" />

</head>
<body>

</body>
</html>

API(SEOMeta)

SEOMeta::addKeyword($keyword);
SEOMeta::addMeta($meta, $value = null, $name = 'name');
SEOMeta::addAlternateLanguage($lang, $url);
SEOMeta::addAlternateLanguages(array $languages);
SEOMeta::setTitleSeparator($separator);
SEOMeta::setTitle($title);
SEOMeta::setTitleDefault($default);
SEOMeta::setDescription($description);
SEOMeta::setKeywords($keywords);
SEOMeta::setTitleSeparator($separator);
SEOMeta::setCanonical($url);
SEOMeta::setPrev($url);
SEOMeta::setNext($url);
SEOMeta::removeMeta($key);

// You can chain methods
SEOMeta::setTitle($title)
            ->setDescription($description)
            ->setKeywords($keywords)
            ->addKeyword($keyword)
            ->addMeta($meta, $value);

// Retrieving data
SEOMeta::getTitle();
SEOMeta::getTitleSession();
SEOMeta::getTitleSeparator();
SEOMeta::getKeywords();
SEOMeta::getDescription();
SEOMeta::getCanonical($url);
SEOMeta::getPrev($url);
SEOMeta::getNext($url);
SEOMeta::reset();

SEOMeta::generate();

API(OpenGraph)

OpenGraph::addProperty($key, $value); // value can be string or array
OpenGraph::addImage($url); // add image url
OpenGraph::addImages($url); // add an array of url images
OpenGraph::setTitle($title); // define title
OpenGraph::setDescription($description);  // define description
OpenGraph::setUrl($url); // define url
OpenGraph::setSiteName($name); //define site_name

// You can chain methods
OpenGraph::addProperty($key, $value)
            ->addImage($url)
            ->addImages($url)
            ->setTitle($title)
            ->setDescription($description)
            ->setUrl($url)
            ->setSiteName($name);

// Generate html tags
OpenGraph::generate();

API(TwitterCard)

Twitter::addValue($key, $value); // value can be string or array
Twitter::setType($type); // type of twitter card tag
Twitter::setTitle($type); // title of twitter card tag
Twitter::setSite($type); // site of twitter card tag
Twitter::setDescription($type); // description of twitter card tag
Twitter::setUrl($type); // url of twitter card tag
Twitter::addImage($url); // add image url
Twitter::addImages($url); // add an array of url images

// You can chain methods
Twitter::addValue($key, $value)
            ->setType($type)
            ->addImage($url)
            ->addImages($url)
            ->setTitle($title)
            ->setDescription($description)
            ->setUrl($url)
            ->setSite($name);

// Generate html tags
Twitter::generate();

API(SEO)

简化访问所有 SEO 提供者

SEO::metatags();
SEO::twitter();
SEO::opengraph();

SEO::setTitle($title);
SEO::getTitle($session = false);
SEO::setDescription($description);
SEO::setCanonical($url);
SEO::addImages($urls);