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

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 in which route we are to activate the correct menu.

Doing some research on the internet I found this 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 this answer does not help me because:

  • I'm at version 5.4 . In the above response we have access to the "is" method directly through the use of the static class Request. In version 5.4 I would have to do a dependency injection to use the Request object and yes to have access to the "is" method. Would I have to do the injection of dependence on all routes? Or is there any way I can not inject objects in this 5.4 version?
  • My routes have slightly different formats.
  • So:

    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 users paulo and beto.

    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 right now would be to run a REGEX test to check for pg1 or pg2 or pg3.

    What would be another approach to avoiding this REGEX test?

        
    asked by anonymous 14.03.2017 / 19:46

    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 be to give names to routes:

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

    After the 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>
    
        
    14.03.2017 / 19:59