php 包含参数的 url 的 Laravel 活动菜单项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43937494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:56:39  来源:igfitidea点击:

Laravel active menu item for url included parameters

phplaravel

提问by Arbnor Salihu

I'm trying to set active class in my list item, but it doesn't work.

我正在尝试在我的列表项中设置活动类,但它不起作用。

My code in blade:

我在刀片中的代码:

@foreach($data as $site)
  <ul class="sidebar-menu" id="second-menu">
  @if(isAdmin())<li class="{{-- active class  for url parameter --}}"><a href="{{ url('sites/'.$site->id.'/edit') }}" >{{ $site->name }}</a></li>
@endif
</ul>
@endforeach

So, if I write: li class="@if(getRouteName() == 'site@index'){{ 'active' }}@endif" , it works nice, but in my case the problem is that I want to get 'active'class in foreach sites/'.$site->id.'/edit

所以,如果我写: li class="@if(getRouteName() == 'site@index'){{ 'active' }}@endif" ,它工作得很好,但在我的情况下,问题是我想在 foreach站点/'.$site->id.'/edit 中获取“活动”

Many thanks.

非常感谢。

回答by Alexey Mezenin

Use is()method. For example:

使用is()方法。例如:

<li class="{{ request()->is('sites/*/edit') ? 'active' : '' }}"

回答by Him Hah

I am currently using Laravel 5.6.* and my solution is:

我目前正在使用 Laravel 5.6.*,我的解决方案是:

<a href="#" class="nav-link {{ request()->is('users*') ? 'active' : '' }}">Users</a>

回答by cetteSara

There is also the following if you have named your route :

如果您已命名您的路线,还有以下内容:

<li class="{{ Request::routeIs('admin') ? 'active' : '' }}">...</li>

Inspired from here

灵感来自这里

回答by Michael Codes

You can try this as well

你也可以试试这个

<a href="#" class="{{ (\Request::route()->getName() == 'put the address of your route here or your route name') ? 'active' : '' }}">

e.g:

例如:

<a href="#" class="{{ (\Request::route()->getName() == 'admin') ? 'active' : '' }}">

Note: I only test in Laravel 5.5

注意:我只在 Laravel 5.5 中测试

回答by Inderjeet Singh

//my admin.app.blade file
<ul class="nav">
      <li class="nav-item @yield('query')">
        <a class="nav-link" href="{{route('query_page')}}">
          <i class="material-icons">query_builder</i>
          <p>Queries</p>
        </a>
      </li>
      <li class="nav-item @yield('feedback')">
        <a class="nav-link" href="{{route('feedback_page')}}">
          <i class="material-icons">feedback</i>
          <p>Feedback</p>
        </a>
      </li>
  </ul>

     //query.blade
     @extends('admin.app')
     @section('title', 'Query Manager')
     @section('page-heading', 'Query Manager')
     @section('query','active')



     //feedback.blade
     @extends('admin.app')
     @section('title', 'Feedback Manager')
     @section('page-heading', 'Feedback Manager')
     @section('feedback','active')

回答by Dilip Hirapara

Make a function in helper.

helper 中创建一个函数。

function activeMenu($uri = '') {
    $active = '';
    if (Request::is(Request::segment(1) . '/' . $uri . '/*') || Request::is(Request::segment(1) . '/' . $uri) || Request::is($uri)) {
        $active = 'active';
    }
    return $active;
}

Use it in sidebar.

在侧边栏中使用它。

<li class="{{ activeMenu('dashboard') }}"></li>
<li class="{{ activeMenu('student') }}"></li>
<li class="{{ activeMenu('teacher') }}"></li>

回答by Grigor IWT

Use short if. Example:

使用短如果。例子:

<a href="#" class="{{ (\Request::route()->getName() == 'this.route') ? 'active' : '' }}">