Laravel未定义变量,但变量已定义


Laravel undefined variable but variable is defined

使用laravel在控制器中设置一个布局变量:

$this->layout->title = 'Foo';
$this->layout->content = View::make('foo.bar');

但是当我在模板中使用$title时,我得到undefined错误:

<title>{{{ $title }}}</title>

但是如果我使用条件语句

<title>{{{ $title or 'Default' }}}</title>

我没有得到错误,它使用$title变量

根据Laravel API for Illuminate'View'View:

protected string     $view    The name of the view.
protected array      $data    The array of view data.

因此,要通过控制器的属性将数据传递给视图,你需要这样做:

$this->layout->data = array('title' => 'Foo');