4

如何将数据传递给布局文件?我只能在内容页面上访问传递的数据,而不能在布局文件上访问。

public function get_index($name){
    return View::make('widget.'.$name)
            ->with("title", ucwords($name).' ‹ Document Management System');
}
4

5 回答 5

7

利用

View::share('data', $data);

在您的before过滤器或__construct您的Base_Controller.

于 2013-02-12T15:47:33.080 回答
3

您需要一个全局视图变量。我认为您需要查看View::share('title', $title); 我也认为您可以将其与->shares('title', $title)

于 2013-02-12T06:46:01.140 回答
2

您还可以$this->layout->with('foo', 'bar')在操作中使用 from 使变量foo可用于您的布局。

于 2013-11-13T19:46:01.263 回答
0

我最近遇到了一种情况,我需要在我的主模板中动态更改 @include('layouts._sidebarLeft') 语句。View::share($key, $value) 解决了这个问题。然后我将我的模板更改为@include($key),每当我需要从默认模板更改它时,我只需在控制器中返回视图之前运行另一个 View::share()。我在 /start/global.php 中定义了默认侧边栏

于 2013-02-17T21:23:23.750 回答
0

使用View::share() https://laravel.com/docs/7.x/views#sharing-data-with-all-views

7.x 的文档说要在 ServiceProvider 中调用此方法。

于 2020-07-26T18:06:12.970 回答