Moe
Moe

Reputation: 470

Laravel 4.1 Request::is for active menu not working

I'm trying to make a menu active depending on route in laravel 4.1, my attempt:

<li
   {{{ (Request::is('/core') ? 'class="active"' : '') }}}><a href="{{{ URL::to('/core') }}}">Control Panel</a>
</li>

My route:

 Route::get('/core', 'CoreController@Showindex');

This is not throwing any errors just simply ignored. any help is appreciated.

Upvotes: 6

Views: 8808

Answers (5)

iAmBorgy
iAmBorgy

Reputation: 93

Update for Laravel 9.* Year 2022

<a class="nav-link {{ (Request::is('home') ? 'active' : '') }}" href="{{ url('home') }}">

Upvotes: 0

Aken Roberts
Aken Roberts

Reputation: 13467

You're using the triple brace {{{ }}} syntax, which is causing any output to be auto-escaped into HTML entities. That's messing with your output (HTML attributes aren't supposed to be escaped).

Use the triple braces when outputting data that is user-generated, or other instances where you do not control it and can't rely on it being 100% secure. If you're displaying URLs, classes, and other items that you are generating yourself, use the standard echo syntax {{ }}

Update for Laravel 5: The raw, unescaped Blade syntax is now {!! !!}. By default, Laravel 5 will escape both the original double and triple brace structures.

Upvotes: 3

Bouke Versteegh
Bouke Versteegh

Reputation: 4677

In Laravel 4.2:

By name:

<li class="{{ Route::is('user') ? 'active' : ''}}">Profile</li>

router.php

Route::get('/user/{id}', ['as' => 'user', 'uses' => 'UserController@profile']);

By url:

<li class="{{ Request::is('user/*') ? 'active' : '' }}">Profile</li>

Upvotes: 9

krustypop
krustypop

Reputation: 79

you can do like Ahmed Siouani said and use Request if you use a true url or if you use a route you can do something like this :

class="{{ URL::route('my.route') === URL::current() ? 'active' : '' }}"

Upvotes: 6

Moe
Moe

Reputation: 470

changed to:

<li
   {{{ (Request::is('/core') ? 'class=active' : '') }}}><a href="{{{ URL::to('/core')  }}}">Control Panel</a>
</li>

from 'class="active"' to 'class=active'

This working fine for <li> tag but not <a> tag, needs to be used like so:

<a href="{{{ URL::to('core') }}}" class="list-group-item {{{ (Request::is('core') ? 'active' : '') }}}">Overview</a>

Upvotes: 8

Related Questions