bmcclure / CakePHP-Menu-Plugin

A CakePHP 2.0 plugin designed to help build menus in the controller and display them in the view
MIT License
17 stars 9 forks source link

Tutorial: How to install the CakePHP-Menu #16

Open Dreamwatcher opened 11 years ago

Dreamwatcher commented 11 years ago

The readme is too old to use them for installing this CakePHP-Menu. After hours of reading threads in the internet and reading the classes to install this Plugin correctly, i got it to work.

For beginners it is hard to understand what things are to do... Here a little tutorial:

  1. Download the newest package: https://github.com/bmcclure/CakePHP-Menu-Plugin/archive/cake-2.0.zip ATTENTION: Not the old one which was linked in the readme!
  2. Copy the whole folder "CakePHP-Menu-Plugin-cake-2.0" to your plugin directory (by default e.g. app/Plugin)
  3. Rename the plugin folder to "Menu"
  4. if you dont load all your plugins at once, you must load it in your bootstrap. write the following in your bootstrap (app/Config/bootstrap.php by default)
CakePlugin::load('Menu', array('bootstrap' => true));
  1. go to your AppController (by default app/Controller/AppController.php) and extend your components-variable or init the components variable:
public $components = array('Menu.MenuBuilder');

So my components-array is now:

public $components = array('DebugKit.Toolbar', 'Session', 'Menu.MenuBuilder');
  1. create a new function in your AppController for creating the menu array (like this array in the readme. this works) and add the view-helper at the beginning:
private function build_menu()
    {
        $this->helpers[] = 'Menu.MenuRenderer'; //Add the MenuHelper
                //Create the menu-Array
        $menus = array(
            array(
                'title' => 'User',
                'url' => array('controller' => 'users', 'action' => 'index'),
                'children' => array(
                    array(
                        'title' => 'User overview',
                        'url' => array('controller' => 'users', 'action' => 'index'),
                    ),
                    array(
                        'title' => 'New User',
                        'url' => array('controller' => 'users', 'action' => 'add'),
                    ),
                ),
            ),
            array(
                'title' => 'Roles',
                'url' => array('controller' => 'roles', 'action' => 'index'),
                'children' => array(
                    array(
                        'title' => 'Role overview',
                        'url' => array('controller' => 'roles', 'action' => 'index'),
                    ),
                    array(
                        'title' => 'New Role',
                        'url' => array('controller' => 'roles', 'action' => 'add'),
                    ),
                    array(
                        'title' => 'Show Role-Restrictions',
                        'url' => array('controller' => 'role_access_restrictions', 'action' => 'index'),
                    ),
                    array(
                        'title' => 'New Role-Restrictions',
                        'url' => array('controller' => 'role_access_restrictions', 'action' => 'add'),
                    ),

                ),
            ),
    );
    $this->MenuBuilder->setMenu('main-menu',$menus); //Now you set the name here! not in the array
    // For default settings name must be menus
//ATTENTION: default is now "menus", not "menu"
    $this->set('menus',$this->MenuBuilder->getMenu('main-menu')); //To have the menu in your View
    }

Use your new function in beforeFilter:

    public function beforeFilter()
    {
        $this->build_menu();
    }
  1. Render the menu in your view (e.g. default.ctp)
<?php echo $this->MenuRenderer->render('main-menu'); ?>

Ready! now your menu were built ;-)

th-gauweiler commented 11 years ago

Thanks a lot!

giraam commented 10 years ago

Thanks for this man! I generated my menu array from the database, followed the tutorial step by step and the line

$this->MenuBuilder->setMenu('main-menu', $menus);

threw the error

Fatal error: Class 'MenuLib\Menu' not found in C:\xampp\htdocs\CakeProyect\app\Plugin\Menu\Controller\Component\MenuBuilderComponent.php on line 77

The only difference between the example menu array and mine is the property permissions I'm using CakePHP 2.4

I will greatly appreciate any help!

Dreamwatcher commented 10 years ago

It looks like you dont have the file "/app/Plugin/Menu/Lib/MenuLib/Menu.php". Please look for the path and correct your installation. Can you post a sample array?

giraam commented 10 years ago

First, thanks for your quick response.

I checked the existence of the file and the file exists.

This is the array I generated:

array (
    array (
        'title' => 'Usuarios',
        'url' => array ( 'controller' => 'Usuarios', 'action' => 'index', ),
        'permissions' => array ( 'admin', 'ejecutivo de cuentas', ),
        'children' => array (
            array (
                'title' => 'Tipos de Usuario',
                'url' => array ( 'controller' => 'TiposUsuarios', 'action' => 'index', ),
                'permissions' => array ( 'admin', ),
                'children' => array (
                    array (
                        'title' => 'Modulos',
                        'url' => array ( 'controller' => 'Modulos', 'action' => 'index', ),
                        'permissions' => array ( 'admin', ),
                    ),
                ),
            ),
            array (
                'title' => 'Equipos',
                'url' => array ( 'controller' => 'Equipos', 'action' => 'index', ),
                'permissions' => array ( 'admin', ),
            ),
        ),
    ),
)
bmcclure commented 10 years ago

@giraam What version of PHP is running on your server?

This plugin would require at least PHP 5.3 I believe, in order to support the functionality of the class library and its namespaces.

giraam commented 10 years ago

@bmcclure I thought about the PHP version too since I have PHP 5.5 in my notebook so I tried with my office workstation where I use PHP 5.4 and I've had the exact same problem. I'll be honest, I have not delved much into the problem because I was in a hurry at the time but I'll be more than happy to test it again debugging the code. At first glance it should have worked.

bmcclure commented 10 years ago

Are you sure that you have put the following line in your Config/bootstrap.php file?

CakePlugin::load('Menu', array('bootstrap' => true));

If the plugin's bootstrap.php file isn't being loaded (via "'bootstrap' => true" above), these errors might result. That file registers an autoloader for the class library.

Even if you use CakePlugin::loadAll, you would still need to add an array key in there for Menu telling it to load it's bootstrap file.

giraam commented 10 years ago

@bmcclure not sure I did it. I will go back into my repository, test it again and I'll let you know.

giraam commented 10 years ago

@bmcclure indeed that was the problem!

"Even if you use CakePlugin::loadAll, you would still need to add an array key in there for Menu telling it to load it's bootstrap file."

I assumed wrong that CakePlugin::loadAll would do the trick. Thanks for your help and your time! It works as expected by adding that line.