PHP code example of torifat / cake-menu_builder

1. Go to this page and download the library: Download torifat/cake-menu_builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

torifat / cake-menu_builder example snippets


    ...
    CakePlugin::load('MenuBuilder');
    

    ...
    CakePlugin::loadAll();
    

    ...
    var $helpers = array(..., 'MenuBuilder.MenuBuilder');

    function beforeFilter() {
        ...
        // Define your menu
        $menu = array(
            'main-menu' => array(
                array(
                    'title' => 'Home',
                    'url' => array('controller' => 'pages', 'action' => 'home'),
                ),
                array(
                    'title' => 'About Us',
                    'url' => '/pages/about-us',
                ),
            ),
            'left-menu' => array(
                array(
                    'title' => 'Item 1',
                    'url' => array('controller' => 'items', 'action' => 'view', 1),
                    'children' => array(
                        array(
                            'title' => 'Item 3',
                            'url' => array('controller' => 'items', 'action' => 'view', 3),
                        ),
                        array(
                            'title' => 'Item 4',
                            'url' => array('controller' => 'items', 'action' => 'view', 4),
                        ),
                    )
                ),
                array(
                    'title' => 'Item 2',
                    'url' => array('controller' => 'items', 'action' => 'view', 2),
                ),
            ),
        );

        // For default settings name must be menu
        $this->set(compact('menu'));
        ...
    }
    

        echo $this->MenuBuilder->build('main-menu');
    

        echo $this->MenuBuilder->build('left-menu');
    

        echo $this->MenuBuilder->build('main-menu', array('class' => array('fun', 'red'));
        // OR
        echo $this->MenuBuilder->build('main-menu', array('class' => 'fun green');
    

    ...
    var $helpers = array(
        ...
        'MenuBuilder.MenuBuilder' => array(/* array of settings */)
    );
    

    ...
    function beforeFilter() {
        ...
        $user = $this->Auth->user();
        $this->set(compact('user'));
    }
    

    ...
    function beforeFilter() {
        ...
        $user = Authsome::get();
        $this->set(compact('user'));
    }
    

    ...
    function beforeFilter() {
        ...
        // Define your menu
        $menu = array(
            'main-menu' => array(
                // Anybody can see this
                array(
                    'title' => 'Home',
                    'url' => array('controller' => 'pages', 'action' => 'home'),
                ),
                // Users and Admins can see this, Guests and Managers can't
                array(
                    'title' => 'About Us',
                    'url' => array('controller' => 'pages', 'action' => 'about-us'),
                    'permissions' => array('user','admin'),
                ),
                // Only Guests can see this
                array(
                    'title' => 'Login',
                    'url' => array('controller' => 'users', 'action' => 'login'),
                    'permissions' => array(''),
                ),
            ),
            ...
        );
        // For default settings name must be menu
        $this->set(compact('menu'));
        ...
    }
    

    ...
    var $helpers = array(..., 'MenuBuilder.MenuBuilder');

    function beforeFilter() {
        ...
        // Define your menu
        $menu = array(
            'main-menu' => array(
                array(
                    'separator' => '<dt>Main Menu</dt>',
                ),
                array(
                    'title' => 'Home',
                    'url' => array('controller' => 'pages', 'action' => 'home'),
                ),
                array(
                    'title' => 'About Us',
                    'url' => '/pages/about-us',
                ),
            )
        );

        // For default settings name must be menu
        $this->set(compact('menu'));
        ...
    }