PHP code example of rybakit / navigation-bundle

1. Go to this page and download the library: Download rybakit/navigation-bundle 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/ */

    

rybakit / navigation-bundle example snippets


public function registerBundles()
{
    $bundles = [
        ...
        new Rybakit\Bundle\RybakitNavigationBundle(),
    ];

    ...
}
 php


use Rybakit\Bundle\NavigationBundle\Navigation\Item;

$root = new Item('root');
$child = new Item('child');
$root->add($child);

$parent = $child->getParent(); // $root
$has = $root->has($child); // true
$root->remove($child);
 php


use Rybakit\Bundle\NavigationBundle\Navigation\ItemFactory;
use Rybakit\Bundle\NavigationBundle\Navigation\Filter\BindFilter;

...

$array = [
    'label'    => 'root',
    'children' => [
        ['label' => 'Item 1.1'],
        ['label' => 'Item 1.2', 'children' => [['label' => 'Item 1.2.1']]],
        ['label' => 'Item 1.3'],
    ],
];

$factory = new ItemFactory(new BindFilter());
$root = $factory->create($array);
 php
// Acme/DemoBundle/Navigation/NavigationBuilder.php


namespace Acme\DemoBundle\Navigation;

use Rybakit\Bundle\NavigationBundle\Navigation\ItemFactory;
use Rybakit\Bundle\NavigationBundle\Navigation\Filter\BindFilter;
use Rybakit\Bundle\NavigationBundle\Navigation\Filter\FilterChain;
use Rybakit\Bundle\NavigationBundle\Navigation\Filter\MatchFilter;
use Rybakit\Bundle\NavigationBundle\Navigation\Filter\Matcher\RoutesMatcher;

class NavigationBuilder
{
    ...

    public function createNavigation()
    {
        $request = $this->requestStack->getMasterRequest();
        $route = $request->attributes->get('_route');
        $routeParams = $this->request->attributes->get('_route_params', []);

        $filter = new FilterChain([
            $matchFilter = new MatchFilter(new RoutesMatcher($route, $routeParams)),
            new BindFilter(),
        ]);

        $factory = new ItemFactory($filter);
        $root = $factory->create([
            'label'     => 'acme_demo.home',
            'route'     => 'acme_demo_home',
            'children'  => [
                [
                    'label'  => 'acme_demo.user_new',
                    'route'  => 'acme_demo_user_new',
                    'routes' => ['acme_demo_user_create'],
                ],
            ],
        ]);

        if (!$current = $matchFilter->getMatched()) {
            $current = $root;
        }
        $current->setActive();

        return ['root' => $root, 'current' => $current];
    }
}