Giter VIP home page Giter VIP logo

menu's Introduction

Menu

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

API Docs

Are you the type of person that writes menus by hand in view files or do you find yourself looking for the best place to store links to pages on your website? then Menu is for you!

Quick overview example

$menu = Menu::handler('mailbox'); 

// items
$menu
    ->add('contacts', 'Contacts')
    ->add('inbox', 'Inbox')
    ->raw(null, null, ['class' => 'divider'])
    ->add('folders', 'Folders', Menu::items() 
        ->prefixParents() 
        ->add('urgent', 'Urgent') // with prefix: /folders/urgent
        ->add('sent', 'Sent')
        ->add('deleted', 'Deleted')
    );

// styling
$menu
    ->addClass('nav navbar-nav')
    ->getItemsByContentType(Menu\Items\Contents\Link::class)
    ->map(function($item) {
        if ( $item->isActive() )  {
            $item->addClass('active');
        }
    });

{!! $menu !!} will output:

<ul class="nav navbar-nav">
  <li class="active"> <!-- current element in laravel, detected by library -->
    <a href="http://myapp.com/contacts">Contacts</a>
  </li>
  <li>
    <a href="http://myapp.com/inbox">Inbox</a>
  </li>
  <li class="divider"></li>
  <li>
    <a href="http://myapp.com/folders">Folders</a>
    <ul>
      <li>
        <a href="http://myapp.com/folders/urgent">Urgent</a>
      </li>
      <li>
        <a href="http://myapp.com/folders/sent">Sent</a>
      </li>
      <li>
        <a href="http://myapp.com/folders/deleted">Deleted</a>
      </li>
    </ul>
  </li>
</ul>

Key concepts

Item lists

An item list is what a menu is all about and it should be pretty self explanatory because it simply stores a list of items. there are some configurations available for an item list. You can, for example set the HMTL element that will be used to render the list, prefix every item in the list with all the parent's url segments, and a lot more. We will explore these options later.

Menu handlers

Menu handlers allow us to create and interact with item lists and act as a place to store and retrieve our menus. Because we are able to interact with multiple item lists at the same time some interesting possibilities become available to us.

Items

The Menu package has 2 types of items available out of the box.

  • Link For creating links to other pages
  • Raw Be free to add anything you like in the item. This type is usually used for dividers, titles etc.

The HTML element and attributes for the item can also be changed, more on this topic later.

Installing

Laravel 3

Install Menu via the artisan command line tool. Open the terminal and navigate to your Laravel project's root. Now type the following command :

php artisan bundle:install menu

To let Laravel know the Laravel Menu package should be started, open up application/packages.php and add the following lines to the packages array.

'menu' => array('auto' => true),

Laravel 4

Add this to your composer.json file's "require" :

"vespakoen/menu": "2.*"

And add the following to your app/config/app.php file :

  • In the Service Providers array : 'Menu\MenuServiceProvider',
  • In the aliases array : 'Menu' => 'Menu\Menu',

Laravel 5

Add this to your composer.json file's "require" :

"vespakoen/menu": "3.*"

And add the following to your config/app.php file :

  • In the Service Providers array : 'Menu\MenuServiceProvider',
  • In the aliases array : 'Menu' => 'Menu\Menu',

Lastly, run the following from your project's root dir:

php artisan vendor:publish

Basic usage

First, let's load some pages into the menu. We will do this by utilising the hydrate method.

Menu::handler('main')->hydrate(function()
  {
    return Page::with('translation')
      ->where('group', '=', 'main')
      ->get();
  },
  function($children, $item)
  {
    $children->add($item->translation->slug, $item->translation->name, Menu::items($item->as));
  });

/* the hydrate method takes these arguments
  $resolver       Closure     A callback to resolve results
  $decorator      Closure     A callback that gets called for every result fetched from the resolver with the corresponding ItemList and result as the arguments
  $idField        integer (default = 'id')           the property on the result that contains the id
  $parentIdField  integer (default = 'parent_id')    the property on the result that contains the parent id
  $parentId       integer (default = 0)              the parentId to start hydrating from
*/

Now that we have loaded our pages into the menu, and even identified every menu item with a name (via Menu::items($item->as)) a lot of options are available to us.

Find a node by it's name and add a subitem.

Menu::find('users')
  ->add('users/create', 'Create new user');

Add some properties to the root node

Menu::handler('main')
  ->addClass('nav navbar-nav');

Get all ItemLists at a certain depth and add a class

Menu::handler('main')
  ->getItemListsAtDepth(0)
  ->addClass('level-1');

Get all ItemLists at a depth range and change the element

Menu::handler('main')
  ->getItemListsAtDepthRange(0,2)
  ->setElement('div');

Get all Items at a certain depth and add a class

Menu::handler('main')
  ->getItemsAtDepth(0)
  ->addClass('level-1');

Get Items by it's content type and use the map function to walk over the results and perform actions based on gathered information

Menu::handler('main')
  ->getItemsByContentType('Menu\Items\Contents\Link')
  ->map(function($item)
  {
    if($item->isActive() && $item->hasChildren())
    {
      $item->addClass('is-active-link-with-children');
    }

    if($item->getContent()->getUrl() == 'home')
    {
      $item->addClass('is-home');
    }
  });

Get all ItemLists and add a class to them if they have children

Menu::handler('main')
  ->getAllItemLists()
  ->map(function($itemList)
  {
    if($itemList->hasChildren())
    {
      $itemList->addClass('has-children');
    }
  });

Breadcrumbs

Breadcrumb hell is a thing of the past.

Bootstrap ready breadcrums are as easy as this

Menu::handler('main')
    ->breadcrumbs()
    ->setElement('ol')
    ->addClass('breadcrumb');

The breadcrumbs method searches all handlers and returns a plain old ItemList, that you can manipulate. If you call the breadcrumbs method directly on the Menu class, it will search all your handlers for breadcrumbs, and by default will return the first match. However, there might be cases where you want to choose the breadcrumbs out of the ones it found, for this you can provide a callback method as the first argument.

And example is shown below:

Menu::breadcrumbs(function($itemLists)
    {
      return $itemLists[0]; // returns first match
    })
    ->setElement('ol')
    ->addClass('breadcrumb');

Diving deeper

The Laravel Menu packages consists of a couple of classes, but you can interact with all of them via the Menu class. Let's take a look at the handler method. it takes a string or an array as the only argument, the string(s) given are the names for the item lists we want to retrieve. If an itemlist we asked for didn't exist yet, it will create it for us. After the menu class has found and created the item lists we want, it will hand back a menuhandler that handles the item lists we asked for.

// Get a MenuHandler instance that handles an ItemList named "main"
Menu::handler('main');

When we call a method on this menu handler, it will simply forward the call to all the item lists that it handles. In order to find out what we can do now that we have a handler, we need to take a look at the methods on the ItemList class.

The ItemList class has a method called add that you are probably going to use a lot. It adds an Item of type "link" to the ItemList.

Menu::handler('main')->add('home', 'Homepage');

/* The add method takes these arguments
  $url  string  The URL to another page
  $title  string  The visible string on the link
  $children (default = null)  ItemList  (optional) The children of this page
  $link_attributes (default = array())  array (optional) HTML attributes for the <a> element
  $item_attributes (default = array())  array (optional) HTML attributes for the list element (usually <li>)
  $item_element (default = 'li')  string  (optional) The type of the list element
*/

Let's take a look at the raw method, for adding "anything" to the list.

Menu::handler('main')->raw('<img src="img/seperator.gif">');

/* The raw method takes these arguments
  $html string  The contents of the item
  $children (default = null)  ItemList  (optional) The children of this item
  $item_attributes (default = array())  array (optional) HTML attributes for the list element (usually <li>)
  $item_element (default = 'li')  string  (optional) The type of the list element
*/

Great! Now that we have learned how to add items to an item list, let's have a look at how we add children to a item. Every item can have children, the children object is just another ItemList. As we have seen before, we can create item lists via the handler method, but this method returns a MenuHandler, making it unusable for item children. So what do we use? the items method returns a fresh ItemList object. Let's have a look.

Menu::handler('main')
    ->add('home', 'Homepage', Menu::items()
        ->add('sub-of-home', 'Sub of homepage'));

/* The items method takes these arguments
  $name (default = null)  string  (optional) The name (=identifier) of this ItemList
  $attributes (default = array()) array (optional) HTML attributes for the ItemList element (usually <ul>)
  $element (default = 'ul') string  (optional) The type of the ItemList element
*/

So now we know how to build menus, add items and items with children. Let's find out how to display the menus. The MenuHandler and ItemList classes implement the "__toString" method, that calls the render method. This means you can simply echo the MenuHandler or ItemList object. Here is an example to make things more clear.

echo Menu::handler('main');

// Is the same as

echo Menu::handler('main')->render();

Now that we have the basics under control, we are going to explore some other cool features this package provides.

Class diagram

Class diagram

Some last words

Thanks for following along and using this package. Special thanks to @Anahkiasen for refactoring this package and boosting new life into it!

menu's People

Contributors

anahkiasen avatar carbontwelve avatar freekmurze avatar markcameron avatar memco avatar msurguy avatar nostalgie avatar nticaric avatar rendom avatar rojtjo avatar tobiasneidig avatar vespakoen avatar yuri-moens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

menu's Issues

Set additional elements for active state

I'd like to set additional elements inside the closing to accomplish something like this output.

<ul>
    <li class="active"><a href="#"><span class="title">Users</span></span><span class="arrow open"></span></a></li>
</ul>

Here is what it looks like when not active

<ul>
    <li><a href="#"><span class="title">Users</span></span><span class="arrow down"></span></a></li>
</ul>

Is that possible and I'm just missing it? If not, do you have a suggestion where I should start looking to modify?

html attributes in child ul

I have this :

            {{
// Render some categories
Menu::handler('categories',array('class' => 'nav'))
    ->add('algorithms', 'Algorithms', Menu::items()->prefix_parents()
        ->add('cryptography', 'Cryptography')
        ->add('data-structures', 'Data Structures')
        ->add('digital-image-processing', 'Digital Image Processing')
        ->add('memory-management', 'Memory Management'), array('class' => 'dropdown-toggle'), array('class' => 'dropdown'))
    ->add('graphics-and-multimedia', 'Graphics & Multimedia', Menu::items()->prefix_parents()
        ->add('directx', 'DirectX')
        ->add('flash', 'Flash')
        ->add('opengl', 'OpenGL'));

}}

Is there a way of adding html attributes to the child ul of the "Algorithms" node? i can add attributes to the

  • tag and the link tag but not to the
      child that holds the child menu.

      I mean this to have twitter bootstrap support.. since the child ul needs to have the "dropdown-menu" class

  • Raw item seen as active on homepage

    It looks like isActive() evaluates to true.

    trim($this->getUrl(), '/') == trim($this->getRequest()->getPathInfo(), '/') or
    

    Which adds an active class to the raw LI element.

    Active State

    Hello vespakoen,
    Can you create a method that give the requested link a class call active?

    Kind Regards
    Abstractmedia

    [Proposal] Small request

    Hi @vespakoen , can u please make $contanier in Menu class protected, because I'm extending your menu, and always after update composer, I should correct this property. And one more request, can u provide version for menu, cause I have big project, and in future I want freeze his.

    P.S. Sorry for my english, hope u understand me :)

    Menu::find() error

    The Menu::find('nav')->add('users/create', 'My company'); it just keeps giving me a errror with Call to a member function add() on a non-object

    Not reading from local config on L4

    Following up on #23. The result from that bug is that you have to edit the config in vendor/vespakoen/menu/src/config/config.php. Not a great idea when you're trying to commit your changes.

    In Laravel you can run php artisan config:publish vespakoen/menu and have it copy the config to your local. You can then override the config with your local stuff. I've done this, but it doesn't seem to be using that local config.

    Getting an error when I try to composer update

    When I try to do a composer update on laravel, I often get an error on the post-update artisan optimize:

    "Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Error: Class 'Menu\/MenuServiceProvider' not found in \/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/ProviderRepository.php line 123"
    

    Any idea what's causing this and how to fix it?

    active_state error

    Hi Vespa !
    I have an unhandled exception when current page is a specified on menu saying index active_class is undefined, on menu.php line 739.

        public function render($options = array())
        {
            $options = array_merge($this->options, $options);
    
            extract($options);
    
            if($this->is_active())
            {
                $item_attributes = merge_attributes($item_attributes, array('class' => $options['active_class']));
            }
    
            if($this->has_active_child())
            {
                $item_attributes = merge_attributes($item_attributes, array('class' => $options['active_child_class']));
            }

    I think this come from a more strict error settings on my php.ini$options['active_class']
    My solution to correct the problem was to add && isset($options['active_class']) in the if statement. (Same thing for active_child_class).

    Let my know if you want a pull request for this.

    Not reading from config on L4

    It seems like it's not using the config file. I've got:

    Menu::handler('main')
        ->add('admin',      'Admin')
        ->add('details',    'Details')
    ;
    
    echo Menu::handler('main');

    While it prints out the menu, it doesn't read anything out of the config. The most immediate one I noticed was that active_class was not being added even though isActive was returning true in Item.php. I tried printing out $this->options in MenuObject after the Menu::getOption() call, but it was empty. I also tried changing the config file name to _config.php and it kept working. I also changed the item_list element from ul to div, and it still rendered as a ul.

    Maybe I'm doing something wrong...?

    Travis

    Just pushed a basic test to master, could you hook the repo up to Travis so that the build status displays correctly ?

    Dependencies and recent changes

    So as you may have seen I went a little ahead and removed the hard coded dependencies in the repo (which is bad anyway) to replace them with Illuminate components that will on their hand handle Request, UrlGenerator and HTML.

    I kind of got a little carried away but then I remembered you had some concerns about dependencies in the past so this was just to reassure you Menu is still framework agnostic and works as a standalone. The only difference is now you don't have to manually initialize the Request class — which is kind of cool.

    Anyway if you're opposed to this I can always revert, just tell me.

    Don't render submenus

    Hi, i updated the library, and now i my code is not working well, it isn't showing the submenu.
    Here is my code:

    Menu::handler('sidebar')
             -> add('/', '<i class="fa fa-home fa-lg"></i> <span class="title">Dashboard</span>', null, array('title' => 'Go to Dashboard'))
             -> add('admin/users', '<i class="fa fa-group fa-lg"></i> <span class="title">Users</span>', null, array('title' => 'Go to Users')) -> activePattern('\/admin\/users\/')
             -> add('admin/sapps', '<i class="fa fa-code fa-lg"></i> <span class="title">Sapplications</span>', null, array('title' => 'Go to Service Applications')) -> activePattern('\/admin\/sapps\/')
             -> add('admin/servers', '<i class="fa fa-hdd-o fa-lg"></i> <span class="title">Servers</span>', null, array('title' => 'Go to Servers')) -> activePattern('\/admin\/servers\/')
             -> add('admin#', '<i class="fa fa-bullseye fa-lg"></i> <span class="title">System</span>', 
                    Menu::items('config', array('class' => 'sub-menu'))
                        -> add('admin/system/dashboard', 'Dashboard', null, array('title' => 'Go to Dashboard')) -> activePattern('\/admin\/system\/dashboard\/')
                        -> add('admin/system/notifications', 'Notifications', null, array('title' => 'Go to Notifications')) -> activePattern('\/admin\/system\/notifications\/')
                        -> add('admin/system/templates', 'Templates', null, array('title' => 'Go to Templates')) -> activePattern('\/admin\/system\/templates\/')
                        -> add('admin/system/logs', 'Logs', null, array('title' => 'Go to Logs')) -> activePattern('\/admin\/system\/logs\/')
                , array('title' => 'Go to System Config')) -> activePattern('\/admin\/system\/');
    
    echo Menu::handler('sidebar')->render();

    We can't get only the root items of a menu.

    example menu

    <?php
    Menu::handler('main')->hydrate(function() {
            return [
                (object)['id' => 1, 'parent_id' => 0, 'slug' => '', 'name' => 'home', 'as' => 'home'],
                (object)['id' => 2, 'parent_id' => 0, 'slug' => 'records', 'name' => 'records', 'as' => 'records'],
                (object)['id' => 3, 'parent_id' => 2, 'slug' => 'music', 'name' => 'music', 'as' => 'music'],
                (object)['id' => 4, 'parent_id' => 3, 'slug' => 'microphone', 'name' => 'music', 'as' => 'microphone'],
            ];
        },
        function($children, $item) {
            $children->add($item->slug, $item->name, Menu::items($item->as));
        }
    );

    When I do that :

    <?php
      echo Menu::handler('main')->setOption('max_depth', 0)->render();

    I get all children.

    When I do :

    <?php
      echo Menu::handler('main')->setOption('max_depth', 1)->render();

    I get the two first depth of the menu.

    max_depth should be set by default with a no usable value (like false, -1, NULL) and be set at 0 to handle only the root children.

    Can your menu system work with this kind of menu?

    I've really been liking your menu system and I've been trying to get it to work with this kind of menu

    <div class="ui menu">
      <a class="active item vt-p">
        <i class="home icon"></i> Home
      </a>
      <a class="item vt-p">
        <i class="mail icon"></i> Messages
      </a>
      <div class="right menu">
        <div class="item">
          <div class="ui icon input">
            <input type="text" placeholder="Search...">
            <i class="search link icon"></i>
          </div>
        </div>
      </div>
    </div>
    

    Your menu system seems like it's only built for unordered lists.

    How to use your package.

    Menu::handler('main')->hydrate(function()
      {
        return Page::with('translation')
          ->where('group', '=', 'main')
          ->get();
      },
      function($children, $item)
      {
        $children->add($item->translation->slug, $item->translation->name, Menu::items($item->as));
      });

    You use some 'translation'? Should I have it too? What table structure should I have and what columns should I surely have?

    No offense, but your docs are bad, although your package good enough for many cases, I suppose.

    Question about rendering menu.

    Ok I've been trying to use the menu system for a little bit now and just decided to ask you now if it can do this. I found this menu system also and I got it working but it isn't flexible enough to add classes https://bitbucket.org/purposemedia/menu/src. I'm trying to use a foreach loop and loop over the menus I tried this in yours.

    $menu = '';
                foreach ($array as $key => $value) {
                    $menu .= Menu::handler('main')->add($value['slug'], $value['name']);
                }
    
                return $menu;
    

    It kind of works but it just doubles the menus up. can it do this like the other menu system? Thanks for the help.

    Question about render performance

    Have you noticed the call to render being particularly slow? It seems to eat up about 30% of the request time. (300 out of 800 ms on localhost, and 80ms out of 260ms on a test server)

    I went to cache the menu after render, but the problem is that doesn't allow the "active" class to be properly applied based on the current route.

    I'm considering caching with a cache key that includes the active top level item name, so although I would have 5 or 6 caches of nearly the same menu markup, it would be much faster, and allow the "active" class to be properly appended.

    Just wanted to see if you noticed any performance issues with the render, and if there were any other solutions to speed it up.

    Render submenu active depth.

    My Menu:

      Menu::handler('main')
        ->add(route('root'), 'Home')
        ->add(route('dealers'), 'Dealers', Menu::items()
            ->add(route('find_dealer'), 'Find Dealer')
        )
        ->add(route('index_tires'), 'Product')
        ->add(route('about', ['page' => 'company]), 'Company', Menu::items()
            ->add(route('about', ['page' => 'company']), 'About Us')
            ->add(route('about', ['page' => 'history']), 'History')
            ->add(route('about', ['page' => 'page3']), 'Page 3')
        );

    I have menus in different part of layout: header and sidebar

    I want render depth(0) (Works perfect) at header and depth(1) (Dont work) at sidebar. But it isnt working.

    Header

     {{ Menu::handler('main')->setOption('max_depth', 0)->render() }}
    

    Sidebar

     {{ Menu::handler('main')->setOption('max_depth', 1)->render() }}
    

    It should return Submenu itens of active MenuItem.

    Keep Parent Menu active

    Hi, is there an easy way already to keep the parent menu active. Like when you're having the following links: /projects and /projects/slug-name

    The projects is in the main navigation, and the other one somewhere else in the subnavigation. How can I easily keep the active-class to the Projects link in the main navigation, when I'm a Project detail page?

    composer install issue - Too many arguments.

    Running laravel on windows 8 with xampp
    and when I try :
    php composer.json install "vespakoen/menu": "dev-master"

    I get:

    {
    "require": {
    "laravel/framework": "4.0.",
    "zizaco/confide": "dev-master",
    "zizaco/entrust": "dev-master",
    "laravelbook/laravel4-powerpack": "dev-master",
    "jasonlewis/basset": "
    ",
    "jasonlewis/expressive-date": "1.*",
    "bigelephant/presenter": "dev-master"
    },
    "autoload": {
    "classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/presenters",
    "app/database/migrations",
    "app/tests/TestCase.php"
    ]
    },
    "minimum-stability": "dev"
    }

    And when I try :
    php composer.phar install "vespakoen/menu": "dev-master"

    I get:

    [RuntimeException]
    Too many arguments.

    Is this issue on my side or has something to do with package/composer?

    Docs

    Hey I'm a few hours into Laravel and think there's a gap in your docs that a newish person can't easily bridge. Everything is super straight forwards "Put this into file A" etc, then when I got to "Basic Usage" it was suddenly like the road ran out :)

    I can't figure out where I should put that chunk of code? In a controller? Am I supposed to create these "children" and "item" arrays? I'll probably just revert to a more manual system for now, but it seems like with a few more details I could use this one which would be swell.

    Error since lasted commit

    Hi,

    since lasted commit, i have error :

    `Error in exception handler: Object of class Menu\MenuHandler could not be converted to string in /home/bas/app_638402a0-56/app/storage/views/b832f800bce91694a35f97c89afd4f67:46

    `

    Render 2nd level in current section

    Hi,

    I am trying to render the 2nd level navigation menu for the current section.

    Menu::handler('main')
        ->add(route('home.index'), 'Home')
        ->add(route('property'), 'Property', Menu::items()
            ->add(route('property.sites.index'), 'Sites')
            ->add(route('property.buildings.index'), 'Buildings')
        )
        ->add(route('finance'), 'Finance', Menu::items()
            ->add(route('finance.invoices.index'), 'Invoices')
            ->add(route('finance.budgets.index'), 'Budgets')
        );

    Using getItemListsAtDepth(1) lists all of the level 2 items regardless of what page I'm on. I want to render Sites and Buildings if I'm inside the Property section.

    Is there any way to do this?

    Many thanks.

    Composer install getting problem

    I am running composer.phar install and getting this problem:

     Problem 1
        - Installation request for anahkiasen/former dev-master -> satisfiable by anahkiasen/former[dev-master].
        - Can only install one of: anahkiasen/html-object[dev-master, 1.1.2].
        - vespakoen/menu dev-master requires anahkiasen/html-object 1.1.2 -> satisfiable by anahkiasen/html-object[1.1.2].
        - anahkiasen/former dev-master requires anahkiasen/html-object dev-master -> satisfiable by anahkiasen/html-object[dev-master].
        - Installation request for vespakoen/menu dev-master -> satisfiable by vespakoen/menu[dev-master].
    

    Register package on Packagist

    Although Menu has a Composer file, it isn't yet available on Packagist with makes it impossible to use on Laravel 4.
    I think the composer branch should be merged into master (since the composer.json must be found in the master branch for Packagist) and the hook be set up on Packagist.

    ItemList always renders as a UL

    No matter what you set item_list.element to in the config, it always renders as a UL. Confirmed by printing out $element in ItemList.php::render() (line 229).

    Ports other than 8000 being cut off URL

    I'm running into an issue when using this package and using Browser Sync.
    Whenever I approach the page via the standard localhost:8000 address, everything seems to work just fine.

    However, when using Browser Sync that runs on port 3002, it seems like the package cuts off the port number, resulting in a 404 error when clicking a link. Is there something I'm missing here?

    No Laravel 4.1 support ? And what about that hydrate thing ?

    I installed laravel 4.1 fresh,
    and i added "vespakoen/menu": "dev master"
    to the composer json
    and this is the result...

    Your requirements could not be resolved to an installable set of packages.

    Problem 1
    - Installation request for vespakoen/menu dev-master -> satisfiable by vespakoen/menu[dev-master].
    - vespakoen/menu dev-master requires anahkiasen/underscore-php dev-master -> no matching package found.

    Potential causes:

    Read http://getcomposer.org/doc/articles/troubleshooting.md for further common problems.

    Furthermore is use this package maybe over a year by now, but the recent changes indeed blaf new users off... as it was first very easy, it seems that you now need to have studied rocket-science to be able to use it. Hydrate whot ???

    $navMenu = Menu::handler('nav', array('class' => 'nav nav-pills pull-left'));

    $navMenu->add('home', 'Home', null, array('class' => 'btn btn-default navbar-btn'))
    ->add('shop', 'Shop', null, array('class' => 'btn btn-default navbar-btn'))
    ->add('about', 'About Us', null, array('class' => 'btn btn-default navbar-btn'))
    ->add('contact', 'Contact', null, array('class' => 'btn btn-default navbar-btn'));

    That was working perfect, so what is all that with that hydrate thing ?
    Can you explain what the hydrate is used for ?

    How do I Start

    Whats the best place to create the menu. I dont want to create it in the controllers classes, I would like it to be in a central location, I guess thats how it is built to work. Would love your opinion.

    Missing parameter options in MenuHandler->render()

    In Readme.md it says that the render()-method takes an array options and even in the function doc header it is mentioned. In fact, the function takes none.

    Is the documentation outdated? But how can I set max_depth then?

    /**
     * Render all the ItemLists this handler acts on and return the HTML
     *
     * @param array $options Optional render settings
     *
     * @return string
     */
    public function render()
    {
      $contents = '';
    
      // Loop through the ItemLists this handler handles
      // And render each one in the content
      foreach ($this->handles as $name) {
        $contents .= Menu::getItemList($name)->render();
      }
    
      return $contents;
    }

    Hydrate error

    I'm getting a error when I use the hydrate function.

    $navMenu = Menu::handler('nav', array('class' => 'nav nav-pills pull-left'));
    
            $navItems = array(
                array('url' => 'home', 'label' => 'Home'),
                array('url' => 'shop', 'label' => 'Shop'),
                array('url' => 'about', 'label' => 'About'),
                array('url' => 'contact', 'label' => 'Contact'),
                array('url' => 'shop/categories', 'label' => 'Categories', 'parent_url' => 'shop')
            );
    
            $navMenu->hydrate($navItems, function($children, $item)
                {
                    $children->add($item['url'], $item['label'], Menu::items($item['url']));
                }, 'url', 'parent_url');
    
            $navMenu->getAllItemsAtDepth(0)
            ->addClass('btn btn-default navbar-btn');
    

    I keep getting this error Undefined index: parent_url

    I copied this from the README.md.

    
    $navMenu = Menu::handler('nav', array('class' => 'nav nav-pills pull-left'));
    
            $navItems = array(
                array('url' => 'home', 'label' => 'Home'),
                array('url' => 'shop', 'label' => 'Shop'),
                array('url' => 'about', 'label' => 'About'),
                array('url' => 'contact', 'label' => 'Contact'),
                array('url' => 'shop/categories', 'label' => 'Categories', 'parent_id' => '1')
            );
    
            Menu::handler('main')->hydrate(function() use ($navItems)
              {
                return $navItems;
              },
              function($children, $item)
              {
                $children->add($item->translation->slug, $item->translation->name, Menu::items($item->as));
              });
    

    Undefined index: parent_id

    I guess I'm doing something wrong? Thanks for your help.

    External Links

    Any easy way of doing external links. I have a menu that has links to sites external partners.
    Thanks

    Adding classes and id's

    -UPDATED WITH EXAMPLE-
    Hi I'm using L3 branch and I would like to know how to add classes and
    id's to the menu and to the items

    This is how I would like it:

    <nav>
    <ul id="#mainmenu" class="menu">
        <li>
              <ul class="submenu">
                   <li></li>
              </ul>
        </li>
    </ul>
    </nav>

    Is this possible?

    So I figured it out. Cool stuff. Hopefully this will open some eyes.

    EXAMPLE CONTROLLER

    class Admin_Controller extends Base_Controller {
    
        public  function menu()
        {
            return  Menu::handler('main')
                    ->raw('<div class="sidebar-toggler hidden-phone"></div>')
                    ->add('admin', '<i class="icon-home"></i><span class="title">Dashboard</span><span class="selected"></span>')
                    ->add('admin/profiles', '<i class="icon-user"></i>Profielen')
                    ->add('admin/vacatures', '<i class="icon-tasks"></i>Vacatures')
                    ->add('admin/pages', '<i class="icon-tasks"></i>Pages');
    
        }
    
        public function action_index()
        {
    
    
            return View::make('admin.index')
                ->with(array(
                    'menu' => $this->menu()
                ));
        }
        public function action_pages()
        {   
    
            return View::make('admin.pages.index')
                        ->with(array(
                    'menu' => $this->menu()
                ));
        }   
    
    }

    AND IN THE VIEW:

    {{ $menu }}

    Escaped links

    On Laravel 3.1.9 using the latest version of Menu, the links are escaped when using the following call:

    {{ Menu::container('admin')->render(array('class' => 'nav'), array('class' => 'link')) }}

    This is from within a blade file. The code generated is this:

    <ul class="nav"><li>&lt;a href=&quot;http://localhost:8888/test-projects/laravel/3.1.9/public/index.php/test/test&quot; class=&quot;link&quot;&gt;Test&lt;/a&gt;</li></ul>

    It seems Laravel's internal HTML::ul generator calls the entities method on what it inserts into a list item (line 320 of laravel/html.php). I do not know if this is something that should reported to Laravel core, or here, but I thought you would be interested in case this ends up breaking code for other menu users.

    Menu Roadmap

    Menu roadmap

    • Add Link and Raw classes to better handle content
    • Finish code clean-up
    • Rename repository to just menu as the package's been agnostic for a while
    • Allow chaining of attributes and classes

    Question: adding another set of item list.

    Hi,

    I would like to know how to add a new item list to the previously created item lists?
    For ex:

    Package 1
    -has admin sub menu item lists

    Package 2
    -add a new item list to package 1's admin sub menu items lists.

    Been trying all stuff the entire day with no success.
    Thanks!

    FatalErrorException

    I started to get the error below after composer update:

    Symfony \ Component \ Debug \ Exception \ FatalErrorException

    Unsupported operand types
    open: /srv/www/gbt.se/vendor/vespakoen/menu/src/Menu/Items/ItemList.php

    // Check for maximal depth
    $maxDepth = $this->getOption('max_depth');
    if ($maxDepth != 0 and $depth > $maxDepth) return false;
    // Render contained items
    $contents = null;
    foreach ($this->children as $item) {
    $contents .= $item->render($depth + 1);
    }
    

    Active class problem

    I have a problem
    http://grab.by/iUAc - all good, active class generated
    http://grab.by/iUAu - wery bad,

  • haven't class
    If a category one, but it has a lot of articles, this is problematic, the whole hierarchy to stick to the menu. We have to do that it is defined by the top slashes, and all that goes well, made ​​active.

  • Error since lasted commit

    Hi,

    since lasted commit, i have error :

    Error in exception handler: Object of class Menu\MenuHandler could not be converted to string in 
    /home/bas/app_638402a0-56/app/storage/views/b832f800bce91694a35f97c89afd4f67:46
    
    `<?php echo Menu::handler('menuNoAuth'); ?>`
    

    Class 'Menu\MenuServiceProvider' not found

    I have followed the instructions in README.md but I am unable to use this menu system, as I am greeted with " Class 'Menu\MenuServiceProvider' not found ".

    Symfony\Component\Debug\Exception\FatalErrorException
    …/­vendor/­laravel/­framework/­src/­Illuminate/­Foundation/­ProviderRepository.php123

    The return line is line 123.

        public function createProvider(Application $app, $provider)
        {
            return new $provider($app);
        }

    Question: addClass to anchor tag

    Hi,
    I'm trying to get this working with a bootstrap template but i cannot get one of the menu items to display correct.

    I'm using a manually filled menu list for now and so far i've got the following code (placed inline in my view, so probably not the right location for this logic but yeah).

    Menu::handler('main')->addClass('nav nav-list');
    Menu::handler('main')->getItemsAtDepth(0)->map(function($item) { if($item->hasChildren()) { $item->addClass('dropdown-toggle'); } });
    Menu::handler('main')->getItemListsAtDepth(1)->setElement('ul')->addClass('submenu');
    

    This outputs the following:

    <ul class="nav nav-list">
      <li>
        <a href="#">Home</a>
      </li>
      <li class="dropdown-toggle active-child">
        <a href="#">Child test</a>
          <ul class="submenu">
            <li>
              <a href="#">Child 1</a>
            </li>
            <li class="active">
              <a href="#">Child 2</a>
          </li>
        </ul>
      </li>
    </ul>
    

    Now the problem i'm having is that i need the 'dropdown-toggle' class to be added to the 'Child test' anchor tag and not to the 'li' element.

    Can this be done without using raw?

    Adding class to main ul

    For a bootstrap template, I need to add the nav navbar-nav classes to the UL of the menu. But how to do that?

     <ul class="nav navbar-nav">
                <li class="active"><a href="/home">Home</a></li>
                <li><a href="/contact">Contact</a></li>
    </ul>
    

    This doesn't work for example:
    {{ Menu::handler('siteMenu', array('class'=>'nav navbar-nav'), 'ul') }}

    Requirements could not be resolved to an installable set of packages

    I get this when i was trying to install the package.

    PS d:\xxx\yyy> composer update
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - Conclusion: remove laravel/framework 4.0.x-dev
        - Conclusion: don't install laravel/framework 4.0.x-dev
        - Conclusion: don't install laravel/framework v4.0.10
        - Conclusion: don't install laravel/framework v4.0.9
        - Conclusion: don't install laravel/framework v4.0.8
        - Conclusion: don't install laravel/framework v4.0.7
        - Conclusion: don't install laravel/framework v4.0.6
        - Conclusion: don't install laravel/framework v4.0.5
        - Conclusion: don't install laravel/framework v4.0.4
        - Conclusion: don't install laravel/framework v4.0.3
        - Conclusion: don't install laravel/framework v4.0.2
        - Conclusion: don't install laravel/framework v4.0.1
        - Conclusion: don't install laravel/framework v4.0.0
        - Installation request for vespakoen/menu dev-master -> satisfiable by vespakoen/menu[dev-master].
        - Conclusion: don't install laravel/framework v4.0.0-BETA4
        - Conclusion: don't install laravel/framework v4.0.0-BETA3
        - vespakoen/menu dev-master requires orchestra/testbench 2.1.* -> satisfiable by orchestra/testbench[2.1.x-dev, v2.1
    .0].
        - orchestra/testbench 2.1.x-dev requires laravel/framework 4.1.* -> satisfiable by laravel/framework[4.1.x-dev, v4.1
    .0, v4.1.1, v4.1.10, v4.1.11, v4.1.2, v4.1.3, v4.1.4, v4.1.5, v4.1.6, v4.1.7, v4.1.8, v4.1.9].
        - orchestra/testbench v2.1.0 requires laravel/framework 4.1.* -> satisfiable by laravel/framework[4.1.x-dev, v4.1.0,
     v4.1.1, v4.1.10, v4.1.11, v4.1.2, v4.1.3, v4.1.4, v4.1.5, v4.1.6, v4.1.7, v4.1.8, v4.1.9].
        - Can only install one of: laravel/framework[v4.0.0-BETA2, 4.1.x-dev].
        - Can only install one of: laravel/framework[v4.1.0, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.1, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.10, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.11, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.2, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.3, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.4, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.5, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.6, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.7, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.8, v4.0.0-BETA2].
        - Can only install one of: laravel/framework[v4.1.9, v4.0.0-BETA2].
        - Installation request for laravel/framework 4.0.* -> satisfiable by laravel/framework[4.0.x-dev, v4.0.0, v4.0.0-BET
    A2, v4.0.0-BETA3, v4.0.0-BETA4, v4.0.1, v4.0.10, v4.0.2, v4.0.3, v4.0.4, v4.0.5, v4.0.6, v4.0.7, v4.0.8, v4.0.9].
    

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.