KnpMenuBundle: Check if isGranted

Discuss Symfony 2 bundles here

Moderators: dcobalt, tiagojsag

KnpMenuBundle: Check if isGranted

Postby mjnet » Sun Aug 19, 2012 8:34 pm

Hello!

I try to add a menu item depending if the user is logged in or not. I simply want to add a "logout" button.

Code: Select all
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;

class MenuBuilder implements ContainerAwareInterface {

   private $factory;
   
   private $container;
   
   /**
    * @param ContainerInterface $container
    */
   public function setContainer(ContainerInterface $container = null) {
      $this->container = $container;
   }

   /**
    * @param FactoryInterface $factory
    */
   public function __construct(FactoryInterface $factory) {
      $this->factory = $factory;
      
   }

   public function createMainMenu(Request $request) {
      
      $menu = $this->factory->createItem('root');

      $menu->addChild('Home', array('route' => 'home'));
      $menu->addChild('Profil', array('route' => 'fos_user_profile_show'));
      $menu->addChild('Registrieren',   array('route' => 'fos_user_registration_register'));

      if( $container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
         $menu->addChild('Logout', array('route' => 'fos_user_security_logout'));
      } else {
         $menu->addChild('Login', array('route' => 'fos_user_security_login'));
      }
      
      return $menu;
   }

}


Here I get: Fatal error: Call to a member function get() on a non-object
I guess this occures due to the constructor?

When I add $this->container = $container; to the __construct() I get: An exception has been thrown during the rendering of a template ("Notice: Undefined variable: container in ...
I guess because of using not the $container from containerawareinterface right?

So quiet simple but I don't get it :(

Thanks! mjnet
mjnet
Junior Member
 
Posts: 28
Joined: Tue Jan 03, 2012 11:59 pm

Re: KnpMenuBundle: Check if isGranted

Postby ChristopheL » Sun Aug 19, 2012 10:28 pm

Hello,

It looks like you are trying to define your menu as a service as described at
https://github.com/KnpLabs/KnpMenuBundl ... service.md
and that you want additionaly inject the container in your service.

so if you add
Code: Select all
      calls:
            - [ setContainer, [ @service_container ] ]

to your menu builder service, it should inject the container as you expect.

Anyway, if you use only the security_context in your menu builder, it would be better to inject only the security_context in the constructor rather than injecting the whole container.

Best regards,
Christophe
ChristopheL
Junior Member
 
Posts: 27
Joined: Tue Nov 22, 2011 9:55 pm

Re: KnpMenuBundle: Check if isGranted

Postby mjnet » Mon Aug 20, 2012 12:19 am

Hi Christophe,

Thanks for the help!
Yes you're right, I use menu as a service. I'm very new to service oriented programming but looks like i got it!
I followed your words and just use security.context here. So you told me to use "calls"... no success with this. But with "arguments" it'll work. That's good as well?

Code: Select all
services:
    likeme.menu_builder:
        class: Likeme\SystemBundle\Menu\MenuBuilder
        arguments: ["@knp_menu.factory", '@security.context']


Just for documentation, construct looks now like this.
Code: Select all
     protected $securityContext;
   
   /**
    * @param FactoryInterface $factory
    */
   public function __construct(FactoryInterface $factory, SecurityContextInterface $securityContext) {
      $this->factory = $factory;
      $this->securityContext = $securityContext;
   }
mjnet
Junior Member
 
Posts: 28
Joined: Tue Jan 03, 2012 11:59 pm

Re: KnpMenuBundle: Check if isGranted

Postby ChristopheL » Mon Aug 20, 2012 7:15 pm

Hello,

Yes, it's fine like that, mandatory dependencies should be injected through the constructor.

the use of 'call' is necessary only if you want to inject using a setter (like in your first code sample with the setContainer).

Best regards,
Christophe
ChristopheL
Junior Member
 
Posts: 27
Joined: Tue Nov 22, 2011 9:55 pm

Re: KnpMenuBundle: Check if isGranted

Postby mjnet » Mon Aug 20, 2012 7:43 pm

Thanks for explanation and help Christophe!
mjnet
Junior Member
 
Posts: 28
Joined: Tue Jan 03, 2012 11:59 pm


Return to Bundles discussion

Who is online

Users browsing this forum: No registered users and 2 guests