问题
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