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
