包括基于Laravel路线

时间:2014-01-19 18:22:32

标签: laravel-4 blade

如何根据Laravel刀片使用的路径“类别”包含文件?我基本上试图在我的视图模板的子导航部分中执行此操作:

应用程序/视图/布局/ default.blade.php

@if (Route::resource() == 'tasks')
    @include('navs.task')
@elseif (Route::resource() == 'projs')
    @include('navs.proj')
@elseif (Route::resource() == 'miscs')
    @include('navs.misc')
@else 
    @include('navs.info')
@endif

但是会抛出错误“未定义的类常量'资源'”

3 个答案:

答案 0 :(得分:0)

你在Route :: currentRouteName()之后。

@if (Route::currentRouteName() == 'tasks')
    @include('navs.task')
@elseif (Route::currentRouteName() == 'proj')
    @include('navs.proj')
@elseif (Route::currentRouteName() == 'misc')
    @include('navs.misc')
@else 
    @include('navs.info')
@endif

答案 1 :(得分:0)

很晚,但我认为使用Request :: is是一种更简单的方法。

@if (Route::is('tasks'))
   @include('navs.task')
@elseif (Route::is('projs'))
   @include('nav.proj')
@elseif (Route::is('miscs'))
    @include('nav.misc')
@else 
    @include('nav.info')
@endif

此外,如果还有其他情况,请使用Switch案例。

答案 2 :(得分:-1)

迈克尔P:你的回答让我得到了正确答案。 Route :: currentRouteName()返回过于细化的'task.index'或'proj.create'。 Route :: current() - > getUri()有效。

@if (Route::current()->getUri() == 'tasks')
    @include('nav.task')
@elseif (Route::current()->getUri() == 'projs')
    @include('nav.proj')
@elseif (Route::current()->getUri() == 'miscs')
    @include('nav.misc')
@else 
    @include('nav.info')
@endif