orzcc/autometa

Laravel 的元数据工具

dev-master 2019-11-29 10:26 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:02 UTC


README

AutoMeta 是一个用于 Laravel 5 的包,提供了一些常用 SEO 技术的辅助函数。

修复了一些错误,并简化了 artesaos/seotools 的代码。

安装

1 - 依赖关系

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

composer require orzcc/autometa

2 - 提供者

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

config/app.php

// file START ommited
    'providers' => [
        // other providers ommited
        'Orzcc\AutoMeta\Providers\AutoMetaServiceProvider',
    ],
// file END ommited

3 - 外观

为了使用 Meta 外观,你需要将其注册到 config/app.php 文件中,你可以按照以下方式操作:

// file START ommited
    'aliases' => [
        // other Facades ommited
        'Meta'   => 'Orzcc\AutoMeta\Facades\AutoMeta',
    ],
// file END ommited

4 - 使用方法

元标签生成器

使用 Meta,你可以为 head 创建元标签

在你的控制器中

use Meta;

class CommomController extends Controller
{

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        Meta::setTitle('Home');
        Meta::setDescription('This is my page description');
        
        $posts = Post::all();

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

    /**
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $post = Post::find($id);

        Meta::setTitle($post->title);
        Meta::setDescription($post->resume);
        Meta::addMeta('article:published_time', $post->published_date->toW3CString(), 'property');
        Meta::addMeta('article:section', $post->category, 'property');
        Meta::addKeyword(['key1', 'key2', 'key3']);

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

在你的视图中

<html>
<head>
	{!! Meta::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='2015-01-31T20:30:11-02:00' />
	<meta property='article:section' content='news' />

</head>
<body>

</body>
</html>

配置

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

  • 默认值 - 如果未指定页面的值,则显示什么值。如果值为 false,则不显示任何内容。
  • 网站管理员 - 是否为主要的网站管理员工具设置标签值的设置。如果你是 null,则不显示任何内容。

API(元数据)

Meta::setTitleSeperator($seperator);
Meta::generate::setTitle($title);
Meta::setDescription($description);
Meta::setKeywords($keywords);
Meta::addKeyword($keyword);
Meta::addMeta($meta, $value = null, $name = 'name');

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

// Retrieving data
Meta::getTitle();
Meta::getTitleSession();
Meta::getTitleSeperator();
Meta::getKeywords();
Meta::getDescription();
Meta::reset();

Meta::generate();

赞助商

技术生活地带