-
-
Save Invis1ble/503db8bb9ff6dafc52c8b2bf7c6fec42 to your computer and use it in GitHub Desktop.
{% extends '::base.html.twig' %} | |
{% block title %}My Awesome Page!{% endblock %} | |
{% block stylesheets %} | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"> | |
{% endblock %} | |
{% block body %} | |
<div class="navbar navbar-default navbar-fixed-top"> | |
<div class="container-fluid"> | |
{# Brand and toggle get grouped for better mobile display #} | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#admin-navbar-collapse-1"> | |
<span class="sr-only">Toggle navigation</span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="#">Awesome!</a> | |
</div> | |
{# Collect the nav links, forms, and other content for toggling #} | |
<div class="collapse navbar-collapse" id="admin-navbar-collapse-1"> | |
{{ knp_menu_render('AcmeHelloBundle:MenuBuilder:mainMenu', {'currentClass': 'active', 'template': 'AcmeHelloBundle:Menu:knp_menu.html.twig'}) }} | |
{{ knp_menu_render('AcmeHelloBundle:MenuBuilder:userMenu', {'currentClass': 'active', 'template': 'AcmeHelloBundle:Menu:knp_menu.html.twig'}) }} | |
</div> | |
</div> | |
</div> | |
<div class="content" style="margin-top: 60px;"> | |
Content goes here! | |
</div> | |
{% endblock %} | |
{% block javascripts %} | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> | |
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> | |
{% endblock %} |
<?php | |
namespace Acme\HelloBundle\Menu; | |
use Knp\Menu\ItemInterface; | |
use Knp\Menu\Matcher\Voter\VoterInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class RequestVoter implements VoterInterface | |
{ | |
private $container; | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
public function matchItem(ItemInterface $item) | |
{ | |
if ($item->getUri() === $this->container->get('request')->getRequestUri()) { | |
// URL's completely match | |
return true; | |
} else if($item->getUri() !== $this->container->get('request')->getBaseUrl().'/' && (substr($this->container->get('request')->getRequestUri(), 0, strlen($item->getUri())) === $item->getUri())) { | |
// URL isn't just "/" and the first part of the URL match | |
return true; | |
} | |
return null; | |
} | |
} |
services: | |
acme.hello.menu.voter.request: | |
class: Acme\HelloBundle\Menu\RequestVoter | |
arguments: | |
- @service_container | |
tags: | |
- { name: knp_menu.voter } |
To work correct in symfony 3.2 need updates:
RequestVoter
`<?php
namespace Bundles\CoreBundle\Menu;
use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Voter\VoterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class RequestVoter implements VoterInterface
{
/**
* @var Request
*/
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* @param ItemInterface $item
* @return bool|null
*/
public function matchItem(ItemInterface $item)
{
if ($item->getUri() === $this->request->getRequestUri()) {
// URL's completely match
return true;
} else if($item->getUri() !== $this->request->getBaseUrl().'/' && (substr($this->request->getRequestUri(), 0, strlen($item->getUri())) === $item->getUri())) {
// URL isn't just "/" and the first part of the URL match
return true;
}
return null;
}
}
`
and service
service.yml
`
services:
bundles.core.menu.voter.request:
class: Bundles\CoreBundle\Menu\RequestVoter
autowire: true
tags:
- { name: knp_menu.voter }
`
to see correct remove new line in service
In > Symfony3.2 you should use the ContainerAwareTrait because Symfony\Component\DependencyInjection\ContainerAware is not supported anymore. Thanks for sharing.
Replace:
use Symfony\Component\DependencyInjection\ContainerAware;
with:
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
and then add the trait in the class itself:
use ContainerAwareTrait;
Thank you for providing that. Can you please explain why the RequestVoter
is needed?
Dropdown is supported by using:
$username = $this->tokenStorage->getToken()->getUsername();
$menu->addChild($username, [
'extras' => [
'dropdown' => true,
]
]);
$menu[$username]->addChild('Logout', array(
'route' => 'fos_user_security_logout'
));
Hmm, I love it but it doesn't suppert dropdowns?
Edit: Ill leave that here: