Get route name in view

I am trying to create a navigation menu, I have 3 such elements:

  • Control Panel
  • Pages
    • List
    • Add
  • Articles
    • List
    • Add

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

and if the page AddI want as a bold PagesandAdd

my 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 the thid method:

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

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

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

+3
source share
3 answers

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

Route::currentRouteAction();

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) )) }}

Route::currentRouteName() , 'as' => 'routename' .

+4

Request::segments() URL-, :

yoursite/admin/users/create

:

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

In a click:

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

All Articles