Reputation: 3320
I have a group of routes like so:
Route::group(['prefix' => 'messages'], function () {
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessagesController@store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessagesController@show']);
Route::put('{id}', ['as' => 'messages.update', 'uses' => 'MessagesController@update']);
Route::get('{id}/read', ['as' => 'messages.read', 'uses' => 'MessagesController@read']); // ajax + Pusher
});
In my side bar I'd like to highlight the area the user is in when they click on any of these routes:
<li class="d-flex flex-column {{ (Route::currentRouteName() == 'messages') ? 'active' : '' }}">
This would work if the route was 'messages'. is there away I can do it to be like Route::currentRouteNameContains... Obviously that's not a Laravel method but how can I have it active if it CONTAINS messages?
Upvotes: 1
Views: 3649
Reputation: 81
Upd: Laravel 9+
Now you can use Route facade.
Route::is("messages*")
Doc: https://laravel.com/api/9.x/Illuminate/Support/Facades/Route.html#method_is
Upvotes: 1
Reputation: 11044
See str_contains helper function
<li class="d-flex flex-column {{ str_contains(Route::currentRouteName(), 'messages') ? 'active' : '' }}">
Beware that this would return true even if the route is /user/chat/messages/unread
which is probably not what you want
You may look at starts_with
<li class="d-flex flex-column {{ starts_with(Route::currentRouteName(), 'messages') ? 'active' : '' }}">
The helper functions no longer ship with the default installation
You need to install the laravel/helpers package
composer require laravel/helpers
Upvotes: 2
Reputation: 484
You can use getPrefix() helper function, too.
{{ request()->route()->getPrefix() === '/messages' ? 'active' : '' }}
Also you can use Route facades to do this. result will same with first
{{ Route::current()->getPrefix() === '/messages' ? 'active' : '' }}
Or combine basename() like below
{{ basename(request()->route()->getPrefix()) === 'messages' ? 'active' : '' }}
Upvotes: 0