So you're trying to make a dynamic menu?
beter way for display menu in all page use laravel View Composer
and for show active url use a helper function like this:
function activeSlug($slug)
{
$curentSlug = Request::segment(2);
if($curentSlug === trim($slug))
{
return 'class = "active"';
}
return false;
}
and now in blade template
<a href="/site/posts" {{ activeSlug('posts') }} >Posts</a>
getmoneydouble said:
So you're trying to make a dynamic menu?
Yep
livana said:
beter way for display menu in all page use laravel View Composer
and for show active url use a helper function like this:
function activeSlug($slug) { $curentSlug = Request::segment(2); if($curentSlug === trim($slug)) { return 'class = "active"'; } return false; }
and now in blade template
<a href="/site/posts" {{ activeSlug('posts') }} >Posts</a>
Sorry never used View Composer before, where the class must be located? How we register it?
how did you access activeSlug via view where did you place it?
you can create a file helper.php and in app/start/global.php ad this code:
require app_path().'/filters.php';
also for autoloading you can use composer.json file like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
},
"files": [
"app/helper.php"
]
why not to use javascript to select active menu item? I do it like this:
/*
* making meniu active
*/
var menuLinks = $(".nav-list > li > a");
$.each(menuLinks, function(){
var url = $(this).attr("href");
var urlRegex = new RegExp(url + ".*","i");
if(document.URL.match(urlRegex) != null && url != "#"){
$(this).parent().addClass("active");
}
});
If you have menu items that derive from parent items you need to structure your database keeping the "facet pattern" in mind.
To activate a menu item use a package like https://github.com/letrunghieu/active
You can write your own menu wrapper class to do this easily.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community