在laravel应用程序中创建动态元标记

问题描述 投票:0回答:1

我正在尝试在我的laravel应用程序中实现动态元标记。我有一个head.blade.php文件,其中包含所有代码。这就是我一直在做的,我在pages文件夹下创建了一个名为meta-conf.php的文件,head.blade.php包含 -

@if(Route::currentRouteName() != false) 
{{
    $page = Route::currentRouteName();
}}
    @include('pages.meta-conf');
{{
    $title = $meta[$page]['title'];
    $keywords = $meta[$page]['keywords'];
    $description = $meta[$page]['description'];
}}
@else
{{
   $title = "akademe";
   $keywords = "";
   $description = "Quickly find near by courses";
}}
@endif

meta-conf.php包含 -

<?php

$meta['index']['title'] = "Home";
$meta['index']['keywords'] = "explore, learn, repeat, education, register, institute, courses";
$meta['index']['description'] = "Quickly find near by courses";

现在它给我一个错误“Undefined variable:meta”。我已经包含了meta-conf.php文件,其中包含定义的$ meta数组,如上所示。不知是什么导致了这个问题。

php laravel dynamic tags blade
1个回答
0
投票

我会做什么而不是你正在做的是这样的:

  • 将标题,关键字和描述作为公共变量添加到基础控制器(app/Http/Controllers/Controller.php
  • 初始化Controller构造函数上的变量并设置“默认”值
  • 从您的控制器(课程,搜索等)设置每个页面的特定值
  • 在基础控制器上实现“renderPage”方法,以便您可以从控制器传递视图和参数
  • Render方法将加载请求的视图并传递参数和元标记变量
  • 更新您的head.blade.php以使用您的元变量

我没有测试过上面的任何一个,但是认为这样的东西应该可行:)

© www.soinside.com 2019 - 2024. All rights reserved.