85

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]) !!}

But I'm getting this error:

Route [/preferences/1] not defined

A similar error occurs when calling the route() helper directly:

route('/preferences/' . Auth::user()->id');

I think I'm misunderstanding the docs on this topic but I've defined a route for PATCH requests with a given parameter, and set this in the view correctly. What am I overlooking here?

3
  • define your namespaces under path route controller. Commented Feb 25, 2015 at 8:51
  • take a look to : laravel.com/docs/5.0/routing#route-model-binding Commented Feb 25, 2015 at 8:51
  • Thanks for your reply. I thought route model binding is only necessary for passing entire objects to the route, not just an integer parameter? Nonetheless I've tried to register a model user of App\User to the router, but still this doesn't work. Also tried to test it with a closure in the route instead of calling the controllers method. Commented Feb 25, 2015 at 9:06

13 Answers 13

101

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. :)

Sign up to request clarification or add additional context in comments.

3 Comments

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?
nevermind, dug into the FormBuilder.php source, looks like this works route: ['users.update', user]
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.
52

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 :).

2 Comments

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
thanks a lot, i was going crazy over this. definitely it gives problem with duplicate URLs even if you have different names. :)
22

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

2 Comments

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.
Thanks! Saved me! route('about') was not accessible for some reason until i applied ->name('about') to the web.php file for the route.
19

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.

1 Comment

This does work for me! The cache is persistent in my app
9

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.

Comments

8

One more cause for this:

If the routes are overridden with the same URI (Unknowingly), it causes this error:

Eg:

Route::get('dashboard', ['uses' => 'SomeController@index', 'as' => 'my.dashboard']);
Route::get('dashboard/', ['uses' => 'SomeController@dashboard', 'as' => 'my.home_dashboard']);

In this case route 'my.dashboard' is invalidate as the both routes has same URI ('dashboard', 'dashboard/')

Solution: You should change the URI for either one

Eg:

Route::get('dashboard', ['uses' => 'SomeController@index', 'as' => 'my.dashboard']);
Route::get('home-dashboard', ['uses' => 'SomeController@dashboard', 'as' => 'my.home_dashboard']); 

// See the URI changed for this 'home-dashboard'

Hope it helps some once.

Comments

5

My case is a bit different, since it is not a form but to return a view. Add method ->name('route').

MyView.blade.php looks like this:

<a href="{{route('admin')}}">CATEGORIES</a>

And web.php routes file is defined like this:

Route::view('admin', 'admin.index')->name('admin');

Comments

3

i had the same issue and find the solution lately.

you should check if your route is rather inside a route::group

like here:

Route::group(['prefix' => 'Auth', 'as' => 'Auth.', 'namespace' => 'Auth', 'middleware' => 'Auth']

if so you should use it in the view file. like here:

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

Comments

2

In my case the solution was simple:

I have defined the route at the very start of the route.php file.

After moving the named route to the bottom, my app finally saw it. It means that somehow the route was defined too early.

1 Comment

yes in laravel 5 just use name(). example: Route::match(['get','post'],'/dish/update/{id}','DishController@storeUpdate')->name('route__name'); redirect()->route('route__name',['id'=>$site->id]);
1

On a side note:

I had the similar issues where many times I get the error Action method not found, but clearly it is define in controller.

The issue is not in controller, but rather how routes.php file is setup

Lets say you have Controller class set as a resource in route.php file

Route::resource('example', 'ExampleController');

then '/example' will have all RESTful Resource listed here: http://laravel.com/docs/5.0/controllers#restful-resource-controllers

but now you want to have some definition in form e.g: 'action'=>'ExampleController@postStore' then you have to change this route (in route.php file) to:

Route::controller('example', 'ExampleController');

Comments

1

In my case, I was using a duplicate method. I was trying to update like this:

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

When what I meant to do was something similar to this:

Route::get('/preferences/{id}', 'UserController@edit');
Route::post('/preferences/{id}', 'UserController@update');

Notice the get and post methods are different but the URLs are the same.

2 Comments

That did it for me. Copied a routes file and changed the route names but not the uris. It would have taken me a while to figure it out. Wish debugger would recognize the collision and throw that as an error instead.
Good point @PhilTune ... it would be nice if the debugger caught this. This one can be difficult to nail down by eye.
0

Please note that the command

php artisan route:list

Or to get more filter down list

php artisan route:list | grep your_route|your_controller

the forth column tells you the names of routes that are registered (usually generated by Route::resource)

Comments

0

My issue was that I was passing the route function to a method that, in turn, uses another route call under the hood!

Always check the implementation of the method to which you intend to pass the route:

For example, The issue is undefined because the to_route function itself utilizes the route function internally

// Wrong:

to_route(route('route_name'));

// Do this

to_route('route_name');
// Wrong

$this->get('/example')->assertRedirectToRoute(route('confirm'));

// Do this

$this->get('/example')->assertRedirectToRoute('confirm');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.