Laravel 5 检查当前 url 是否与操作匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30425129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:37:28  来源:igfitidea点击:

Laravel 5 check if current url matches action

phplaravelroutinglaravel-5

提问by Ozzy

I am currently generating my application urls using {{action('Namespace\Class@method')}}. How would I check if the current page request maps to that current action Namespace\Class@method?

我目前正在使用{{action('Namespace\Class@method')}}. 我将如何检查当前页面请求是否映射到当前操作Namespace\Class@method

I would like to do something like:

我想做类似的事情:

<a href="{{action('Namespace\Class@method')}}
  @if (currentAction('Namespace\Class@method'))
    class="active"
  @endif
>Some link</a>

How would I achieve this in Laravel 5?

我将如何在 Laravel 5 中实现这一目标?

回答by lukasgeiter

There's no built in method for this, however you can retrieve the current action name with Route::currentRouteAction(). Unfortunately this method will return a fully namespaced class name. So you will get something like:

没有为此的内置方法,但是您可以使用Route::currentRouteAction(). 不幸的是,此方法将返回一个完全命名空间的类名。所以你会得到类似的东西:

App\Http\Controllers\FooBarController@method

You can either check for that or use something like ends_withso you don't have to specify the full path:

您可以检查它或使用类似的东西,ends_with这样您就不必指定完整路径:

@if(ends_with(Route::currentRouteAction(), 'FooBarController@method'))


You might also consider namingyour routes with 'as' => 'route.name'. This would allow you to use: Route::is('route.name')

您还可以考虑命名您的路线'as' => 'route.name'。这将允许您使用:Route::is('route.name')

回答by Daniel Doinov

Actualy I had it simplified to the following code:

实际上,我将其简化为以下代码:

 <li class="@if(Route::is('getLogin')) active @endif"><a href="{{URL::route('getLogin')}}">Login</a></li>

That assumes that you have named routes. Which is a good idea in the first place, because you migth change the url of an action without going through your whole project to change the links to that action.

这假设您已命名路由。首先这是一个好主意,因为您可以更改操作的 url,而无需通过整个项目来更改指向该操作的链接。

回答by whoacowboy

I know this is old, but for what it is worth.

我知道这是旧的,但它是值得的。

Laravel has a couple of built-in helper methods for referring to URLsaction and route.

Laravel 有几个内置的辅助方法用于引用 URL操作和路由。



action

action

Your route file would look like this.

您的路由文件将如下所示。

Route::get('/funtastic', 'FuntasticController@show');

Your blade view would look like this

你的刀片视图看起来像这样

<a href="{{action('FuntasticController@show')}}
  @if(action('FuntasticController@show') == Request::url())  
    class="active"
  @endif
>Some link</a>


route

route

If you use named routes.

如果您使用命名路由

<a href="{{route('namedRoute')}}
  @if(route('namedRoute') == Request::url()) 
    class="active"
  @endif
>Some link</a>

回答by Sajjad Ashraf

This is how I do it without any named routes

这就是我在没有任何命名路线的情况下做到的

<a class="{{ str_contains(request()->url(), '/some-page') ? 'active' : '' }}" href="/some-page">Some Page</a>

This is not a perfect solution but it works for most cases

这不是一个完美的解决方案,但它适用于大多数情况