Skip to content

Commit 2f8dfc0

Browse files
committed
Fix #55: Manage navigation with KnpMenuBundle
1 parent a0d625c commit 2f8dfc0

File tree

12 files changed

+225
-62
lines changed

12 files changed

+225
-62
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
77
## NEXT - YYYY-MM-DD
88

99
### Added
10+
- Menu's are now managed via KnpMenuBundle.
1011
- Introducing a yarn/npm based front-end workflow.
1112
- Adhere to phpleague/skeleton best practices.
1213
- Updated .editorconfig.

app/AppKernel.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function registerBundles()
2828
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
2929
new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),
3030
new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
31+
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
3132

3233
// Own bundles
3334
new DataHub\OAIBundle\DataHubOAIBundle(),

app/Resources/DataHubOAuthBundle/views/Default/layout.html.twig

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
</h3>
1111
</div>
1212
<div class="list-group">
13+
14+
{% set menuItem = knp_menu_get('main', ['OAuth']) %}
15+
{{ knp_menu_render(['main', 'OAuth']) }}
16+
<!--
1317
<a href="{{ path('datahub_oauth_clients_index') }}" class="list-group-item">
1418
{{ 'Clients' | trans }}
1519
</a>
1620
1721
<a href="{{ path('datahub_oauth_tokens_index') }}" class="list-group-item">
1822
{{ 'Tokens' | trans }}
19-
</a>
23+
</a> -->
2024
</div>
2125
</div>
2226
</div>

app/Resources/FOSUserBundle/views/Default/layout.html.twig

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
</h3>
1111
</div>
1212
<div class="list-group">
13-
<a href="{{ path('datahub_user_users_index') }}" class="list-group-item">
14-
{{ 'Users' | trans }}
15-
</a>
13+
{% set menuItem = knp_menu_get('main', ['Users']) %}
14+
{{ knp_menu_render(['main', 'Users']) }}
1615
</div>
1716
</div>
1817
</div>

app/Resources/public/sass/main.scss

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
.navbar-default .navbar-nav > li > a {
3636
color: black;
3737
}
38-
39-
.navbar-default .navbar-nav > li > a.active {
38+
39+
.navbar-default .navbar-nav > li.active > a:hover,
40+
.navbar-default .navbar-nav > li.active > a {
4041
color: white;
42+
background-color: inherit;
4143
}
4244

4345
.navbar-default .navbar-brand {

app/Resources/views/app/topnav.html.twig

+2-51
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,8 @@
1313
</a>
1414
</div>
1515
<div id="navbar" class="navbar-collapse collapse">
16-
<ul class="nav navbar-nav main-nav">
17-
<li>
18-
<a href="{{ path('datahub_shared_default_index') }}"
19-
{% if app.request.attributes.get('_route') == 'datahub_shared_default_index' %} class="active"{% endif %}>
20-
{{ 'Dashboard' | trans }}
21-
</a>
22-
</li>
23-
{% if is_granted('ROLE_USER') and is_granted('ROLE_ADMIN') %}
24-
<li>
25-
<a href="{{ path('datahub_oauth_default_index') }}"
26-
{% if app.request.attributes.get('_route') == 'datahub_oauth_default_index' %} class="active"{% endif %}>
27-
{{ 'OAuth' | trans }}
28-
</a>
29-
</li>
30-
{% endif %}
31-
{% if is_granted('ROLE_USER') and is_granted('ROLE_ADMIN') %}
32-
<li>
33-
<a href="{{ path('datahub_user_users_index') }}"
34-
{% if app.request.attributes.get('_route') == 'datahub_user_users_index' %} class="active"{% endif %}>
35-
{{ 'Users' | trans }}
36-
</a>
37-
</li>
38-
{% endif %}
39-
<li>
40-
<a href="{{ path('nelmio_api_doc_index') }}">
41-
{{ 'REST API' | trans }}
42-
</a>
43-
</li>
44-
<li>
45-
<a href="{{ path('datahub_static_docs_oai') }}"
46-
{% if app.request.attributes.get('_route') == 'datahub_static_docs_oai' %} class="active"{% endif %}>
47-
{{ 'OAI-PMH' | trans }}
48-
</a>
49-
</li>
50-
</ul>
51-
<ul class="nav navbar-nav navbar-right">
52-
{% if is_granted('ROLE_USER') and is_granted('ROLE_ADMIN') %}
53-
<li>
54-
<a href="{{ path('fos_user_security_logout') }}">
55-
{{ 'Logout' | trans }}
56-
</a>
57-
</li>
58-
{% else %}
59-
<li>
60-
<a href="{{ path('fos_user_security_login') }}">
61-
{{ 'Login' | trans }}
62-
</a>
63-
</li>
64-
{% endif %}
65-
66-
</ul>
16+
{{ knp_menu_render('main', { 'currentClass': 'active', 'depth': 1 }) }}
17+
{{ knp_menu_render('profile', { 'currentClass': 'active' }) }}
6718
</div>
6819
</div>
6920
</nav>

app/config/services.yml

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ services:
2323
class: Twig_Extensions_Extension_Text
2424
tags:
2525
- name: twig.extension
26+
27+
datahub.core.menu_builder:
28+
class: DataHub\SharedBundle\Menu\MenuBuilder
29+
arguments: ["@knp_menu.factory", "@security.authorization_checker"]
30+
tags:
31+
- { name: knp_menu.menu_builder, method: createMainMenu, alias: main }
32+
- { name: knp_menu.menu_builder, method: createProfileMenu, alias: profile }

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"symfony/psr-http-message-bridge": "^1.0",
4141
"zendframework/zend-diactoros": "^1.3",
4242
"sabre/xml": "^1.5",
43-
"willdurand/hateoas-bundle": "^1.3"
43+
"willdurand/hateoas-bundle": "^1.3",
44+
"knplabs/knp-menu-bundle": "^2.0"
4445
},
4546
"require-dev": {
4647
"sensio/generator-bundle": "~3.0",

composer.lock

+124-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace DataHub\SharedBundle\Menu;
4+
5+
use Knp\Menu\FactoryInterface;
6+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
7+
8+
class MenuBuilder
9+
{
10+
private $factory;
11+
12+
/**
13+
* Constructor
14+
*
15+
* @param FactoryInterface $factory MenuFactory
16+
* @param AuthorizationCheckerInterface $authChecker Authentication checker
17+
*/
18+
public function __construct(FactoryInterface $factory, AuthorizationCheckerInterface $authChecker)
19+
{
20+
$this->factory = $factory;
21+
$this->authChecker = $authChecker;
22+
}
23+
24+
/**
25+
* Defines the profile menu
26+
*
27+
* @param Array $array
28+
*/
29+
public function createProfileMenu(array $options) {
30+
$menu = $this->factory->createItem('root');
31+
32+
$menu->addChild('Login', array('route' => 'fos_user_security_login'));
33+
34+
if ($this->authChecker->isGranted('ROLE_USER') !== false) {
35+
unset($menu['Login']);
36+
$menu->addChild('Logout', array('route' => 'fos_user_security_logout'));
37+
}
38+
39+
$menu->setChildrenAttribute('class', 'nav navbar-nav navbar-right');
40+
41+
return $menu;
42+
}
43+
44+
/**
45+
* Defines the MainMenu menu
46+
*
47+
* @param Array $array
48+
*/
49+
public function createMainMenu(array $options)
50+
{
51+
$menu = $this->factory->createItem('root');
52+
53+
$menu->addChild('Dashboard', array('route' => 'datahub_shared_default_index'));
54+
55+
if ($this->authChecker->isGranted('ROLE_USER') !== false) {
56+
$menu->addChild('OAuth', array('route' => 'datahub_oauth_clients_index'));
57+
58+
$menu['OAuth']->addChild('Clients', array('route' => 'datahub_oauth_clients_index', 'attributes' => array('class' => 'list-group-item')));
59+
$menu['OAuth']->addChild('Tokens', array('route' => 'datahub_oauth_tokens_index', 'attributes' => array('class' => 'list-group-item')));
60+
61+
$menu->addChild('Users', array('route' => 'datahub_user_users_index'));
62+
$menu['Users']->addChild('Users', array('route' => 'datahub_user_users_index', 'attributes' => array('class' => 'list-group-item')));
63+
}
64+
65+
$menu->addChild('REST API', array('route' => 'nelmio_api_doc_index'));
66+
$menu->addChild('OAI-PMH', array('route' => 'datahub_static_docs_oai'));
67+
68+
$menu->setChildrenAttribute('class', 'nav navbar-nav main-nav');
69+
70+
return $menu;
71+
}
72+
}

web/build/app.css

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/build/app.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)