duandaoji3992 2014-03-07 11:09
浏览 28

在laravel 4中不能使用post方法

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.

  • 写回答

2条回答 默认 最新

  • dtf76989 2014-03-07 11:11
    关注

    Try changing this line

     Route::controller('feeds', 'FeedsController');
    

    to

    Route::resource('feeds', 'FeedsController');
    

    in your routes.php

    评论

报告相同问题?