Support the ongoing development of Laravel.io →
posted 9 years ago
Views
Last updated 1 year ago.
0

So you're trying to make a dynamic menu?

Last updated 1 year ago.
0

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>
Last updated 1 year ago.
0

getmoneydouble said:

So you're trying to make a dynamic menu?

Yep

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0
Last updated 1 year ago.
0

@livana,

how did you access activeSlug via view where did you place it?

Last updated 1 year ago.
0

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"
		]
Last updated 1 year ago.
0

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");
        }
    });
Last updated 1 year ago.
0

Does this work for routes using slug?

0

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.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

edubuc edubuc Joined 17 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.