Laravel 4.1 Request::is for active menu not working

喜你入骨 提交于 2019-12-06 00:30:24

问题


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.


回答1:


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>



回答2:


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>



回答3:


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' : '' }}"



回答4:


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.



来源:https://stackoverflow.com/questions/20808016/laravel-4-1-requestis-for-active-menu-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!