Laravel:如何将变量传递给我的基本布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31274875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: how to pass a variable to my basic layout
提问by Red Cube
I am trying to pass a variable to my basic layout. This is because I need it in all pages. I thought that writing something like that on my BaseController
我正在尝试将变量传递给我的基本布局。这是因为我在所有页面中都需要它。我认为在我的 BaseController 上写类似的东西
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$footertext = MyText::where('status', 1);
$this->layout = View::make($this->layout, ['footertext' =>$footertext ]);
}
}
}
And on my I thought that writing something like that on my main.blade.php could work.
我认为在我的 main.blade.php 上写类似的东西是可行的。
{{ $footertext }}.
Instead I having this error,
相反,我有这个错误,
Undefined variable: footertext
未定义的变量:footertext
and after two hours of looking around...I didn't find any solution. Any help is welcome.
经过两个小时的环顾……我没有找到任何解决方案。欢迎任何帮助。
回答by Thiago Coelho
Not long ago I was trying to do the same.
不久前,我也在尝试做同样的事情。
If you are using Laravel 5 you can edit the AppServiceProvider.php inside app/Providers
and register a provider for this layout like:
如果您使用的是 Laravel 5,您可以在里面编辑 AppServiceProvider.phpapp/Providers
并为此布局注册一个提供程序,例如:
public function boot()
{
view()->composer('my.layout', function($view) {
$myvar = 'test';
$view->with('data', array('myvar' => $myvar));
});
}
Now if you are using Laravel 4 I think it's more simple. In the app/filters.php
:
现在,如果您使用的是 Laravel 4,我认为它更简单。在app/filters.php
:
View::composer('my.layout', function ($view) {
$view->with('variable', $variable);
});
In both ways any variable you pass will be available to all templates that are extending the master template.
在这两种方式中,您传递的任何变量都可用于扩展主模板的所有模板。
References:
参考:
https://laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-templatehttps://coderwall.com/p/kqxdug/share-a-variable-across-views-in-laravel?p=1&q=author%3Aeuantor
https://laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-template https://coderwall.com/p/kqxdug/share-a-variable-across-views -in-laravel?p=1&q=author%3Aeuantor
回答by Grig Dodon
For laravel 5.3 I am using in AppServiceProvider.php
inside app/Providers
对于 laravel 5.3 我在AppServiceProvider.php
里面使用app/Providers
public function boot()
{
view()->composer('layouts.master', function($view)
{
$view->with('variable', 'myvariable');
});
}
参考:https: //laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-template
*Dedicated Class inlcuded
*包括专用课程
回答by Alex
In Laravel 5.6 none of these methods work.
AppServiceProvider
:
在 Laravel 5.6 中,这些方法都不起作用。
AppServiceProvider
:
public function boot()
{
View::share('key', 'value');
}
Where View
is the facade Illuminate\Support\Facades\View
View
门面在哪里Illuminate\Support\Facades\View
回答by Zakaria Acharki
Sometimes we need to pass data from controller to view in laravel like working with database query, select option and more. it's simple and easy with built in function in laravel. We can send data from controller to view easily with with() in laravel. Also there are more way to send or pass data to view from controller. I am describing some easy way how to pass data form controller to view in laravel.
有时我们需要将数据从控制器传递到 laravel 中查看,比如使用数据库查询、选择选项等等。laravel 内置函数,简单易行。我们可以使用 Laravel 中的 with() 从控制器发送数据以轻松查看。还有更多方法可以发送或传递数据以从控制器查看。我正在描述一些如何传递数据表单控制器以在 Laravel 中查看的简单方法。
1. Passing an array :
1. 传递数组:
$data = array(
'name' => 'Rakesh',
'email' => '[email protected]'
);
return View::make('user')->with($data);
//Accesing $data on view :-
{{$data}}
{{$data['email']}}
2. Working with query :
2. 使用查询:
function view() {
$q = Model::where('name', '=', 'Foo')->first();
$users = Model::order_by('list_order', 'ASC')->get();
return $view->with('users', $users)->with('q', $q);
}
//Accesing on view :-
{{ $q->name }}
Hope that help you :)
希望对你有帮助:)