2014-02-15 198 views
1

我使用Route::controller來查看和編輯表單。在這個動作laravel說:laravel在route :: controller中找不到控制器方法

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工匠路線

+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+ 
| 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  | [email protected]   | auth   |    | 
|  | GET admin/profile              |      | [email protected]   | auth   |    | 
|  | POST admin/profile/update/{one?}/{two?}/{three?}/{four?}/{five?}  | profile.update  | [email protected]   | auth   |    | 
|  | GET admin/profile/{_missing}           |      | [email protected]  | auth   |    | 
|  | GET admin/manaheHeaders/index/{one?}/{two?}/{three?}/{four?}/{five?} | manageHeader.index | [email protected]  | auth   |    | 
|  | GET admin/manaheHeaders            |      | [email protected]  | auth   |    | 
|  | POST admin/manaheHeaders/update/{one?}/{two?}/{three?}/{four?}/{five?} | manageHeader.update | [email protected] | auth   |    | 
|  | GET admin/manaheHeaders/{_missing}          |      | [email protected] | auth   |    | 
|  | GET test                | test    | Closure        |    |    | 
+--------+------------------------------------------------------------------------+---------------------+---------------------------------------+----------------+---------------+ 
+0

也許檢查'artisan routes'的輸出以確保你的URI和路由名是正確的。你也沒有提到(或者我沒有看到)錯誤出現的位置 - 在調用Form :: open()時,還是在瀏覽器中訪問URI(或兩者) ?如果'Form :: open()'調用沒有問題並顯示錶單,則可能需要檢查它生成的URL,以確保它與您的路由符合預期。 – alexrussell

+0

你打的網址是什麼? –

+0

@AntonioCarlosRibeiro在路線或控制器? –

回答

1

您有:

v---- (POST) 
POST admin/profile/update/{one?}/{two?}/{three?}/{four?}/{five?}  | profile.update  | [email protected]   | auth   |    | 

但在形式你已經使用'method' => 'PUT'(在Form::model()),所以HTTP方法不匹配,因此該方法是不存在的,因爲你有postUpdate

控制器方法接受兩個參數。第一個是控制器處理的基本URI ,而第二個是 控制器的類名。接下來,只需添加方法到控制器,與 前綴,他們響應HTTP動詞

因此,該方法應該與put前綴或更改請求方法POST這是默認設置,所以你可能如果您打算使用POST,IMO,請從表格中刪除該方法。

+0

將'PUT'改爲'POST'或從'表格'中刪除'方法'不能解決這個問題 –

+0

你確定這一點,你嘗試過嗎? –

+0

是的。檢查這個鏈接http://paste.debian.net/82247/我現在得到這個錯誤:'找不到控制器的方法.' –

相關問題