Every method I have ever come up with always seems like a hack. Id like to be able to do something like, if the url matches this pattern then this element is active.
<?php/*
|--------------------------------------------------------------------------
| Detect Active Route
|--------------------------------------------------------------------------
|
| Compare given route with current route and return output if they match.
| Very useful for navigation, marking if the link is active.
|
*/functionisActiveRoute($route, $output = "active"){
if (Route::currentRouteName() == $route) return $output;
}
/*
|--------------------------------------------------------------------------
| Detect Active Routes
|--------------------------------------------------------------------------
|
| Compare given routes with current route and return output if they match.
| Very useful for navigation, marking if the link is active.
|
*/functionareActiveRoutes(Array $routes, $output = "active"){
foreach ($routes as $route)
{
if (Route::currentRouteName() == $route) return $output;
}
}
@bashy Hey. Not sure if I'm doing a good thing by posting to this thread, however because I am in the same situation and because I adopted your way of going with the help, I must ask you why don't I get something when using your code although my URI looks like thishttp://www.example.xyz/categorii/muzica
@bashy Sorry, it was my mistake. I dd in the custom helper class itself and I've managed to see what I was doing wrong. I passed in the argument same as what segment(1) returned. Therefore it was something like "categorii/categorii" instead of "categorii/muzica". Sorry for the trouble.
@bashy Well, I'm new to Laravel, however from my point of view Service Providers would be the way to go with this. And I would appreciate if you can prove me wrong or right, because I can't understand IoC and Service Providers although I read the docs like 10 times. But that how I see Service Provider put to great use.
@claudiu_pelmus Well that snippet only does one level so I normally do this now
/*
|--------------------------------------------------------------------------
| Detect Active Route
|--------------------------------------------------------------------------
|
| Compare given route with current route and return output if they match.
| Very useful for navigation, marking if the link is active.
|
*/publicstaticfunctionisActiveRoute($route, $output = 'active'){
if (Route::currentRouteName() == $route) {
return $output;
}
}
/*
|--------------------------------------------------------------------------
| Detect Active Routes
|--------------------------------------------------------------------------
|
| Compare given routes with current route and return output if they match.
| Very useful for navigation, marking if the link is active.
|
*/publicstaticfunctionareActiveRoutes(Array $routes, $output = 'active'){
foreach ($routes as $route) {
if (Route::currentRouteName() == $route) {
return $output;
}
}
}