0

I'm organizing my website in sections, which should be visible at first glance on my navigation bar (with an active class on the appropriate link). At the moment, I'm checking for each link in the navbar if the current URL matches the one for the link, but it's only working for 1 URL in each case. It should be like this :

  • article ----------------> article section
  • article/create -----> article section
  • article/edit --------> article section
  • forum -------------------> forum section
  • forum/post/12345 -> forum section

Since all my "sections" use controllers, I was thinking maybe I could implement a way (maybe using the constructor) to pass a variable (section) to all the views returned by a controller, so that my layout can have access to it and set the active class on the proper link.

But I don't want to have to do return View::make('myView')->with('section', $this->section); everytime

Anyone knows how to achieve that ? Thanks.

2 Answers 2

4

You should use Request::segment(1) to compare it with section.

If your url is article/create and you use Request::segment(1) it will return you article and not article/create

And in fact, you don't have to pass anything to Blade in this case, because it should be visible:

@if (Request::segment(1) == 'article')
  class="active"
@endif
Sign up to request clarification or add additional context in comments.

2 Comments

Oooh it's a good idea. I never thought to do it directly in the view, but since I'm using route grouping for everything it should work fine ! Thanks for the tip
This is better than the Request::is('blah*); method listed everywhere else thanks.
1

You could also share it between all views, this way its easier to change the segment later if that changes and saves you from having to edit Request::segment(1) in all your views (if you have more)

Use: View::share('section', Request::segment(1));

Then in every view get the value with: $section

1 Comment

Thanks, but my navigation is a partial view directly inserted in the layout

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.