Request::is() only work on parent route. How to use that on child route?

匆匆过客 提交于 2021-01-29 17:32:14

问题


i've tried using Request::is() for print some bootstrap class but unfortunately only work on parent route 'user', but when i using in their child not working 'user/create'

Here is my code

<ul class="dropdown-menu">
     <li class="{{ Request::is('posts') ? 'active' : '' }}"><a class="nav-link" href="/posts">Posts</a></li>
     <li class="{{ Request::is('posts/create') ? 'active' : '' }}"><a class="nav-link" href="/posts/create">Create New</a></li>
</ul>

回答1:


u can try using (*) for any routes before or after url :

<ul class="dropdown-menu">
     //post/sub_url
     <li class="{{ Request::is('posts/*') ? 'active' : '' }}"><a class="nav-link" href="/posts">Posts</a></li>
     //match url
     <li class="{{ Request::is('posts/create') ? 'active' : '' }}"><a class="nav-link" href="/posts/create">Create New</a></li>
     //match any sub_url with any prefix
     <li class="{{ Request::is('*/posts/create/*') ? 'active' : '' }}"><a class="nav-link" href="/posts/create">Create New</a></li>
</ul>



回答2:


Try this Request::segment(1) for parent because for child also segment(1) will be post or i prefer to put in in_array() so need i can add more route to parent active

<ul class="dropdown-menu">
    <li class="{{ in_array(Request::segment(1),['post']) ? 'active' : '' }}"><a class="nav-link" href="/posts">Posts</a></li>
    <li class="{{ in_array(Request::segment(1),['post']) ? 'active' : '' }}"><a class="nav-link" href="/posts/create">Create New</a></li>
</ul>


来源:https://stackoverflow.com/questions/63613735/requestis-only-work-on-parent-route-how-to-use-that-on-child-route

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