Checking the current route in the blade

I check the routes inside my blade template to add the active class to the one defined liin my menu with this code:

<ul>
    <li class="{{ Request::is('*/sobre') || Request::is('*') ? "active" : "" }}">
        <a href="{{ Route::getCurrentRoute()->parameters()['domain'] . "/sobre" }}">Sobre o salão</a>
    </li>
    <li class="{{ Request::is('*/servicos') ? "active" : "" }}">
        <a href="{{ Route::getCurrentRoute()->parameters()['domain'] . "/servicos" }}">Serviços</a>
    </li>
    <li class="{{ Request::is('*/avaliacoes') ? "active" : "" }}">
        <a href="{{ Route::getCurrentRoute()->parameters()['domain'] . "/avaliacoes" }}">Avaliações</a>
    </li>
    <li class="{{ Request::is('*/galeria') ? "active" : "" }}">
        <a href="{{ Route::getCurrentRoute()->parameters()['domain'] . "/galeria" }}">Fotos</a>
    </li>
</ul>

And these are the routes:

Route::group(['prefix' => '{domain}', 'middleware'=>'salao'], function () {
    Route::get('/', 'Frontend\FrontendSalaoController@sobre');
    Route::get('sobre', 'Frontend\FrontendSalaoController@sobre');
    Route::get('servicos', 'Frontend\FrontendSalaoController@servicos');
    Route::get('avaliacoes', 'Frontend\FrontendSalaoController@avaliacoes');
    Route::get('galeria', 'Frontend\FrontendSalaoController@galeria');
});

When I access a route http://website/xor route http://website/x/sobre, the active class is positioned correctly. But, if I get access to the route http://website/x/servicos, the class will be added in the first li and in servicosli.

How can I handle this?

+4
source share
1 answer

Request::is('*')actually matches everyone, so the first element will always have an active class. Instead, you should check '/':

<li class="{{ Request::is('*/sobre') || Request::is('/') ? "active" : "" }}">

is , , :

<li class="{{ Request::is('*/sobre', '/') ? "active" : "" }}">
+5

Source: https://habr.com/ru/post/1608680/


All Articles