如何将数据传递给布局文件?我只能在内容页面上访问传递的数据,而不能在布局文件上访问。
public function get_index($name){
return View::make('widget.'.$name)
->with("title", ucwords($name).' ‹ Document Management System');
}
利用
View::share('data', $data);
在您的before
过滤器或__construct
您的Base_Controller
.
您需要一个全局视图变量。我认为您需要查看View::share('title', $title);
我也认为您可以将其与->shares('title', $title)
您还可以$this->layout->with('foo', 'bar')
在操作中使用 from 使变量foo
可用于您的布局。
我最近遇到了一种情况,我需要在我的主模板中动态更改 @include('layouts._sidebarLeft') 语句。View::share($key, $value) 解决了这个问题。然后我将我的模板更改为@include($key),每当我需要从默认模板更改它时,我只需在控制器中返回视图之前运行另一个 View::share()。我在 /start/global.php 中定义了默认侧边栏
使用View::share()
https://laravel.com/docs/7.x/views#sharing-data-with-all-views
7.x 的文档说要在 ServiceProvider 中调用此方法。