Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding breadcrumbs in Symfony2
    text
    copied!<p>I'm creating a website and have successfully got my navigation Menu working using a tree structure stored in a database.</p> <p>A <code>Menu</code> object as a <code>active</code> property, but I don't know how to go about setting it to active! If I can do that, then I can get the path of the currently active <code>Menu</code> and render the breadcrumbs using WhiteOctober's BreadcrumbsBundle or something similar. </p> <p>I've read tons of questions on SO, and also looked at the different bundles available, but I'm still stuck on how to do this.</p> <p>How would I go about finding out menu item to set as active?</p> <p>EDIT:</p> <ul> <li>Menu class: <a href="https://gist.github.com/robcaw/c76426957ea6f262d217#file-gistfile1-php" rel="nofollow">https://gist.github.com/robcaw/c76426957ea6f262d217#file-gistfile1-php</a> </li> <li>Menu controller: <a href="https://gist.github.com/robcaw/af18e7c0e61f109e9bda#file-gistfile1-php" rel="nofollow">https://gist.github.com/robcaw/af18e7c0e61f109e9bda#file-gistfile1-php</a></li> </ul> <p>EDIT 2:</p> <p>Ok, I've almost done it. I've managed to compare the current path, using <code>$this-&gt;container-&gt;get('request')-&gt;getPathInfo();</code> to a <code>Menu</code>'s set <code>url</code>. </p> <p>To do this I needed to flatten the menu tree structure into an array. As I'm using the DoctrineExtensions' Tree extension, I used its repository's <code>getChildren($node)</code> to do this. So now iterating over the array is easy and can compare the url and path.</p> <p>In the breadcrumb controller, I can pass the <code>$current_item</code> from the menu controller, and again use the Tree repository and its <code>getPath()</code> method which recursively gets parents of the given node.</p> <p>However, there is still a problem, as some pages on the website don't have a menu entry. So for example if I visit <code>site.com/news/tags/stuff</code>, there is no menu entry for it and so breaks the site.</p> <p>So far: </p> <ul> <li>MenuController: <a href="https://gist.github.com/robcaw/61383e26d0a7637bb3f1#file-gistfile1-php" rel="nofollow">https://gist.github.com/robcaw/61383e26d0a7637bb3f1#file-gistfile1-php</a></li> <li>BreadcrumbController: <a href="https://gist.github.com/robcaw/62341672db576fcb2864#file-gistfile1-txt" rel="nofollow">https://gist.github.com/robcaw/62341672db576fcb2864#file-gistfile1-txt</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload