My name is Stefan Ashwell and when I'm not coding personal projects or painting miniatures, I'm a Software Engineer at Holmes Media in Market Harborough.

This blog is a place for me to write about things that require that little bit extra to solve. We all have gaps in our knowledge and every now and then I need to reach out on search engines or blogs to find an answer. Often, these answers come from multiple sources. This blog serves as a personal resource, but if it's helpful for me I'm sure it'll help others too!

Dynamic Routing in Laravel 5

Posted on 16 March 2015

Laravel’s routing features give you a huge amount of control over your application’s URL structure. At first glance however, one question jumped out at me. Is there a way to dynamically handle base URLs while still defining others as you need to?

What is Dynamic Routing?

A lot of the applications I build are content managed websites for clients to update and use themselves. As part of this, you want them to be able to create new top level pages on their website. For example, you want them to create URLs like this:

http://website.com/about

Not this:

http://website.com/page/about

When you look through Laravel’s routing options, all of the examples suggest that you need to use the second option. But in our situation having page in there is messy and unnecessary.

Dynamic Routing in Laravel 5

Let’s experiment with a catch all route that you can map to a Controller to query the database for a matching page:

// app/routes.php

Route::get('/{slug}', function($slug) {
	return $slug;
});

In our experiment above, we’re just spitting out the passed value in an anonymous function. But this is just to prove the theory so we can map this to a Model or Controller later. This seems to work fine, spitting out whatever slug we pass to the browser. Brilliant!

There are a couple of questions however. The first being “Does the homepage still work?”, and the second “What if I want to add my own routes too?”.

The answer to these questions is merely a matter of hierarchy. Laravel will find the first route that matches in your routes.php file and then stop. For example:

// app/routes.php

Route::get('/', '[email protected]');

Route::get('/login', '[email protected]');

Route::get('/{slug}', function($slug) {
	return $slug;
});

In this situation, if you navigate to the homepage you will see your homepage. If you navigate to /login you’ll be mapped to the LoginController and see the login template. Finally, any other URL will spit out the slug on screen.

On the flip side, let’s swap things around and see what happens:

// app/routes.php

Route::get('/{slug}', function($slug) {
	return $slug;
});

Route::get('/', '[email protected]');

Route::get('/login', '[email protected]');

First, the homepage still displays the homepage. However, navigating to /login spits out login in the browser. We’re no longer being taken to the LoginController.

By being careful with the hierarchy of our routes we can have a set up that can handle both hard coded and dynamic URLs in our Laravel application – pretty cool!

Stick around for more Laravel 5 tutorials in the future!