在postEdit()上找不到控制器方法

时间:2014-04-06 02:22:32

标签: php laravel routing laravel-4

即使我确定它存在,我也会得到一个奇怪的错误NotFoundHttpException Controller method not found.。我的路线非常简单,我认为artisan routes检查出来。但我一直收到这个令人沮丧的错误。

单击编辑页面上的提交按钮时会出现错误。

这是routes.php

Route::controller('users', 'UsersController');
Route::controller('listings', 'ListingsController');
Route::get('contacts', function()
{
    return View::make('listings.add');
});
Route::controller('/', 'HomeController');

这是postEdit()

中的ListingsController.php方法
public function postEdit($id)
    {
    // validate
    // read more on validation at http://laravel.com/docs/validation
    $rules = array(
            'status'       => 'required',
            'listingfor'      => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) {
            return Redirect::to('listings/edit/' . $id)
                    ->withErrors($validator);
    } else {
        $listings = Listing::find($id);
        $listings->status = Input::get('status');
        $listings->listingfor = Input::get('listingfor');
        $listings->propertystatus = Input::get('propertystatus');
        $listings->propertytype = Input::get('propertytype');
        $listings->userid = Auth::user()->id;
        $listings->reference_id = Input::get('reference_id');
        $listings->location = Input::get('location');
        $listings->lifestyle = Input::get('lifestyle');
        $listings->category = Input::get('category');
        $listings->apartment_area = Input::get('apartment_area');
        $listings->garden_area = Input::get('garden_area');
        $listings->parking = Input::get('parking');
        $listings->visitors_parking = Input::get('visitors_parking');
        $listings->driver_room = Input::get('driver_room');
        $listings->furnished = Input::get('furnished');
        $listings->salons = Input::get('salons');
        $listings->family_room = Input::get('family_room');
        $listings->master_bedrooms = Input::get('master_bedrooms');
        $listings->balconies = Input::get('balconies');
        $listings->standard_bedrooms = Input::get('standard_bedrooms');
        $listings->maid_room = Input::get('maid_room');
        $listings->dining_room = Input::get('dining_room');
        $listings->bathrooms = Input::get('bathrooms');
        $listings->contact_id = $contact_id;
        $listings->save();

        // redirect
        Session::flash('message', 'Successfully updated Listing!');
        $this->layout->content = Redirect::to('listings/main');
            }
    }

这是一种观点,我通过排除大部分字段来缩短它。 views/listings/edit.blade.php

{{ Form::model($listings, array('action' => array('ListingsController@getEdit', $listings->id), 'method' => 'PUT')) }}

<div class="panel panel-default panel-primary">
 <div class="panel-heading">
  <h3 class="panel-title">Overview</h3>
 </div>
 <div class="panel-body">
  <p>{{ Form::select('status', array(
     ''          => 'Select Status',
     'Active'       => 'Active',
     'Inactive'     => 'Inactive',
     'Trash'     => 'Trash'
      ), null,
      array('class' => 'form-control'
      )) }}
  </p>
{{ Form::submit('Edit', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

从终端php artisan routes

+--------+-----------------------------------------------------------------+------+----------------------------------+----------------+---------------+
| Domain | URI                                                             | Name | Action                           | Before Filters | After Filters |
+--------+-----------------------------------------------------------------+------+----------------------------------+----------------+---------------+
|        | GET|HEAD users/register/{one?}/{two?}/{three?}/{four?}/{five?}  |      | UsersController@getRegister      |                |               |
|        | GET|HEAD users/login/{one?}/{two?}/{three?}/{four?}/{five?}     |      | UsersController@getLogin         |                |               |
|        | GET|HEAD users/logout/{one?}/{two?}/{three?}/{four?}/{five?}    |      | UsersController@getLogout        |                |               |
|        | GET|HEAD users/dashboard/{one?}/{two?}/{three?}/{four?}/{five?} |      | UsersController@getDashboard     |                |               |
|        | POST users/create/{one?}/{two?}/{three?}/{four?}/{five?}        |      | UsersController@postCreate       |                |               |
|        | POST users/signin/{one?}/{two?}/{three?}/{four?}/{five?}        |      | UsersController@postSignin       |                |               |
|        | GET|HEAD|POST|PUT|PATCH|DELETE users/{_missing}                 |      | UsersController@missingMethod    |                |               |
|        | GET|HEAD listings/main/{one?}/{two?}/{three?}/{four?}/{five?}   |      | ListingsController@getMain       |                |               |
|        | GET|HEAD listings/view/{one?}/{two?}/{three?}/{four?}/{five?}   |      | ListingsController@getView       |                |               |
|        | GET|HEAD listings/add/{one?}/{two?}/{three?}/{four?}/{five?}    |      | ListingsController@getAdd        |                |               |
|        | GET|HEAD listings/edit/{one?}/{two?}/{three?}/{four?}/{five?}   |      | ListingsController@getEdit       |                |               |
|        | POST listings/edit/{one?}/{two?}/{three?}/{four?}/{five?}       |      | ListingsController@postEdit      |                |               |
|        | POST listings/add/{one?}/{two?}/{three?}/{four?}/{five?}        |      | ListingsController@postAdd       |                |               |
|        | GET|HEAD|POST|PUT|PATCH|DELETE listings/{_missing}              |      | ListingsController@missingMethod |                |               |
|        | GET|HEAD contacts                                               |      | Closure                          |                |               |
|        | GET|HEAD|POST|PUT|PATCH|DELETE {_missing}                       |      | HomeController@missingMethod     |                |               |
+--------+-----------------------------------------------------------------+------+----------------------------------+----------------+---------------+

1 个答案:

答案 0 :(得分:1)

您的Update Route是:

POST listings/edit/{one?}/{two?}/{three?}/{four?}/{five?} |  | ListingsController@postEdit

因此,您应该在postEdit操作中使用getedit代替form

{{ Form::model($listings, array('action' => array('ListingsController@postEdit', $listings->id))) }}

同样'method' => 'PUT'应该是'method' => 'POST',因为根据您的POST结果,它应该是PUT请求,而不是php artisan routes,默认情况下POST使用,所以你可以省略它。

当您从控制器加载表单时,还要确保已通过模型,例如,如果您有一个加载现有模型进行编辑的方法,那么您有以下路线:

GET|HEAD listings/edit/{one?}/{two?}/{three?}/{four?}/{five?} |  | ListingsController@getEdit

因此,这意味着,您使用getEdit($id)在表单中加载模型,因此您应该使用以下内容:

public function getEdit($id)
{
    $listing = Listing::find($id);
    return View::make('listings.edit')->with('listing', $listing);
}

在这种情况下,Form::model($listings ... )中的变量名称应为$listing$listing->id而不是$listings->id

应使用getEdit方法使用GET请求加载表单,并且应使用postEdit方法通过将表单提交到{{1}等网址来更新模型使用listings/edit/10模型。