KnpMenuBuilder Menu with FontAwesome icons

1.1k views Asked by At

I try to create a menu builder in php, to reproduce this code :

         <ul class="nav nav-pills">
            <li class="first"><a title="Voir panier"
                href="{{ path('cs_web_homepage') }}"> <i
                    class="fa fa-shopping-cart icon-large"></i> Voir Panier
            </a></li>

            {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %} 
                <li><a title="Mon compte"
                    href="{{ path('fos_user_profile_show') }}"><i
                        class="fa fa-user icon-large"></i> Mon compte
                </a></li>
                <li><a title="Déconnexion"
                    href="{{ path('fos_user_security_logout') }}"><i
                        class="fa fa-power-off icon-large"></i> Déconnexion
                </a></li>
            {% else %}
                <li><a title="Connexion"
                    href="{{ path('fos_user_security_login') }}"><i
                        class="fa fa-lock icon-large"></i> Connexion
                </a></li>
                <li class="last"><a title="Register"
                    href="{{ path('fos_user_registration_register') }}"> <i
                        class="fa fa-user icon-large"></i> Inscription
                </a>
                </li>
            {% endif %}
        </ul>

So, I begin with this code :

public function mainMenu(FactoryInterface $factory, array $options)
{
    $request = $this->container->get('request');
    $securityContext = $this->container->get('security.context');

    $menu = $factory->createItem('root', array(
        'childrenAttributes' => array(
            'class' => 'nav nav-pills'
        )
    ));

    $menu->addChild('cart', array(
        'route' => 'cultural_store_web_homepage',
        'linkAttributes' => array('title' => 'Voir Panier'),
        'labelAttributes' => array('icon' => 'icon-user')))->setLabel('Voir Panier');

    return $menu;

}

But, the icon is not added. I also try to put :

$menu->addChild('cart', array(
        'route' => 'cs_web_homepage',
        'linkAttributes' => array('title' => 'Voir Panier')))
        ->setLabel('Voir Panier')
        ->addChild('icon' => array('class' => 'fa fa-shopping-cart');

But it doesn't work... I don't really understand how to use the icon child..

Any help ?

Thx

1

There are 1 answers

0
s1x On BEST ANSWER

A similiar question with your approach was already posted. You could transform the code for you're purpose. See: KnpMenuBundle - send Options and use them in Twig