PHP code example of popphp / pop-nav

1. Go to this page and download the library: Download popphp/pop-nav 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/ */

    

popphp / pop-nav example snippets


$tree = [
    [
        'name'     => 'Users',
        'href'     => '/users',
        'children' => [
            [
                'name' => 'Roles',
                'href' => 'roles'
            ],
            [
                'name' => 'Config',
                'href' => 'config'
            ]
        ]
    ],
    [
        'name' => 'Orders',
        'href' => '/orders'
    ]
];

$nav = new Nav($tree);
echo $nav;

$tree = [
    ['name' => 'Docs',    'href' => 'https://docs.example.com'],
    ['name' => 'Support', 'href' => 'mailto:[email protected]'],
    ['name' => 'Top',     'href' => '#top']
];

$tree = [
    [
        'name'       => 'Dashboard',
        'href'       => '/dashboard',
        'attributes' => ['class' => 'icon-dashboard', 'data-tooltip' => 'Go to dashboard']
    ]
];

$nav = new Nav($tree);
$nav->returnFalse(true);

// Append a new top-level branch (pass true as the 2nd argument to prepend it instead)
$nav->addBranch([
    'name' => 'Orders',
    'href' => '/orders'
]);

// Insert a leaf into an existing branch, found by matching 'name'
$nav->addLeaf('Users', [
    'name' => 'Permissions',
    'href' => 'permissions'
]);

// Only match a 'Users' branch at the top level (depth 0)
$nav->addLeaf('Users', ['name' => 'Permissions', 'href' => 'permissions'], 0);

$config = [
    'top' => [
        'node'  => 'nav',
        'id'    => 'main-nav'
    ],
    'parent' => [
        'node'  => 'nav',
        'id'    => 'nav',
        'class' => 'level'
    ],
    'child' => [
        'node'  => 'nav',
        'id'    => 'menu',
        'class' => 'item'
    ],
    'on'      => 'link-on',
    'off'     => 'link-off',
    'indent'  => '    ',
    'baseUrl' => '/app'
];

$config = [
    'on'         => 'link-on',
    'off'        => 'link-off',
    'currentUrl' => '/pages'
];

use Pop\Nav\Nav;

$nav = new Nav($tree, $config);
echo $nav;

use Pop\Acl\Acl;
use Pop\Acl\AclRole as Role;
use Pop\Acl\AclResource as Resource;

$acl = new Acl();

$admin  = new Role('admin');
$editor = new Role('editor');

$acl->addRoles([$admin, $editor]);

$acl->addResource(new Resource('config'));
$acl->allow('admin');
$acl->deny('editor', 'config');

$tree = [
    [
        'name'     => 'Home',
        'href'     => '/home',
        'children' => [
            [
                'name' => 'Users',
                'href' => 'users'
            ],
            [
                'name' => 'Config',
                'href' => 'config',
                'acl'  => [
                    'resource' => 'config'
                ]
            ]
        ]
    ],
    [
        'name' => 'Orders',
        'href' => '/orders'
    ]
];

$nav = new Nav($tree);
$nav->setAcl($acl);
$nav->setRole($editor);
echo $nav;

$nav = new Nav($tree);
$nav->setAcl($acl);
$nav->setRole($admin);
echo $nav;

$acl->addResource(new Resource('reports'));
// No allow() or deny() call for 'reports' at all.

$tree[0]['children'][] = [
    'name' => 'Reports',
    'href' => 'reports',
    'acl'  => ['resource' => 'reports']
];

$nav = new Nav($tree);
$nav->setAcl($acl);
$nav->setRole($editor);
echo $nav; // 'Reports' renders - nothing denied it, so it's allowed by default

$nav = new Nav($tree);
$nav->setAcl($acl);
$nav->setRole($editor);
$nav->setAclStrict(true);
echo $nav; // 'Reports' is now hidden - nothing explicitly allowed it

  $nav->setAcl($acl);
  $nav->addRoles([$editor, $admin]); // 'config' is still hidden - editor's deny wins
  echo $nav;
  

use Pop\Acl\AclRole as Role;
use Pop\Acl\Policy\PolicyTrait;

class OwnerPolicy extends Role
{
    use PolicyTrait;

    // Method name must match the node's 'acl.permission'
    public function edit($role, $resource = null): bool
    {
        return $this->isOwnerOfCurrentPage(); // your own app-specific logic
    }
}

$tree = [
    [
        'name' => 'Edit Page',
        'href' => 'edit',
        'acl'  => [
            'resource'   => 'page',
            'permission' => 'edit',
            'policy'     => fn() => new OwnerPolicy('owner'),
        ]
    ]
];