2
I have a menu with 3 possibilities: pg1, pg2 or pg3. The page is generated in Blade, using bootstrap.
<a href="#" class="active">pg1</a>
<a href="#">pg2</a>
<a href="#">pg3</a>
I would have to identify on which route we are to activate the correct menu.
Doing some research on the Internet I found that answer that could solve my problem: Inside the view ...
<a href="#" {{{ (Request::is('pg1') ? 'class=active' : '')>pg1</a>
<a href="#" {{{ (Request::is('pg2') ? 'class=active' : '')>pg2</a>
<a href="#" {{{ (Request::is('pg3') ? 'class=active' : '')>pg3</a>
That is, the route that requested the page would be identified and the active class would be loaded accordingly.
But that answer doesn’t help me because:
- I’m in the version 5.4. In the above answer we have access to the "is" method directly through the use of the Request esttactic class. In version 5.4 I would have to do a dependency injection to use the Request object and then have access to the "is" method. Would I have to inject the dependency into all routes? Or is there any way to inject objects into this 5.4 version?
- My routes have slightly different formats.
Thus:
Route::group(['prefix'=>'{account}'], function (){
Route::get('/pg1', function ($account) {
return view('pg1');//pg1 do usuario
});
Route::get('/pg3', function ($account) {
return view('pg3');//pg2 do usuario
});
Route::get('/pg3', function ($account) {
return view('pg3');//pg3 do usuario
});
});
If you have 2 paul and Beto users.
We would have the following possible routes
paulo/pg1,
paulo/pg2,
paulo/pg3,
beto/pg1,
beto/pg2,
beto/pg3,
That is, it would always fail the route verification test to generate the active link <a href="#" {{{ (Request::is('pg1') ? 'class=active' : '')>pg1</a>
.
The only solution I see at the moment would be to do a REGEX test to check pg1 or pg2 or pg3.
What would be another approach to avoid this REGEX test?
tried to use
Request::route()->getName()
? instead ofRequest::is
– RFL