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 …


This content originally appeared on DEV Community and was authored by Buddhi Eashwarage

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!


This content originally appeared on DEV Community and was authored by Buddhi Eashwarage


Print Share Comment Cite Upload Translate Updates
APA

Buddhi Eashwarage | Sciencx (2024-07-15T02:11:40+00:00) Laravel Wildcard Routes & Fallback Routes. Retrieved from https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/

MLA
" » Laravel Wildcard Routes & Fallback Routes." Buddhi Eashwarage | Sciencx - Monday July 15, 2024, https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/
HARVARD
Buddhi Eashwarage | Sciencx Monday July 15, 2024 » Laravel Wildcard Routes & Fallback Routes., viewed ,<https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/>
VANCOUVER
Buddhi Eashwarage | Sciencx - » Laravel Wildcard Routes & Fallback Routes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/
CHICAGO
" » Laravel Wildcard Routes & Fallback Routes." Buddhi Eashwarage | Sciencx - Accessed . https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/
IEEE
" » Laravel Wildcard Routes & Fallback Routes." Buddhi Eashwarage | Sciencx [Online]. Available: https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/. [Accessed: ]
rf:citation
» Laravel Wildcard Routes & Fallback Routes | Buddhi Eashwarage | Sciencx | https://www.scien.cx/2024/07/15/laravel-wildcard-routes-fallback-routes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.