我是Laravel和Blade的新手。我有两个不同的内容,一个用于Home,第二个用于任何视图,不包括home。
我有这段代码,但没有任何反应:
@if(view('home')) // Display content if is home, display 'foo'
@include('partials/foo')
@else
@include('partials/bar') // Okay, not in home, display 'bar'
@endif;
这是正确的方法吗?
答案 0 :(得分:0)
在刀片中使用Route类来获取方法并根据方法决定
@if(\Route::getCurrentRoute()->getActionMethod() == 'index')
@include('partials/foo')
@else
@include('partials/bar') // Okay, not in home, display 'bar'
@endif;
答案 1 :(得分:0)
我使用此How to get the current URL inside @if statement (blade) in Laravel 4?
解决了问题@if(Request::is('/')) // Homepage
'foo'
@else // All pages
'bar'
@endif
答案 2 :(得分:0)
通常,您将使用控制器。流程如下:
/home
在routes.web中:
Route::get('home', 'HomeController@index');
在HomeController类中:
public function index()
{
$variable = ModelName::where('field', 'value')->first();
return view('home')->with('variable', $variable);
}
这使您无需检查刀片中的视图,因为您专门将其发送到那里。
如果您不需要,请删除变量。