Reputation: 59
I'm start with Laravel 4, when i submit form, i get error: NotFoundHttpException.
routes.php
<?php
// We defined a RESTful controller and all its via route directly
Route::controller('feeds', 'FeedsController');
Route::get('/', array('as' => 'index', 'uses' => 'FeedsController@getIndex'));
FeedsController.php
//The method to show the form to add a new feed
public function getCreate() {
//We load a view directly and return it to be served
return View::make('create_feed');
}
//Processing the form
public function postCreate() {
//Let's first run the valiadtion with all provided input
$validation = Validator::make(Input::all(), Feeds::$form_rules);
//If the validation passes, we add the values to the database and return to the form
if($validation->passes()) {
//We try to insert a new row with Eloquent
$create = Feeds::create(array(
'feed' => Input::get('feed'),
'title' => Input::get('title'),
'active' => Input::get('active'),
'category' => Input::get('category')
));
//We return to the form with success or error message due to state of the
if($create) {
return Redirect::to('feeds/create')->with('message', 'The feed added to the database successfully!');
} else {
return Redirect::to('feeds/create')->withInput()->with('message', 'The feed could not be added, please try again later!');
}
} else {
//If the validation does not pass, we return to the form with first error message as flash data
return Redirect::to('feed/create')->withInput()->with('message', $validation->errors()->first());
}
}
create_feed.blade.php
@if(Session::has('message'))
<h2>{{Session::get('message')}}</h2>
@endif
{{Form::open(array('url' => 'feeds/create', 'method' => 'post'))}}
<h3>Feed Category</h3>
{{Form::select('category',array('News'=>'News','Sports'=>'Sports','Technology'=>'Technology'),Input::old('category'))}}
<h3>Title</h3>
{{Form::text('title',Input::old('title'))}}
<h3>Feed URL</h3>
{{Form::text('feed',Input::old('feed'))}}
<h3>Show on Site?</h3>
{{Form::select('active',array('1'=>'Yes','2'=>'No'),Input::old('active'))}}
{{Form::submit('Save!',array('style'=>'margin:20px 100% 0 0'))}}
{{Form::close()}}
php artisan routes
| | GET feeds/create/{one?}/{two?}/{three?}/{four?}/{five?} | | Fe
edsController@getCreate | | |
| | POST feeds/create/{one?}/{two?}/{three?}/{four?}/{five?} | | Fe
edsController@postCreate | | |
| | GET feeds/index/{one?}/{two?}/{three?}/{four?}/{five?} | | Fe
edsController@getIndex | | |
| | GET feeds | | Fe
edsController@getIndex | | |
| | GET feeds/{_missing} | | Fe
edsController@missingMethod | | |
| | GET / | index | Fe
edsController@getIndex | | |
When i submit form, i get error: NotFoundHttpException and url redirect: feed/create. I don't understand.
Upvotes: 0
Views: 2051
Reputation: 571
Your controllers aren't restful. If you ever use artisan then you can get that to generate your controller which will generate it as restful. It will give you such methods like index(), create() store() etc. These are the restful methods you should be using.
By placing your code into each of these where you see fit then on the route you should use Route::resource('feeds', 'FeedsController')
.
If you ever need to generate your own methods because none of those fit into what you are doing then you should explicitly tell your route. e.g. Route::get('/', array('as' => 'index', 'uses' => 'FeedsController@getIndex'));
.
Upvotes: 1
Reputation: 108
Try changing this line
Route::controller('feeds', 'FeedsController');
to
Route::resource('feeds', 'FeedsController');
in your routes.php
Upvotes: 0