每页注入元标记

Inject meta tags per page

所以我 运行 一个 Laravel 5.4 项目,我们有一个系统,每个页面都需要单独的 META 标签来正确显示人们何时发布推文网址等。所以我的问题:

如何将以下代码注入我的 head 标签?

例如新文章可能如下所示:

<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@mysite">
<meta name="twitter:title" content="News Article - Donald Trumps Twitter Deleted!">
<meta name="twitter:image" content="imageurl">
<meta name="twitter:description" content="Donald Trump lost access to his twitter for...">
<meta name="twitter:domain" content="https://www.example.com">
<meta name="description" content="Donald Trump lost access to his twitter for..."/>
<meta name="keywords" content="News, Donald, Trump, Media, Reporting, Urgent, Breaking, Twitter"/>

.

而论坛 post 会是这样的:

<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@mysite">
<meta name="twitter:title" content="Forum - Welcome to the forum!">
<meta name="twitter:image" content="imageurl">
<meta name="twitter:description" content="Hey there and welcome to our forum! For the chance to win 0 please...">
<meta name="twitter:domain" content="https://www.example.com">
<meta name="description" content="Hey there and welcome to our forum! For the chance to win 0 please..."/>
<meta name="keywords" content="Forum, Thread, Post, Win, Welcome, Twitter"/>

.

那么,怎么样?花了一个小时搜索这个,我很困惑!

查看有关模板继承的 laravel 文档 (https://laravel.com/docs/5.2/blade#template-inheritance)

您可以通过在 blade

中使用 @section('title',$article->title) 将元数据注入 <head></head>

您的情况示例:

<head>
    <meta name="twitter:title" content="@yield('twitter_title')">
    <meta name="twitter:image" content="@yield('twitter_image')">
</head>

在你的blade中:

@extends('layouts.layout') //This loads the default blade with your <head></head> section

@section('twitter_title',$article->title)
@section('twitter_image',$article->image)