PHP code example of dama / menu-bundle

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

    

dama / menu-bundle example snippets


class MainMenuTreeBuilder implements MenuTreeBuilderInterface
{
    public function buildTree(Node $root)
    {
        $root
            ->child('social_media')
                ->setAttr('id', 'main_menu_social_media')
                ->setRequiredPermissions(['ROLE_SOCIAL_MENU'])
                ->child('stream')
                    ->setRoute('social_media_stream')
                    ->setRequiredPermissions(['ROLE_SOCIAL_STREAM'])
                    ->setAdditionalActiveRequestMatcher(static function (Request $request): bool {
                        // additionally will mark node as active if the request path starts with '/foo_bar'
                        return str_starts_with($request->getPathInfo(), '/foo_bar');
                    })
                ->end()
                ->child('update_status')
                    ->setRoute('social_media_update_status')
                    ->setRequiredPermissions(['ROLE_SOCIAL_UPDATE_STATUS'])
                ->end()
                ->ifTrue($someCondition) // only add child node(s) inside if the condition is true
                    ->child('statistics')
                        ->setRoute('social_media_statistics')
                        ->setRequiredPermissions([new Expression("has_role('ROLE_USER')")])
                    ->end()
                ->endIf()
            ->end()
        ;
    }
}