Laravel is a PHP framework, which offers clean solution to build your website, especially data driven ones(i.e. not static pages, rather the content is retrieved from a back-end database like MySQL)  . I’m not going to discuss much about Laravel’s terms, such as view, controller, routes here since this posts only focuses on a “dynamic menu”. You can find more Laravel information here.

 

Below is the code from “layouts” view(bootstrapped), which is used as a template for all other pages.

The basic menu items are “FrontPage” and “About-us” and we use @yield(‘dynamicGroup’) as a place holder to indicate that other dynamic menu items will be placed here.

<div class=”navbar navbar-inverse navbar-fixed-top” role=”navigation”>
<div class=”container”>
<div class=”navbar-header”>
<button type=”button” class=”navbar-toggle” data-toggle=”collapse” data-target=”.navbar-collapse”>
<span class=”icon-bar”></span>
<span class=”icon-bar”></span>
<span class=”icon-bar”></span>
</button>
<a class=”navbar-brand” href=”/”>SomethingSomething</a>
</div>
<div class=”navbar-collapse collapse”>
<ul class=”nav navbar-nav”>
<li><a href=”/showcars”><b>Our Cars</b></a></li>
<li><a href=”/”><span class=”glyphicon glyphicon-home”></span>FrontPage</a></li>
<!– dymanic menu,ensure it’s not too many! –>
@yield(‘dynamicGroup’)
<li><a href=”/about-us”>About-us</a></li>
<!– <li><a href=”#contact”>contact us</a></li> –>
</ul>
</div><!–/.navbar-collapse –>
</div>
</div>

 

On each page(i.e view) you simply need to insert the following code to build menu items on-the-fly. In my below example $tourgroups contains a list of items that I want to show on the menu.

@section(‘dynamicGroup’)
@foreach($tourgroups as $tourgrp)
<li><a href=”/tourgroup/{{ $tourgrp->id }}” class=”route”>{{$tourgrp->name}}</a></li>
@endforeach
@stop

Of course, before the view is called, you’ll need to populate $tourgroups and pass to the view like below:

return View::make(‘home.index’)

->with(‘tourgroups’,TourGroup::orderBy(‘sortOrder’)->get());

 

[widgets_on_pages]

Making dynamic menus using Laravel
Tagged on:                     

5 thoughts on “Making dynamic menus using Laravel

  • May 10, 2014 at 11:14 pm
    Permalink

    great article, very clear and helps me a lot, thanks!

    Reply
  • July 1, 2014 at 10:42 pm
    Permalink

    I have to tell you that it’s hard to find your articles in google,
    i found this one on 18 spot, you should build some quality backlinks in order to rank your webiste, i
    know how to help you, just type in google – k2 seo tips and tricks

    Reply
  • July 6, 2014 at 8:04 pm
    Permalink

    I see a lot of interesting articles on your blog.
    You have to spend a lot of time writing, i know how to save you a lot of work, there is a tool that creates unique, SEO friendly
    articles in couple of minutes, just type in google – laranita’s
    free content source

    Reply
  • July 13, 2014 at 7:44 pm
    Permalink

    Really no matter if someone doesn’t know after that its
    up to other viewers that they will assist, so here it occurs.

    Reply
  • September 1, 2014 at 1:10 pm
    Permalink

    I read a lot of interesting posts here. Probably you spend a lot of time writing, i know how to save you
    a lot of time, there is an online tool that creates readable, SEO friendly articles in minutes, just search in google
    – laranitas free content source

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 17 = 19

This site uses Akismet to reduce spam. Learn how your comment data is processed.