1

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)?

Barrosy
  • 1,407
  • 2
  • 25
  • 56

1 Answers1

2

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

Paweł Mikołajczuk
  • 3,692
  • 25
  • 32
  • I was looking for something earlier on but then I got on a page with a simple example for symfony 2 with older bootstrap. Ill take a look at your links. Thanks! – Barrosy Mar 11 '16 at 14:03
  • How would I add classes on the HTML tags the bundle would render? For example, bootstrap uses: ` – Barrosy Mar 11 '16 at 14:44
  • Thank you, it helped me a bit, but I am not entirely sure how to change `current` class to `active` class. Should that be done in a similar way somehow in the builder class aswell? – Barrosy Mar 11 '16 at 14:54
  • 1
    http://stackoverflow.com/questions/24120855/how-to-change-the-current-class-to-active-in-knpmenubundle – Paweł Mikołajczuk Mar 11 '16 at 15:03
  • The only thing that's still not working entirely fine (at least I think) when I go to a details view of a certain item (from `~/movies` to `~/movies/details/1`) in the menu, the `movies` list item loses its "current" or "active" class/state. Anyways thank you so much sir! – Barrosy Mar 11 '16 at 15:11
  • 1
    `current` menu is handled by voters, read more about them here: http://symfony.com/doc/current/cmf/bundles/menu/voters.html – Paweł Mikołajczuk Mar 11 '16 at 15:15