Laravel Route Not Defined, How to fix it?

We’ve all had this error, and it’s pretty annoying, to say the least. It always seems we’ve done everything right and once we try to render the page, the almighty Route Not Defined error is produced.

In this article, we will discover how to fix the route not defined error that is a very common occurrence in Laravel. This error usually points to routes, controller, or view files. Investigating these files will help us fix the route not defined issue.

It usually looks like the screenshot below –

laravel route not defined

Checklist

When you come across this error in Laravel, make sure to check the following parts of your application

1. Web.php

Ensure that the route you are trying to render has had a proper route setup in this file. The route must be named and therefore, should look like the following –

Route::get('/dashboard', 'MyController@dashboard')->name('dashboard');

Notice the ->name('dashboard') part of this routes syntax, this is of extreme importance to be correctly defined if you want to use the route() function to generate URLs.

2. View or Controller files

Lastly, you must ensure that the route function parameter matches the name exactly as defined in the web.php file. For example,

In a view

<a href="{{ route('dashboard') }}">Dashboard</a>

In a controller

$url = route('dashboard');

Summary

Hope this helps, keep this checklist handy for the next time this happens, this common mistake is easy to miss when developing! For further reading on Laravel named routes, you can check out the documentation here.

Leave a Reply

Your email address will not be published.