wuifdesign/laravel-seo

此包已被弃用且不再维护。未建议替代包。

Laravel 的 SEO 集成

v0.1.1 2015-11-28 12:19 UTC

This package is not auto-updated.

Last update: 2020-03-06 16:29:40 UTC


README

  • Laravel: 5.1

此包包括用于 SEO 元标签(Meta、OpenGraph、Twitter)和渲染 schema 块的辅助工具。

安装

首先,您需要将此包包含到您的 composer.json 文件中,

"require": {
    "wuifdesign/laravel-seo": "0.1.*"
}

然后通过 composer 更新或安装

composer update

或者您可以直接运行

composer require wuifdesign/laravel-seo

接下来,您打开 app/config/app.php 文件并添加

providers' => [
     WuifDesign\SEO\ServiceProvider::class
]

如果您想使用外观,也应将以下内容添加到文件中

'aliases' => [
    'SEOTool'=> \WuifDesign\SEO\Facades\SEOTool::class,
],

现在您应该运行

php artisan vendor:publish

将配置文件复制到 app/config/seo.php

运行命令后,配置文件将如下所示

return array(

    'enable_logging' => false,
    'used_prefixes'  => array('og'),

    'meta' =>  array(
        'title_styling' => '%title% - %subtitle%',
        'page_title'       => '123',

        'tags' => array(
            'title' => null,
            'description' => null,
            'author'      => array(null, 'rel'),
        ),

        'webmaster_tags' => array(
            'google'    => null,
            'bing'      => null,
            'alexa'     => null,
            'pinterest' => null,
            'yandex'    => null,
        ),
    ),

    'opengraph' =>  array(
        'tags' => array(
            'title'       => '',
            'description' => '',
            'url'         => null,
            'type'        => null,
            'site_name'   => null,
            'images'      => array(),
        ),
    ),

    'twitter' =>  array(
        'tags' => array(
            'card'        => null,
            'title'       => null,
            'description' => null,
            'site'        => null,
            'creator'     => null,
            'url'         => null,
            'images'      => array(),
        ),
    ),

    'schema' =>  array(
        'organization' => array(
            'type'   => 'Organization',
            'tags' => array(
                'name'    => null,
                'address' => array(
                    'type'           => 'PostalAddress',
                    'tags' => array(
                        'streetAddress'   => null,
                        'postalCode'      => null,
                        'addressLocality' => null,
                        'addressCountry' =>  null,
                    ),
                ),
                'geo'  => array(
                    'type'     => 'GeoCoordinates',
                    'hidden'   => true,
                    'tags' => array(
                        'latitude'  => null,
                        'longitude' => null,
                    ),
                ),
                'telephone' => null,
                'faxNumber' => null,
                'email'     => null,
            ),

        ),
    ),

);

现在您可以添加标签的字符串,如果您想为元素设置属性,您可以解析一个数组

'name' => array($value, $attribute)

使用方法

元标签

使用以下方式添加标题/描述

SEOTool::setTitle('Title');
SEOTool::setDescription('Description');
SEOTool::addImage('http://example.com/hello-world.jpg');

如果您想使用 Blade 渲染所有 SEO 标签,只需将以下内容添加到您的 <head> 部分即可。

{!! SEOTool::render() !!}

如果您只想添加单个元类型的数据,也可以使用

SEOTool::metatags()->setTitle('Title');
SEOTool::opengraph()->setTitle('Title');
SEOTool::twitter()->setTitle('Title');

或者,如果您只想添加自定义标签,可以使用

SEOTool::metatags()->addProperty($key, $value[, $name]);
SEOTool::opengraph()->addProperty($key, $value[, $name]);
SEOTool::twitter()->addProperty($key, $value[, $name]);

使用(可用前缀:'og', 'fb', 'music', 'video', 'article', 'book', 'website')来渲染配置中定义的所有前缀

<html prefix="{{ SEOTool::renderPrefixes() }}">

Schema

您可以在配置文件中定义任意数量的 schema。要显示 schema,只需添加以下代码。

{!! SEOTool::renderSchema($key) !!}