siberfx / lara-meta
用于管理 Laravel 10+ 的 Header Meta 标签的包
6.1.1
2024-05-23 21:17 UTC
Requires
- php: ^8.1|^8.2
- illuminate/http: ^10|^11
- illuminate/support: ^10|^11
- laravel/helpers: ^1.6
README
使用此包,您可以从 Laravel 控制器中管理头部 Meta 标签。
安装
在命令行运行以下命令
$ composer require siberfx/lara-meta
Meta Tags 包还提供了一个门面,提供了创建集合的静态语法。如果您的 Laravel 是 10.x 版本,您可以在 config/app.php
文件的 aliases
键中注册此门面。
'aliases' => [ // ... 'MetaTag' => Siberfx\LaraMeta\Facades\MetaTag::class, ]
发布配置
从您项目的根目录在命令行运行此命令
$ php artisan vendor:publish --provider="Siberfx\LaraMeta\MetaTagsServiceProvider"
配置文件将被发布到 config/meta-tags.php
。
Twitter Cards 和 OpenGraph
您可以在 config/meta-tags.php
文件中找到这些选项的各种设置。
Twitter Cards
{!! MetaTag::twitterCard() !!}
OpenGraph
{!! MetaTag::openGraph() !!}
示例
app/Http/Controllers/Controller.php
<?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesCommands; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use MetaTag; abstract class Controller extends BaseController { use DispatchesCommands, ValidatesRequests; public function __construct() { // Defaults MetaTag::set('description', 'description of the page or content you desire to be visible on google searches'); MetaTag::set('image', asset('images/default-share-image.png')); } }
app/Http/Controllers/HomeController.php
<?php namespace App\Http\Controllers; use MetaTag; class HomeController extends Controller { public function index() { // Section description MetaTag::set('title', 'You are at home'); MetaTag::set('description', 'This is my home. Enjoy!'); MetaTag::set('keywords', 'This is my home. Enjoy!'); MetaTag::set('image', asset('images/detail-logo.png')); MetaTag::set('canonical', 'http://example.com'); MetaTag::set('robots', 'index,follow'); return view('index'); } public function detail() { // Section description MetaTag::set('title', 'This is a detail page'); MetaTag::set('description', 'All about this detail page'); MetaTag::set('image', asset('images/detail-logo.png')); return view('detail'); } public function private() { // Section description MetaTag::set('title', 'Private Area'); MetaTag::set('description', 'You shall not pass!'); MetaTag::set('image', asset('images/locked-logo.png')); return view('private'); } }
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"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{ MetaTag::get('title') . ' :: '. config('app.name') }}</title> {!! MetaTag::tag('description') !!} {!! MetaTag::tag('keywords') !!} {!! MetaTag::tag('image') !!} {!! MetaTag::tag('image') !!} {!! MetaTag::tag('canonical') !!} {!! MetaTag::tag('robots') !!} {!! MetaTag::openGraph() !!} {!! MetaTag::twitterCard() !!} {{--Set default share picture after custom section pictures--}} {!! MetaTag::tag('image', asset('images/default-logo.png')) !!} </head> <body> @yield('content') </body> </html>