zanysoft / laravel-meta-tags
一个用于管理头部元标签的软件包
1.0.5
2023-02-16 05:45 UTC
Requires
- php: ^7.1 || ^8.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
README
使用此软件包,您可以从Laravel控制器中管理头部元标签。
安装
从命令行运行
$ composer require zanysoft/laravel-meta-tags
安装元标签后,您需要将服务提供者注册到应用程序中。打开config/app.php并找到providers键。
'providers' => array( ZanySoft\LaravelMetaTags\MetaTagsServiceProvider::class, )
元标签还提供了一个门面,它提供了创建集合的静态语法。您可以在config/app.php文件的aliases键中注册该门面。
'aliases' => array( 'MetaTag' => ZanySoft\LaravelMetaTags\Facades\MetaTag::class, )
发布配置
在项目的根目录下从命令行运行此命令
$ php artisan vendor:publish --provider="ZanySoft\LaravelMetaTags\MetaTagsServiceProvider"
一个配置文件将被发布到config/meta-tags.php。
尝试斜杠配置
如果您想将尝试斜杠添加到主页URL,则将其设置为true(如果配置文件中不存在,请添加此设置)
'add_trialing_slash' => false,
如果您想将查询字符串添加到规范URL,则将其设置为true(如果配置文件中不存在,请添加此设置)
'canonical_query_string' => false,
Twitter Cards和OpenGraph
这些选项的设置可以在config/meta-tags.php文件中找到。
Twitter Cards
{!! MetaTag::twitterCard() !!}
OpenGraph
{!! MetaTag::openGraph() !!}
对于所有
{!! MetaTag::renderAll() !!}
示例
app/Http/Controllers/Controller.php
<?php namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use MetaTag; abstract class Controller extends BaseController { use ValidatesRequests; public function __construct() { // Defaults MetaTag::set('description', 'Blog Wes Anderson bicycle rights, occupy Shoreditch gentrify keffiyeh.'); 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!'); 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') }}</title>
{!! MetaTag::tag('description') !!}
{!! MetaTag::tag('image') !!}
{!! MetaTag::canonical() !!}
{!! 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>