我正在使用Laravel 4。我想在使用Laravel的Blade模板引擎时,在视图中的@if语句内访问当前URL,但我不知道如何做。
我知道可以使用类似于
有什么建议吗?
我知道可以使用类似于
<?php echo URL::current(); ?>
的方法来实现,但在@if Blade语句内部不可能这样做。有什么建议吗?
<?php echo URL::current(); ?>
的方法来实现,但在@if Blade语句内部不可能这样做。个人而言,我不会尝试在视图中获取它。我对Laravel并不是很擅长,但我想你需要将你的路由发送到控制器,在控制器中通过数组将变量传递到视图,例如使用$url = Request::url();
之类的内容。
至少有一种方法可以实现。
编辑:实际上,看一下上面的方法可能是更好的选择。
对我来说,这是最好的方法:
class="{{url()->current() == route('dashboard') ? 'bg-gray-900 text-white' : 'text-gray-300'}}"
使用Bootstrap可以创建一个简单的导航栏:
<li class="{{ Request::is('user/profile')? 'active': '' }}">
<a href="{{ url('user/profile') }}">Profile </a>
</li>
<li class="{{ Request::is('contacts/*') ? 'active' : '' }}">Dashboard</li>
<li{!!(Request::is('your_url')) ? ' class="active"' : '' !!}>
或者
<li @if(Request::is('your_url'))class="active"@endif>
<b class="{{ Request::is('admin/login') ? 'active' : '' }}">Login Account Details</b>
最简单的方法是使用:Request::url();
但以下是一种复杂的方式:
URL::to('/').'/'.Route::getCurrentRoute()->getPath();
对于命名路由,我使用:
@if(url()->current() == route('routeName')) class="current" @endif
<li>
,并且您需要在Laravel项目中使用HTMLBuilder
库。<script type="text/javascript">
$(document).ready(function(){
$('.list-group a[href="/{{Request::path()}}"]').addClass('active');
});
</script>
class="nav-link {{ \Route::current()->getName() == 'panel' ? 'active' : ''}}"