fomvasss/laravel-meta-tags
Composer 安装命令:
composer require fomvasss/laravel-meta-tags
包简介
A package to manage SEO (meta-tags, xml-fields, etc.)
README 文档
README
Recommended: use fomvasss/laravel-seo.
Laravel Meta Tags
Support
If this package is useful to you, consider supporting its development:
USDT TRC20 address:
THLgp6DxiAtbNHvgnKV56vk1L38UuUagKf
This package lets you manage meta tags and SEO fields from Laravel controllers and Blade templates.
Installation
Run from the command line:
composer require fomvasss/laravel-meta-tags
Publish and Configure
- Publish assets from the command line:
php artisan vendor:publish --provider="Fomvasss\LaravelMetaTags\ServiceProvider"
- A configuration file will be published to
config/meta-tags.php. - A migration file will be published to
database/migrations/DATE_NOW_create_meta_tags_table.php. - A customizable Blade template file will be published to
resources/views/vendor/meta-tags/tags.blade.php.
- Adjust published assets:
- Set available tags in
config/meta-tags.php(uncomment what you need). - Optionally set your own meta tag model class in
config/meta-tags.php. - Edit the
meta_tagsmigration and uncomment the fields you need.
- Run migration:
php artisan migrate
Upgrading
When upgrading from v2 to v3, please see the UPGRADING.md
Integration & Usage
Usage in Eloquent models: app/Models/Article.php
Add Metatagable trait in your entity model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Fomvasss\LaravelMetaTags\Traits\Metatagable; class Article extends Model { use Metatagable; //... }
Controller example: app/Http/Controllers/ArticleController.php
Using the MetaTag facade in controllers: app/Http/Controllers/ArticleController.php
<?php namespace App\Http\Controllers; use MetaTag; class ArticleController extends Controller { public function index() { $articles = \App\Models\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\Models\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('show', compact('article')); } public function search(Request $request) { $articles = \App\Models\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')); } }
For the package to work correctly, store only the URL path in the path field (without domain), with slashes trimmed (/).
Example:
https://site.com/some/pages/?page=23=>some/pageshttps://site.com/some/pages=>/
Using the MetaTag facade in Blade templates: resources/views/layouts/app.blade.php
Simple and efficient:
<!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>
Or output one by one manually:
<!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>
Another example: 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
And you can set meta tags right in the template:
<!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>
Similarly:
{!! \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() !!}
Links
fomvasss/laravel-meta-tags 适用场景与选型建议
fomvasss/laravel-meta-tags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.79k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2018 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Sitemap」 「seo」 「tags」 「laravel」 「meta」 「meta tags」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fomvasss/laravel-meta-tags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fomvasss/laravel-meta-tags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fomvasss/laravel-meta-tags 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony sitemap generator
A powerfull cacher based on laravels file cache driver
PHP Simple Sitemap Generator
Phalcon PHP Meta tags service
Interfaces for web resource models and services to retrieve and create them
Help to manage meta data on Laravel Eloquent model
统计信息
- 总下载量: 29.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-23