Support the ongoing development of Laravel.io →
Requests Views Forms
Last updated 1 year ago.
0

Probably the easiest way would be to use the Request::is() method. It works off the URL and not the route name, so you will need to know the end point of where you expect the user to end.

So example: http://www.example.com/user/favorites

<a href="{{ route('user.favorites') }}" class="nav-link {{ Request::is('user/favorites') ? 'active' : '' }}">
  Favorites
</a>

Where as the route would be

Route::get('/user/favorites', 'something@something')->name('user.favorites')

You can do a google search as some people have changed the Request Is into a new function that works on route names.

~ https://laracademy.co

0

The database stores the route identical to the route's name, ex: dashboard.index in database. In the route file: Route::any('/', array('uses' => 'Backend\HomeController@index', 'as' => 'dashboard.index'));

So is there a simple way to change the output of Request::is() to the 'as' name? My previous tries have been unsuccesful sadly...

0

Try something like in a helpers file.

if( ! function_exists('is_route'))
{
    /**
     * Alias for Request::is(route(...))
     *
     * @return bool
     */
    function is_route($namedRoute)
    {
        return Request::route()->getName() == $namedRoute;
    }
}

then in blade you could do

<a href="#" class="{{ is_route('dashboard.index') ? 'active' : '' }}">Link</a>

0

Thank you for your help! I've been able to pull it off, I'll place it here for reference in case anyone needs this in the future. I used a LinkHelper helper class in which I defined the is_route function. In my view I used this route to check if it corresponded with the route I had in my database.

LinkHelper:

public function is_route($namedRoute)
{
    return Request::route()->getName() == $namedRoute;
}

View:

@foreach($menus as $menu)
    <li>
    <a href="{{$menu->present()->route}}"><span aria-hidden="true" class="{{$menu->icon}} {{        LinkHelper::is_route($menu->route) ? 'active' : '' }}"></span>{{$menu->present()->name}}</a>
    </li>
@endforeach
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

exigopro exigopro Joined 13 Oct 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.