Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Route name in View

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

  • Dashboard
  • Pages
    • List
    • Add
  • Articles
    • List
    • Add

Now I want to bold Pages when user is in this section,

and if is in Add page I want bold both Pages and Add

my routes.php is :

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)

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

like image 613
MajAfy Avatar asked Feb 07 '14 16:02

MajAfy


People also ask

How do I find out my current route Vue?

We can use this. $router. currentRoute. path property in a vue router component to get the current path.

Is Route name Laravel?

Named routing is another amazing feature of Laravel framework. Named routes allow referring to routes when generating redirects or Urls more comfortably. You can specify named routes by chaining the name method onto the route definition: Route::get('user/profile', function () { // })->name('profile');


3 Answers

In Blade:

<p style="font-weight:{{ (Route::current()->getName() == 'admin.pages.index' && Request::segment(0) == 'add') ? 'bold' : 'normal' }};">Pages</p>
like image 79
marcanuy Avatar answered Oct 29 '22 00:10

marcanuy


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

yoursite/admin/users/create

will give:

array(2) {
    [0] "admin"
    [1] "users"
    [2] "create"
}
like image 8
diegofelix Avatar answered Oct 28 '22 23:10

diegofelix


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

Route::currentRouteAction();

This will return action like HomeController@index and you can use something like this in your view

<!-- 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.

like image 7
The Alpha Avatar answered Oct 29 '22 01:10

The Alpha