DEV Community

Cover image for Laravel Wildcard Routes & Fallback Routes
Buddhi Eashwarage
Buddhi Eashwarage

Posted on • Originally published at theodddeveloper.com

Laravel Wildcard Routes & Fallback Routes

Laravel routes can be categorized into several types. The wildcard routes and fallback routes are quite popular among them.

So, let’s explore when to use them.

Spoiler Alert

▶️ Please note that there's a YouTube version of this article as well.

LINK - https://www.youtube.com/watch?v=5n1MqSEE7vU

Wildcard routes

As the word “wildcard” emphasizes it can have any value. We don’t need to declare the routes explicitly here and Laravel resolves them for us implicitly. But when defining wildcard routes there is something we should keep in mind. That’s the order of the routes!

This means when we define the wildcard routes with normal routes, first we should define the normal routes then the wildcard routes, and finally the fallback route (I’ll explain this in a second 🙂)

The following route is the only visible route when you have a fresh Laravel installation.

fresh Laravel installation

So, let’s add some routes there. And then it’ll look like below.

add some routes there

This is the correct order.

As you can see I have highlight the wildcard route in red, and it goes inside curly braces.

In local development environment if we try to access the route:

http://127.0.0.1:8000/hello/world

This will return “Hello World From Normal Route!!”

Hello World From Normal Route

And if we hit the route http://127.0.0.1:8000/hello/test

we hit the route

You can use any name for the second part instead of using “/test” such as “/banana”, “/burger”, “/bmw”, etc 😀

This will return “Hello World From Wild Card Route!!”

All good right?

But what will happen if we swap the order of these routes as below?

swap the order

Here the wildcard route is the first route and then the normal route. Can you guess the output?

Both routes will return “Hello World From Wild Card Route!!”.

http://127.0.0.1:8000/hello/world

Both routes

http://127.0.0.1:8000/hello/test

Hello test wildcard

Why is that?

Because “/hello” is a prefix for the route and it’s common for both wildcard and normal routes. So Laravel ignores all the routes after the “/hello” and resolves through “/{anyroute}” part. That’s why it’s important the routes defining order when you’re using wildcard routes.

Let’s explore the fallback route now.

Fallback route

Laravel has a default 404 / Not Found page.

Which means if I try to access and undefined route, it’ll show me a page like below.

default 404

But using the Laravel fallback route option allows us to customize that behavior.

Defining a fallback route is simple as below.

Defining a fallback route

It will give us a custom 404 page as below.

custom 404 page

But again please keep in mind the order of the routes matters here as well. Therefore the recommended way is to define the fallback route at the bottom of the routes list.

Even you can point the fallback route to a controller method as well as you can do with the other routes which will help to add more customized logic inside the controller and return different views accordingly.

Happy coding!

Top comments (0)