Laravel 5 route not defined, while it is?

226,001

Solution 1

The route() method, which is called when you do ['route' => 'someroute'] in a form opening, wants what's called a named route. You give a route a name like this:

Route::patch('/preferences/{id}',[
    'as' => 'user.preferences.update',
    'uses' => 'UserController@update'
]);

That is, you make the second argument of the route into an array, where you specify both the route name (the as), and also what to do when the route is hit (the uses).

Then, when you open the form, you call the route:

{!! Form::model(Auth::user(), [
    'method' => 'PATCH',
    'route' => ['user.preferences.update', Auth::user()->id]
]) !!}

Now, for a route without parameters, you could just do 'route' => 'routename', but since you have a parameter, you make an array instead and supply the parameters in order.

All that said, since you appear to be updating the current user's preferences, I would advise you to let the handling controller check the id of the currently logged-in user, and base the updating on that - there's no need to send in the id in the url and the route unless your users should need to update the preferences of other users as well. :)

Solution 2

This thread is old but was the first one to come up so I thought id share my solution too. Apart from having named routes in your routes.php file. This error can also occur when you have duplicate URLs in your routes file, but with different names, the error can be misleading in this scenario. Example:

Route::any('official/form/reject-form', 'FormStatus@rejectForm')
                                           ->name('reject-form');  

Route::any('official/form/accept-form', 'FormStatus@acceptForm')
                                           ->name('accept-form');

Changing one of the names solves the problem. Copy, pasting, & fatigue can lead you to this problem :).

Solution 3

If route is not defined, then check web.php routing file.

Route::get('/map', 'NavigationController@map')->name('map'); // note the name() method.

Then you can use this method in the views:

<a class="nav-link" href="{{ route('map') }}">{{ __('Map') }}</a>

PS: the __('Map') is to translate "Map" to the current language.

And the list of names for routes you can see with artisan command:

php artisan route:list

Solution 4

I'm using Laravel 5.7 and tried all of the above answers but nothing seemed to be hitting the spot.

For me, it was a rather simple fix by removing the cache files created by Laravel.
It seemed that my changes were not being reflected, and therefore my application wasn't seeing the routes.

A bit overkill, but I decided to reset all my cache at the same time using the following commands:

php artisan route:clear
php artisan view:clear
php artisan cache:clear

The main one here is the first command which will delete the bootstrap/cache/routes.php file.
The second command will remove the cached files for the views that are stored in the storage/framework/cache folder.
Finally, the last command will clear the application cache.

Solution 5

when you execute the command

php artisan route:list

You will see all your registered routes in there in table format . Well there you see many columns like Method , URI , Name , Action .. etc.

So basically if you are using route() method that means it will accept only name column values and if you want to use URI column values you should go with url() method of laravel.

Share:
226,001
Ben Fransen
Author by

Ben Fransen

I'm Ben.

Updated on September 19, 2021

Comments

  • Ben Fransen
    Ben Fransen over 2 years

    I'm a little confused on how this is supposed to work. But I'm getting an Route [/preferences/1] not defined error.

    In my routes.php I have:

    Route::patch('/preferences/{id}', 'UserController@update');

    And in the view file (account/preferences.blade.php) I have:

    {!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => '/preferences/' . Auth::user()->id]) !!}

    I'm getting an error telling me the route doesn't exist. I think I'm misunderstanding the docs on this topic but in my opinion I've defined a route for PATCH requests with a given parameter, and set this in the view correctly.

    What am I overlooking here?

  • bruchowski
    bruchowski about 9 years
    as a follow up to this question, what if the route is a resource route generated from say.. Route::resource('/users', 'UserController');, and route:list shows that it has a name of users.update, but when I try to generate the route it gives me that error.. does route() only work for explicitly named routes?
  • bruchowski
    bruchowski about 9 years
    nevermind, dug into the FormBuilder.php source, looks like this works route: ['users.update', user]
  • Richard Octovianus
    Richard Octovianus about 7 years
    yes in laravel 5 just use name(). example: Route::match(['get','post'],'/dish/update/{id}','DishControl‌​ler@storeUpdate')->n‌​ame('route__name'); redirect()->route('route__name',['id'=>$site->id]);
  • Steve
    Steve almost 7 years
    This solved the problem for me, I removed the route i have created for the register (Route::get('/register', 'Auth\RegisterController@getRegister');) and let Auth::routes() take control
  • Hashaam Ahmed
    Hashaam Ahmed over 5 years
    thanks a lot, i was going crazy over this. definitely it gives problem with duplicate URLs even if you have different names. :)
  • hossein
    hossein almost 5 years
    My issue was that I didn't realized that my route is inside a group. Which did require me to add a prefix to the name I did put.
  • Pathros
    Pathros over 4 years
    Thanks! Thanks to php artisan route:list I realized that in two routes I had defined the same URL: '/same/url/in/both/routes'. That's why one of them was not listed in the routes list.
  • Van Tran
    Van Tran about 4 years
    This does work for me! The cache is persistent in my app
  • Jeremy
    Jeremy almost 4 years
    Thanks! Saved me! route('about') was not accessible for some reason until i applied ->name('about') to the web.php file for the route.
  • Thang Nguyen
    Thang Nguyen over 2 years
    php artisan route:clear works. Thank you.