laravel 如何在laravel的一个函数中为同一路线返回多个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43270756/
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
How to return multiple views in one function in laravel for same route
提问by Omi kabira
it is quite difficult to explain my question. I hope you will be able understand it.
很难解释我的问题。我希望你能理解它。
For my project I have a master layout which contains the header and yield of content.
对于我的项目,我有一个包含标题和内容产量的主布局。
master.blade.php
master.blade.php
@include('partials.header')
<div class="container-fluid main-con">
@yield('content')
@include('partials.footer')
</div>
now on main public route I am raturning a method.
现在在主要公共路线上,我正在讨论一种方法。
Web.php
网页.php
Route::get('/', [
'uses' => 'ProductContoller@getIndex',
'as' => 'product.mainCats'
]);
ProductContoller.php
产品控制器.php
class ProductContoller extends Controller
{
public function getIndex(){
$ad_cats = Mainaddtype::orderBy('title')->get();
return view( 'shop.main-categories-page', ['mediacats' => $ad_cats]);
}
}
Now my query is, 'main-categories-page' is yielding in content section of master. but I want the same data of Mainadtype in header also. So I want to return view of header on same function. Please help if u understand it.
现在我的查询是,'main-categories-page' 在 master 的内容部分产生。但我也想要标题中 Mainadtype 的相同数据。所以我想返回相同功能的标题视图。如果你理解它,请帮助。
what I am trying now is,
我现在正在尝试的是
public function getIndex(){
$ad_cats = Mainaddtype::orderBy('title')->get();
return view( 'shop.main-categories-page', ['mediacats' => $ad_cats]);
return view( 'partials.header', ['mediacats' => $ad_cats]);
}
But I know we cant return in this way. Thanks in advance.
但我知道我们不能这样回去。提前致谢。
回答by Sayantan Das
You can access the variable in the header as well. Just have a check to see if the variable exists so that you don't get errors when you load other views.
您也可以访问标头中的变量。只需检查变量是否存在,这样在加载其他视图时就不会出错。
@if(!empty($mediacats))
// do awesome stuffs with $mediacats
@endif
This way your data will be available when you are on index page.
这样,当您在索引页面上时,您的数据将可用。
If you want the data to be available in all views, you can use laravel view composer. From official docs:
如果您希望数据在所有视图中可用,您可以使用laravel 视图 composer。来自官方文档:
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location
视图组合器是在呈现视图时调用的回调或类方法。如果您希望在每次渲染视图时将数据绑定到视图,视图编辑器可以帮助您将该逻辑组织到一个位置
Hope it helps.
希望能帮助到你。
回答by D. Pachauri
function gets terminate as it gets the first return statement and also two views can't be returned in laravel, the only possible thing you can do is include view in layout template. example adding category view in template.
函数在获得第一个 return 语句时终止,并且在 laravel 中无法返回两个视图,您唯一可以做的就是在布局模板中包含视图。在模板中添加类别视图的示例。
@if(showStore)
@include('store.category')
@endif