Laravel Blade @if 语句有条件检查路由名称

Sil*_*ash 0 php laravel laravel-blade

有没有办法用blade检查当前页面的路由名称?我有一个刀片布局模板,需要<body>通过当前 uri 更改其属性。请注意,我需要通过blade而不是js来完成此操作。

这是一个示例片段,说明了我想要做的事情:

@if (URL::current() == {{ route('admin.index') }}) //not valid syntax
    <body class="dashboard" id="top">
@else 
    <body class="not-dashboard" id="not-top">
@endif
Run Code Online (Sandbox Code Playgroud)

Kam*_*aul 5

你可以尝试routeIs()助手

@if (request()->routeIs('admin.index'))
<body class="dashboard" id="top">
@else
<body class="not-dashboard" id="not-top">
@endif
Run Code Online (Sandbox Code Playgroud)

并且您的代码有语法错误,修复方法是

@if (URL::current() == route('admin.index')) // remove those curly braces  
    <body class="dashboard" id="top">
@else 
    <body class="not-dashboard" id="not-top">
@endif
Run Code Online (Sandbox Code Playgroud)