Reputation: 1457
After trying to include Bootstrap into my project, I got myself in a situation where I have a bit of an issue with the navigation.
Currently my navigation looks like:
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
{# something to do here: #}
<li {% if currentRoute == "home" %} class="active"{% endif %}><a href="{{ path('home') }}">Home</a></li>
<li {% if currentRoute == "movies" %} class="active"{% endif %}><a href="{{ path('movies') }}">Movies</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="../future-feature/">Future feature</a></li>
</ul>
</div><!--/.nav-collapse -->
and I should add $currentRoute = $request->get('_route');
to every controller and {% if currentRoute == "routeNameGoesHere" %} class="active"{% endif %}
to every <li>
tag.
Controller for e.g. home:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class HomeController extends Controller
{
public function indexAction(Request $request)
{
$currentRoute = $request->get('_route');
// replace this example code with whatever you need
return $this->render('website/homepage.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
'currentRoute' => $currentRoute,
]);
}
}
But I want to clear it up, maybe make a seperate controller for my layout template, however that is where my question comes to my mind: Is it possible to make a controller for the layout template in Symfony to handle Bootstrap navigation? Or what is the most common used way to handle this kind of situation? (The code works all fine, it's just that I think there should be a simpler way for writing this piece of code).
Edit: I found out that whenever I would go to a url ~/home/othercontroller
in the above given example, my browser would return: Variable "currentRoute" does not exist in base.html.twig at line...
aswell. Is there a way that I would still make the home
list item capable of being active when I am on a directory/route that contains home
in front of it (~/home/othercontroller
)?
Upvotes: 1
Views: 3185
Reputation: 3812
You can check KnpMenuBundle
. It should save you lot of work with managing routes in controller/view layers.
Here You can find KnpMenu integartion with bootstrap: https://gist.github.com/nateevans/9958390
KnpMenuBundle: http://symfony.com/doc/current/bundles/KnpMenuBundle/index.html
Upvotes: 2