---
title: "Laravel 4: Using Request::is() With Named Routes"
url: "https://indigotree.co.uk/insights/laravel-4-using-request-is-named-routes/"
markdown_url: "https://indigotree.co.uk/insights/laravel-4-using-request-is-named-routes.md"
date_published: "2015-01-06T12:19:47+00:00"
date_modified: "2023-09-22T12:04:57+01:00"
author: "Louise Towler"
featured_image: "https://indigotree.co.uk/wp-content/uploads/2014/11/laravel-banner.png"
featured_image_alt: "Laravel Logo"
categories:
  - "Web Development"
tags:
  - "Laravel"
  - "Routes"
  - "URI"
description: "Recently I found myself trying to add an active css class to a navigation bar in a Laravel 4 project. Fortunately, much like everything in Laravel, there is a straightforward way to do it. I'm using…"
---

# Laravel 4: Using Request::is() With Named Routes

Recently I found myself trying to add an active css class to a navigation bar in a Laravel 4 project. Fortunately, much like everything in Laravel, there is a straightforward way to do it. I'm using [named routes](https://laravel.com/docs/4.2/routing#named-routes "Laravel | Named Routes"), as I don't want to hardcode the URL structure throughout my application, and as far as I am aware, this how you would check the current URI against a particular route name.

```
Request::is(ltrim(route('users.show', [], false), '/'));
```

As you can see, it's not very readable, and it's a pain to type each time. After some tinkering, I came up with the following. It supports multiple route names, routes parameters and wildcards.

```
if( ! function_exists('is_route'))
{
	/**
	 * Alias for Request::is(route(...))
	 *
	 * @return mixed
	 */
	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);
	}
}
```

## Named routes usage

In all the examples below I've included a ternary operator to output an active class (as a more real world usage example). You of course don't need this if you're only interested in the boolean value from the `is_route()` function.

You can check the current URI against 1 route:

```
{{ is_route('user.index') ? 'active' : '' }}
```

With one or more parameters:

```
{{ is_route(['user.show', $user->id]) ? 'active' : '' }}
```

With a wildcard:

```
{{ is_route(['user.show', '*']) ? 'active' : '' }}
```

And of course, multiple named routes:

```
{{ is_route('user.index', ['user.show', $user->id]) ? 'active' : '' }}
```

If you have an interesting project and would like to discuss how we can help, [contact us](https://indigotree.co.uk/contact-us/) or give us a call [01442 877473](tel:01442877473).
