在laravel 4中不能使用post方法

时间:2014-03-07 11:09:09

标签: php laravel

我从Laravel 4开始,当我提交表单时,我收到错误: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      |                |               |

当我提交表单时,我收到错误:NotFoundHttpException和url redirect:feed / create。我不明白。

2 个答案:

答案 0 :(得分:1)

你的控制器不安宁。如果您曾经使用过工匠,那么您可以使用它来生成您的控制器,这将使其生成宁静。它将为您提供诸如index(),create()store()等方法。这些是您应该使用的宁静方法。

将代码放入您认为合适的每个代码中,然后在路线上使用Route::resource('feeds', 'FeedsController')

如果您需要生成自己的方法,因为这些方法都不适合您正在做的事情,那么您应该明确告诉您的路线。例如Route::get('/', array('as' => 'index', 'uses' => 'FeedsController@getIndex'));

答案 1 :(得分:0)

尝试更改此行

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

Route::resource('feeds', 'FeedsController');
你的routes.php中的