laravel 在视图中获取路线名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21632714/
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 08:59:33  来源:igfitidea点击:

get Route name in View

phpviewcontrollerlaravel

提问by MajAfy

I trying to design navigation menu, I have 3 Items like this:

我试图设计导航菜单,我有 3 个这样的项目:

  • Dashboard
  • Pages
    • List
    • Add
  • Articles
    • List
    • Add
  • 仪表盘
  • 页面
    • 列表
    • 添加
  • 文章
    • 列表
    • 添加

Now I want to bold Pageswhen user is in this section,

现在我想当Pages用户在这个部分时加粗,

and if is in Addpage I want bold both Pagesand Add

如果在Add页面中,我想要加粗PagesAdd

my routes.phpis :

我的routes.php是:

Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function()
{
    Route::any('/', 'App\Controllers\Admin\PagesController@index');
    Route::resource('articles', 'App\Controllers\Admin\ArticlesController');
    Route::resource('pages', 'App\Controllers\Admin\PagesController');
});

I found thid method :

我找到了这种方法:

$name = \Route::currentRouteName();
var_dump($name);

But this method return string 'admin.pages.index' (length=17)

但是这个方法返回 string 'admin.pages.index' (length=17)

Should I use spliteto get controller or Laravel have a method for this ?

我应该splite用来让控制器或 Laravel 有一个方法吗?

采纳答案by The Alpha

You may use this (to get current action, i.e. HomeController@index)

您可以使用它(获取当前操作,即HomeController@index

Route::currentRouteAction();

This will return actionlike HomeController@indexand you can use something like this in your view

这将返回action喜欢HomeController@index,你可以使用这样的事情在你看来

<!-- echo the controller name as 'HomeController' -->
{{ dd(substr(Route::currentRouteAction(), 0, (strpos(Route::currentRouteAction(), '@') -1) )) }}

<!-- echo the method name as 'index' -->
{{ dd(substr(Route::currentRouteAction(), (strpos(Route::currentRouteAction(), '@') + 1) )) }}

The Route::currentRouteName()method returns the name of your route that is used as 'as' => 'routename'in your route declaration.

Route::currentRouteName()方法返回'as' => 'routename'在路由声明中使用的路由名称。

回答by marcanuy

In Blade:

在刀片中:

<p style="font-weight:{{ (Route::current()->getName() == 'admin.pages.index' && Request::segment(0) == 'add') ? 'bold' : 'normal' }};">Pages</p>

回答by diegofelix

Request::segments()will return an array with the current url for example:

Request::segments()将返回一个包含当前 url 的数组,例如:

yoursite/admin/users/create

will give:

会给:

array(2) {
    [0] "admin"
    [1] "users"
    [2] "create"
}