Blade in Laravel 5.4 - How to work with class="active" in the menu links of a page in this scenario?

Asked

Viewed 997 times

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:

  1. 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?
  2. 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 of Request::is

1 answer

2


I usually send this as a variable from the controller:

public function pg2() {
   ...
   return view()->with(['active' => 'pg2']);
}

In view:

<a href="#" class="@if($active == 'pg1') active @endif">pg1</a>
<a href="#" class="@if($active == 'pg2') active @endif">pg2</a>
<a href="#" class="@if($active == 'pg3') active @endif">pg3</a>



Another simple solution may include naming routes:

Route::get('/pag1', 'MeuController@pag1')->name('pag1');
Route::get('/pag2', 'MeuController@pag2')->name('pag2');
Route::get('/pag3', 'MeuController@pag3')->name('pag3');

After in view:

<a href="#" class="@if(Route::current()->getName() == 'pg1') active @endif">pg1</a>
<a href="#" class="@if(Route::current()->getName() == 'pg2') active @endif">pg2</a>
<a href="#" class="@if(Route::current()->getName() == 'pg3') active @endif">pg3</a>
  • I did not test, but I was going to quote something similar, this is the same way +1

  • There are numerous ways @Guilhermenascimento, but I also think the first is the best (of which I know)

  • It worked. I think in my case would only solve the first example because I am working with master templates in Lade. And the header loads in one place, regardless of which page it will be inserted

  • It would work in the same @zwitterion (almost sure), because the routes is only one at a time. Anyway I usually choose the first

  • Crreto @Miguel, I was thinking later and realized that I will have to use something like the second example. I’m doing tests and I don’t actually call the view from inside the routes. I usually call the view from inside the controller. Then I’ll have to find a way to pass this variable to the controller and the controller to the views. Echo that the way will be naming the same routes.

  • edited my previous post

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.