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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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!
How would I add classes on the HTML tags the bundle would render? For example, bootstrap uses: <ul class="nav navbar-nav"> but the bundle renders just a plain <ul> tag without adding a class?
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?
current menu is handled by voters, read more about them here: symfony.com/doc/current/cmf/bundles/menu/voters.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.