0

My question is how I can read current route name in Laravel FW if I have for example this route group:

        Route::group(['prefix' => 'machines'], function(){
        // GET
        Route::get('/', array('uses' => 'ServerController@index', 'as' => 'machines'));
        Route::get('/update/{id}', array('uses' => 'ServerController@update', 'as' => 'machines.Update')); 
        //POST
        Route::post('/updatePost/{id}', array('uses' => 'ServerController@updatePost', 'as' => 'machines.UpdatePost'));

        });

Normally I'm reading this value with: Route::currentRouteName() but it working only to Route::get('/', ***); and does not work for (for example: Route::get('/update/{id}'); which name is 'machines.Update'. It's returning nothing.

Why I'm doing this? Im trying to create "active menu". For now, my function presenting like this:

HTML::macro('nav_link', function ($route, $title, $parameters = array()) {

$class = '';

if(Route::currentRouteName() == $route) {
    $class = ' class="active" ';
} else {
    $class = '';
}

return '<li' . $class . '>' . link_to_route($route, $title, $parameters = array()) . '</li>';

});

and

{{ HTML::nav_link('dashboard', 'Dashboard', array()); }}
{{ HTML::nav_link('machines', 'Machines', array()); }}

I want to create "dynamic" macro which will create <li> items and it will check if it is the active item and next will give them active state by class="active".

SOLUTION:

In Laravel 4.1 function Route::currentRouteName() was changed to Route::current()->getName() and now it working as hell :)

For posterity:

HTML::macro('nav_link', function ($route, $title, $parameters = array()) {

$class = '';

if(strpos(Route::current()->getName(), $route) !== false) {
    $class = ' class="active" ';
} else {
    $class = '';
}

return '<li' . $class . '>' . link_to_route($route, $title, $parameters = array()) . '</li>';

});

Usage:

{{ HTML::nav_link('machines', 'Machines', array()); }}

It's working code for "dynamic active menu".

2
  • Where are you using currentRouteName? Why? EDIT: Also, seems that in Laravel 4.1 currentRouteName has been replaced. github.com/laravel/framework/issues/2919 Commented Aug 30, 2014 at 19:07
  • Yes but it's working for now (still). I'm trying to do the "active menu", by comparing the names of routes. Commented Aug 30, 2014 at 19:10

1 Answer 1

0

From your comment, use Request::is

<li {{ Request::is('portal') ? 'class="active"' : '' }}>
    <a href="{{ URL::route('admin.dashboard') }}"><i class="fa fa-dashboard"></i>Dashboard</a>
</li>
Sign up to request clarification or add additional context in comments.

4 Comments

You can also use wildcards, e.g. Request::is('update/*')
Its ok for "manual check" but look now on my question I edited it.
What version of Laravel are you using?
4.1. A second before I found function in documentation that returns what I wanted (it was hard). Route::currentRouteName() in Laravel 4.1 was changed to Route::current()->getName() and now it properly returning what I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.