LoginSignup
10
18

More than 5 years have passed since last update.

Laravelでルーティングネームを取得する

Posted at

インクルードファイルを作ってる時とか、トップページにだけ●●を表示みたいなことをすると思います。

そういう時、こう書いたらいいのですが

@if(\Request::is('/'))
<p>index</p>
@endif

編集ページや詳細ページのみに●●を表示、みたいなことをするとき、上の書き方だと苦労することになりますよね?
そこで、きっと設定しているであろうnamespaceの値を取得して、それを用いて条件分岐させます。

routes/web.php
Route::get('/', 'ItemsController@index')->name('index');
Route::pattern('items', '\d+');
Route::get('/{items}', 'ItemsController@detail')->name('detail');
resources/views/layouts/app.blade.php
@if(\Route::current() -> getName() == 'index')
<p>index</p>
@endif

@if(\Route::current() -> getName() == 'detail')
<p>detail</p>
@endif
10
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
18