简体   繁体   English

使用 Route::is() 检查路由是否为主页

[英]Using Route::is() to check if the route is the homepage

How can i check to see if the current route is the root page?如何检查当前路由是否为根页面?

i've tried in a blade file typing thie following:我试过在刀片文件中输入以下内容:

@if(Route::is('/')
//show something
@else 
//show something else
@endif

i've also tried it with Route::currentRouteName() such as我也用 Route::currentRouteName() 试过了,比如

@if(Route::currentRouteName() == '/')
//do something
@else
//do something else
@endif

and it doesn't seem to work on the root page.它似乎在根页面上不起作用。 is there an alternative way?有没有替代方法? or is it that the root doesn't have a route?还是根没有路由?

Route::get('/', 'HomeController@index')->name('root');

@if(Route::currentRouteName() == 'root')
  //do something
@else
 //do something else
@endif

you can use your function to check currentRouteName, you just need to make sure you set the name for that route like I mentioned below:您可以使用您的函数来检查 currentRouteName,您只需要确保像我下面提到的那样为该路线设置名称:

Route::get('/', 'HomeController@index')->name('home'); Route::get('/', 'HomeController@index')->name('home');

@if(Route::currentRouteName() == 'home')
//do something
@else
//do something else
@endif
@if(Request::is('/'))
  //do something
@else
  //do something
@endif

Alternatively, you can also use routeIs() :或者,您也可以使用routeIs()

@if(request()->routeIs('home'))
  // yay
@else
  // nay
@endif

The helper works like the normal "is", which means that it supports multiple patterns, but matches against route names instead. helper 的工作方式与正常的“is”类似,这意味着它支持多种模式,但与路由名称匹配。

How can i check to see if the current route is the root page?如何检查当前路由是否为根页?

i've tried in a blade file typing thie following:我已经尝试在刀片文件中键入以下内容:

@if(Route::is('/')
//show something
@else 
//show something else
@endif

i've also tried it with Route::currentRouteName() such as我也尝试过Route :: currentRouteName()这样

@if(Route::currentRouteName() == '/')
//do something
@else
//do something else
@endif

and it doesn't seem to work on the root page.而且它似乎在根页面上不起作用。 is there an alternative way?还有其他方法吗? or is it that the root doesn't have a route?还是根没有路由?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM