没有前缀名称的Laravel路由

时间:2018-06-21 09:23:11

标签: php laravel

我正在尝试利用Laravel目标网页路线。例如,Laravel默认情况下会将您带到欢迎页面,并且在斜杠后没有网址文本。

Route::get('/', function () {
    return view('welcome');
});

在我的欢迎页面中,我使用以下条件根据路线名称应用样式。

{!! Route::is('/')? 'class="index"':'' !!}

但是此代码不起作用。如何正确查看欢迎页面的路线?

编辑:使用“请求”而不是“路由”可以使它工作。但是,为了保持一致性,我想知道是否也可以使用“ Route”来完成。

5 个答案:

答案 0 :(得分:1)

尝试

O_DIRECT (Since Linux 2.4.10)
          Try to minimize cache effects of the I/O to and from this file.  In general this will degrade  perfor‐
          mance,  but  it is useful in special situations, such as when applications do their own caching.  File
          I/O is done directly to/from user-space buffers.  The O_DIRECT flag on its  own  makes  an  effort  to
          transfer  data synchronously, but does not give the guarantees of the O_SYNC flag that data and neces‐
          sary metadata are transferred.  To guarantee synchronous I/O, O_SYNC  must  be  used  in  addition  to
          O_DIRECT.  See NOTES below for further discussion.

或更简单地

@if(Request::is('/'))
  class="index"
@endif

答案 1 :(得分:1)

尝试一下:

<li class="@if (request()->is('/')) index @endif"></li>
    // ...
</li>

您还可以在此使用通配符:

<li class="@if (request()->is('/some-url/*')) active @endif">
    // ...
</li>

答案 2 :(得分:1)

命名您的路线,并使用该名称来设置样式

Route::get('/', function () {
    return view('welcome');
})->name('home');

在您的刀片中

{!! (Route::currentRouteName() == 'home')? 'class="index"':'' !!}

答案 3 :(得分:1)

尝试一下:

<li {!! Request::is('/') ? 'class=index' : '' !!}>...</li>

答案 4 :(得分:1)

Route::get('/', function () {
    return view('welcome');
})->name('home');

刀片式

class = "default class @if(\Request::route()->getName() == 'home')your_class @endif"
相关问题