fomvasss / laravel-meta-tags
用于管理SEO(元标签、XML字段等)的包
3.5.2
2022-10-13 08:56 UTC
Requires
- php: >=7.0.0
- illuminate/database: ^5.8|^6.0|^7.0|^8.0|^9.0
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0
- illuminate/view: ^5.8|^6.0|^7.0|^8.0|^9.0
README
此包的开发和维护已停止!请使用更好的解决方案 fomvasss/laravel-seo
Laravel Meta Tags
使用此包,您可以从Laravel控制器和“blade”模板管理元标签和SEO字段。
安装
从命令行运行
composer require fomvasss/laravel-meta-tags
发布和设置
- 发布资源 - 在命令行中运行此命令
php artisan vendor:publish --provider="Fomvasss\LaravelMetaTags\ServiceProvider"
- 配置文件将被发布到
config/meta-tags.php
。 - 迁移文件将被发布到
database/migrations/DATE_NOW_create_meta_tags_table.php
。 - 可自定义的blade模板文件将被发布到
resources/views/vondor/meta-tags/tags.blade.php
。
- 编辑资源
- 在
config/meta-tags.php
中设置可用的标签 - 取消注释所需的 - 如果需要,在
config/meta-tags.php
中设置自己的元标签模型类 - 编辑迁移文件
meta_tags
- 设置可用的字段标签 - 取消注释所需的
- 运行迁移
php artisan migrate
升级
从v2升级到v3时,请参阅UPGRADING.md
集成与使用
Eloquent模型中的使用: app/Models/Article.php
在您的实体模型中添加Metatagable
特质
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Fomvasss\LaravelMetaTags\Traits\Metatagable; class Article extends Model { use Metatagable; //... }
app/Http/Controllers/ArticleController.php:
在控制器中使用外观MetaTag
: app/Http/Controllers/ArticleController.php
<?php namespace App\Http\Controllers; use MetaTag; class ArticleController extends Controller { public function index() { $articles = \App\Model\Article::paginate(); MetaTag::setTags([ 'title' => 'Article index page', 'description' => 'It is article index page', ]); return view('index', compact('articles')); } public function store(Request $request) { // create entity $article = \App\Model\Article::create($request->only([ //.. article data ])); // create meta tag for entity $article->metaTag()->create($request->only([ //.. meta tags fields ])); } public function show($id) { $article = \App\Model\Article::findOrFail($id); // Set tags for showing MetaTag::setEntity($article) ->setDefault([ 'title' => $article->title, // if empty $article->metaTag->title - show this title ])->setTags([ 'seo_text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', 'h1' => $article->title, ]); return view('stow', compact('article')); } public function search(Request $request) { $articles = \App\Model\Article::bySearch($request->q) ->paginate(); // Set tags for showing MetaTag::setPath() // if argument `setPath()` is empty (or not set) - path = `request()->path()` ->setDefault([ 'title' => 'Search page', 'robots' => 'noindex', 'og_title' => 'Search page OG', 'twitter_title' => 'Search page Twitter', 'canonical' => 'page/search', ]); return view('index', compact('articles')); } }
为了使包正确工作,您必须在数据库中保存,在path
字段中,仅保存url路径本身,不包含域名,并去除斜杠(/
)
示例
https://site.com/some/pages/?page=23
=>some/pages
https://site.com/some/pages
=>/
在blade模板中使用外观MetaTag
: resources/views/layouts/app.blade.php
简单高效
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8"> {!! MetaTag::render() !!} </head> <body> @yield('content') </body> </html>
或者手动逐个输出
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>{!! MetaTag::tag('title') !!}</title> <meta name="description" content="{!! MetaTag::tag('description') !!}"> <meta name="keywords" content="{!! MetaTag::tag('keywords') !!}"> </head> <body> @yield('content') </body> </html>
另一个示例: resources/views/articles/show.blade.php
@extends('layouts.app') @section('content') <h1>{!! MetaTag::tag('title') !!}</h1> <div>{!! $article->body !!}</div> <div>{{ MetaTag::tag('seo_text') }}</div> @endsection
您可以直接在模板中设置元标签
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8"> @php(MetaTag::setEntity($article)) @php(MetaTag::setDefault(['description' => 'My default meta tag'])) {!! MetaTag::render() !!} </head> <body> @yield('content') </body> </html>
类似地
{!! \MetaTag::setEntity($article) ->setDefault(['description' => 'My default meta tag']) ->render() !!}
{!! \MetaTag::setPath('articles') ->setDefault(['robots' => 'follow', 'canonical' => 'page/articles']) ->setDefault(['title' => 'All articles']) ->setDefault(['og_title' => 'All articles']) ->setDefault(['og_locale' => 'de']) ->setDefault(['og_image' => 'files/images/5be3d92e02a55890e4301ed4.jpg', 'og_image_height' => 123]) ->render() !!}