When building your navigation or menu you will probably need to highlight the current page to help users visualize where they are on your website. Laravel offers a few ways to check the current route and URL.
Route
Route
can tell us something about the active route.
For instance, you want to toggle active if it matches the name of the current route at the URL /workspace/1/users/3
.
class="{ { Route::getCurrentRoute()->getName() === 'users.show' ? 'active' : '' } }"
Request
Unlike Route
, request()
can be used to get information about the current URL.
If you build your URL structure with a RESTful approach, you can toggle active state by URL segments.
Given at URL /workspace/1/users/3
:
class="{ { request()->segment(3) === 'users' ? 'active' : '' } }"
This will turn active on any subpage of “Users”.
What is your approach? Share in the comments.