2017 © Pedro Peláez
 

library menu-generator

Simple Menu Generator

image

kebir/menu-generator

Simple Menu Generator

  • Tuesday, May 31, 2016
  • by Kebir
  • Repository
  • 1 Watchers
  • 2 Stars
  • 68 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Menu Generator

Installations

Install through composer:, (*2)

{
    require: {
        "kebir/menu-generator": "dev-master"
    }
}

Usage

<?php

//Given this list of menus
$menus = [
    ["id" => 1, "name" => "Menu 1", "url" => "test.com", "parent_id" => 3],
    ["id" => 2, "name" => "Menu 2", "url" => "test.com", "parent_id" => 0],
    ["id" => 3, "name" => "Menu 3", "url" => "test.com", "parent_id" => 0],
    ["id" => 4, "name" => "Menu 4", "url" => "test.com", "parent_id" => 2],
    ["id" => 5, "name" => "Menu 5", "url" => "test.com", "parent_id" => 3],
    ["id" => 6, "name" => "Menu 6", "url" => "test.com", "parent_id" => 1],
];

//Let's build a hierarchical menus list
$builder = new Kebir\MenuGenerator\Builder();
$menus_generated = $builder->build($menus);

//Check the output
foreach ($menus as $menu) {
    echo $menu->getName() . "-" . $menu->getUrl()."\n";
    foreach ($menu->getElements() as $submenu) {
        echo "--- ".$submenu->getName()."\n";
    }
}
/** output
    Menu 2 - test.com
        --- Menu 4
    Menu 3 - test.com
        --- Menu 1
        --- Menu 5
*/

If you want to display the menu, the package provides a HtmlListRenderer class that will display the menus using html <ul> and <li> tags. The class requires an instance of Kebir\MenuGenerator\Selector\Selector which is responsible of detecting if a menu should considered as selected., (*3)

<?php

$current_url = 'http://test.com/page1';
$simple_selector = new Kebir\MenuGenerator\Selector\SimpleUrlSelector($current_url);
$renderer = new Kebir\MenuGenerator\Renderer\HtmlListRenderer($simple_selector);
echo "

    "; foreach ($menus as $menu) { $renderer->render($menu); } echo "
";

Laravel Users

The package includes a Service Provider and a Facade for the Renderer. Edit the app/config/app.php:, (*4)

<?php

  //Add the service provider
  'Kebir\MenuGenerator\MenuGeneratorServiceProvider'
  ...
  //add the facade alias
  'MenuRenderer'    => 'Kebir\MenuGenerator\Facades\Renderer'

To use it, simply call the following in your blade template for example:, (*5)

{{ MenuRender::render($menu) }}

To mark a menu as selected, you can use the Kebir\MenuGenerator\Selector\SimpleUrlSelector class when creating the menu renderer., (*6)

For laravel users, the service provider is configured to include the Kebir\MenuGenerator\Selector\LaravelSelector instead. This will allow you, in the config file provided, to define which entry in the menu should be selected when a page is reached., (*7)

This is very useful when the current page is not in the menu but is related to another url which is in the menu. Here is an example of usage for the config/config.php file, (*8)

<?php
return array(
   //To link /users/1/edit to another url /users in the menu.
   'linked_urls' => array(
        '/users/1/edit' => '/users'
    ),

    //To link an action to an url
    'linked_actions' => array(
        'UsersController@edit' => '/users'
    ),

    //To link a route to an url
    'linked_routes' => array(
        'user_edit_path' => '/users'
    )
);

The Versions

31/05 2016

dev-master

9999999-dev

Simple Menu Generator

  Sources   Download

MIT

The Development Requires

by Mohamed El Kebir Ba

31/05 2016

1.3.2

1.3.2.0

Simple Menu Generator

  Sources   Download

MIT

The Development Requires

by Mohamed El Kebir Ba

27/05 2016

1.3.1

1.3.1.0

Simple Menu Generator

  Sources   Download

MIT

The Development Requires

by Mohamed El Kebir Ba

19/04 2016

1.3.0

1.3.0.0

Simple Menu Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mohamed El Kebir Ba

07/03 2015

1.2

1.2.0.0

Simple Menu Generator

  Sources   Download

MIT

The Development Requires

by Mohamed El Kebir Ba

30/07 2014

1.1

1.1.0.0

Simple Menu Generator

  Sources   Download

MIT

The Development Requires

by Mohamed El Kebir Ba

10/07 2014

1.0.1

1.0.1.0

Simple Menu Generator

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Mohamed El Kebir Ba

05/07 2014

1.0

1.0.0.0

Simple Menu Generator

  Sources   Download

The Requires

  • php >=5.4

 

The Development Requires

by Mohamed El Kebir Ba