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