【问题标题】:Laravel Master Layout is being evaluated when using another layout使用其他布局时正在评估 Laravel 主布局
【发布时间】:2015-03-15 21:48:56
【问题描述】:

我一直在尝试在刀片布局文件中提供一组变量,例如

  • layouts/master.blade.php
  • layouts/admin.php

我使用的变量名为 $sitesettings->sitetitle。

我这样输出这个变量:

    <title>{{ $sitesettings->sitetitle }}</title>

此变量填充在 BaseController 中:

class BaseController extends Controller 
{

    public function __construct()
    {   
        View::share('sitesettings', SiteSetting::find(1));
    }

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if ( !is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }
    }

}

问题是当我访问管理布局时。主人似乎正在接受评估,并出现以下错误。

ErrorException
Undefined variable: sitesettings (View: /home/vagrant/Code/DanielDelCore/laravel/app/views/layouts/master.blade.php) (View: /home/vagrant/Code/DanielDelCore/laravel/app/views/layouts/master.blade.php)

提前感谢您的帮助。

丹尼尔。

【问题讨论】:

    标签: php laravel blade


    【解决方案1】:

    我不确定为什么会出现这个问题,但我建议您使用不同的方法。 View composers.

    View::composer(['layouts.master', 'layouts.admin'], function($view){
        $view->with('sitesettings', SiteSetting::find(1));
    });
    

    您可以将此代码放入app/filters.php 或创建一个新的app/composers.php 并将其包含在app/start/global.php 的末尾:

    require app_path().'/composers.php';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2012-05-18
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多