fomvasss/laravel-meta-tags

用于管理SEO(元标签、XML字段等)的包

安装次数: 21,032

依赖: 0

建议者: 0

安全性: 0

星标: 28

关注者: 3

分支: 7

公开问题: 1

类型:composer-package

3.5.2 2022-10-13 08:56 UTC

This package is auto-updated.

Last update: 2024-09-13 12:53:14 UTC


README

此包的开发和维护已停止!请使用更好的解决方案 fomvasss/laravel-seo

Laravel Meta Tags

License Build Status Latest Stable Version Total Downloads Quality Score

使用此包,您可以从Laravel控制器和“blade”模板管理元标签和SEO字段。

安装

从命令行运行

composer require fomvasss/laravel-meta-tags

发布和设置

  1. 发布资源 - 在命令行中运行此命令
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
  1. 编辑资源
  • config/meta-tags.php中设置可用的标签 - 取消注释所需的
  • 如果需要,在config/meta-tags.php中设置自己的元标签模型类
  • 编辑迁移文件meta_tags - 设置可用的字段标签 - 取消注释所需的
  1. 运行迁移
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:

在控制器中使用外观MetaTagapp/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模板中使用外观MetaTagresources/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() 
!!}

链接