Laravel模板-将数据传递到布局


Laravel template - passing data to layout

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

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

使用

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

在您的before过滤器或Base_Controller__construct中。

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

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

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

使用View::share()https://laravel.com/docs/7.x/views#sharing-所有视图的数据

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