laravel在route :: controller中找不到控制器方法

时间:2014-02-15 16:28:47

标签: php laravel laravel-4

我正在使用Route::controller来查看和编辑表单。在这个行动laravel说:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Controller method not found.

我的路线:

Route::group(array('prefix'=> 'admin' ,'before'=>'auth'), function(){

    Route::controller('profile', 'ProfileController', array('getIndex'=>'profile.index', 'postUpdate'=>'profile.update'));

});

我的表格:

{{ Form::model($profile, array('route' => array('profile.update', $profile->id), 'method' => 'PUT')) }}

{{ Form::close() }}

ProfileController可:

class ProfileController extends \BaseController {

    public $layout = 'back_end.layouts.main';
    function __construct() {
        $this->beforeFilter('auth', array('except' => array('getIndex', 'postUpdate')));
        $this->beforeFilter('csrf', array('on' => 'post'));
    }    
    public function getIndex()
    {
        if( Auth::check() ){
           $profiles = Auth::user();
          return  View::make('back_end.layouts.profile')->with('profile', $profiles);
        }
        else return Redirect::intended('login');
    }

    public function postUpdate($id)
    {
       if (Session::token() != Input::get('_token'))
       {
            return Response::view('back_end.missing', array(), 404);
       }

       $rules = array(
            'name'        => 'required|alpha',
            'family'      => 'required',
            'email'       => 'required|email',
            'currPassword'=> 'required',
            'password'    => 'required|confirmed',
            'password_confirmation'=>'required',
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails())
        {
           return Redirect::to('/admin/profile')
            ->withErrors($validator)
            ->withInput();
        }
        $id   = Input::get ('id');
        $data = User ::find($id);
        $HashPassowrd = Hash::make(Input::get('password'));

        if( ! Hash::check( Input::get('currPassword') , $data->password ) )
        {
            return Redirect::to('/admin/profile')
            ->withErrors('Current Password Error!');
        }
        else{

            $admin = new User;
            $admin = User::find($id);
            $admin->name     = Input::get('name');
            $admin->family   = Input::get('family');
            $admin->email    = Input::get('email');
            $admin->password = $HashPassowrd;
            $admin->save();

            return Redirect::to('/admin/profile')
            ->withErrors('Edit Successfull');
        }
    }
}

php artisan route

+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+
| Domain | URI                                                                    | Name                | Action                                | Before Filters | After Filters |
+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+
|        | GET /                                                                  |                     | Closure                               |                |               |
|        | GET index                                                              | index               | Closure                               |                |               |
|        | GET admin/index                                                        | dashboard           | Closure                               |                |               |
|        | GET logout                                                             | logout              | Closure                               |                |               |
|        | POST auth                                                              | auth                | Closure                               | csrf           |               |
|        | GET login                                                              | login               | Closure                               |                |               |
|        | GET admin/profile/index/{one?}/{two?}/{three?}/{four?}/{five?}         | profile.index       | ProfileController@getIndex            | auth           |               |
|        | GET admin/profile                                                      |                     | ProfileController@getIndex            | auth           |               |
|        | POST admin/profile/update/{one?}/{two?}/{three?}/{four?}/{five?}       | profile.update      | ProfileController@postUpdate          | auth           |               |
|        | GET admin/profile/{_missing}                                           |                     | ProfileController@missingMethod       | auth           |               |
|        | GET admin/manaheHeaders/index/{one?}/{two?}/{three?}/{four?}/{five?}   | manageHeader.index  | ManageHeadersController@getIndex      | auth           |               |
|        | GET admin/manaheHeaders                                                |                     | ManageHeadersController@getIndex      | auth           |               |
|        | POST admin/manaheHeaders/update/{one?}/{two?}/{three?}/{four?}/{five?} | manageHeader.update | ManageHeadersController@postUpdate    | auth           |               |
|        | GET admin/manaheHeaders/{_missing}                                     |                     | ManageHeadersController@missingMethod | auth           |               |
|        | GET test                                                               | test                | Closure                               |                |               |
+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+

1 个答案:

答案 0 :(得分:1)

你有:

 v---- (POST)
POST admin/profile/update/{one?}/{two?}/{three?}/{four?}/{five?}       | profile.update      | ProfileController@postUpdate          | auth           |               |

但是在您使用'method' => 'PUT'的表单中(Form::model()),因此HTTP方法不匹配,因此该方法不存在,因为您有postUpdate

  

controller方法接受两个参数。第一个是基URI   控制器处理,而第二个是类的名称   控制器。接下来,只需向控制器添加方法,前缀为   他们回复的HTTP动词

因此,该方法应以put为前缀或更改默认设置的请求方法POST,以便在打算使用POST时从表单中删除该方法,IMO。