Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgeary92's avatar

Request::is(route(...)) alias.. Useful for active menu states

I wrote this little helper function to use Request::is() with routes. I may have overlooked another method, so if someone has a better way to do this, please let me know.. Otherwise here is a little snippet for everyone :)

The current way to do this (i believe) is..

{{ Request::is(ltrim(route('users.edit', array(), false), '/')) }}

This is what I came up with:

if( ! function_exists('is_route'))
{
    /**
     * Alias for Request::is(route(...))
     *
     * @return bool
     */
    function is_route()
    {
        $args = func_get_args();

        foreach($args as &$arg)
        {
            if(is_array($arg))
            {
                $route = array_shift($arg);
                $arg = ltrim(route($route, $arg, false), '/');
                continue;
            }
            $arg = ltrim(route($arg, [], false), '/');
        }

        return call_user_func_array(array(app('request'), 'is'), $args);
    }
}

You can supply multiple routes like so:

{{ is_route('users.edit', 'users.show', 'users.delete') ? 'active' : '' }}

Or just the one:

{{ is_route('users.edit') ? 'active' : '' }}

Or with parameters:

{{ is_route(['users.edit', $user->id], ['users.show', $user->id]) ? 'active' : '' }}
0 likes
1 reply

Please or to participate in this conversation.