Reputation: 2139
I'm trying to create a form that submits data to a controller. However, I am getting this error -
Route [site.add] not defined.
This is my routes.php -
Route::resource('site','SiteController');
Route::get('submitsite','SiteController@submit');
Route::post('storesite','SiteController@add');
And this is my view file -
{{ Form::open(array(
'route' => 'site.add'
))
}}
//Form Elements go here
{{ Form::close() }}
I literally replaced the names from another controller that works perfectly, this one is giving me problems though..
Upvotes: 1
Views: 156
Reputation: 146191
You have declared a route without a name like this:
Route::post('storesite','SiteController@add');
In the form generating code you are using 'route' => 'site.add'
and no route is availble with this name so now you can either change the form generating code to this:
{{ Form::open(array('url' => 'site.add')) }}
Or you can change the route declaration by assigning a name to it:
Route::post('storesite', array('as' => 'site.add', 'uses' => 'SiteController@add'));
The as
used to assign a name to that route and uses
is used to assign the controller@method
name as it's action handler.
It's better to use a named route because to generate a URI
for a given route you can only use the route name instead of a complex URI
and it's easy to remember and also doesn't effect the code if you change the URI
for that route at any point because URI
s created from a named route using the name doesn't depend on the URI/PATH
directly, it gets generated on run time so easy to maintain. So, if you have a route like this:
Route::post('storesite', array('as' => 'site.add', 'uses' => 'SiteController@add'));
And you have used a form like this:
{{ Form::open(array('route' => 'site.add')) }}
Now if you change the uses/action
from SiteController@add
to SiteController@addnew
then you can do it even without touching the other code because your route has been used by it's name not directly using the SiteController@add
to generate the URI
.
Upvotes: 2
Reputation: 12169
Route [site.add]
not defined because you haven't added the name route yet.
Replace
Route::post('storesite','SiteController@add');
to
Route::post('storesite',[ 'as' => 'site.add', 'uses' => 'SiteController@add']);
Upvotes: 0