2014-10-29 73 views
0

你能幫我嗎?我正在使用Laravel創建我自己的登錄過程。順便說一句,我在拉拉維爾還是個新人,而且我的知識還不夠。如何在Laravel中使用控制器訪問模型函數?

我的場景是我創建了一個代碼,它帶有一個可以訪問模型函數的控制器中的參數。這個模型函數會查找用戶數據是否正確或在數據庫中匹配。但我還沒有創建它。我只想看看我的參數中的數據是否可以在模型中訪問。

問題是在模型中我無法訪問參數值。

這裏是我的代碼:

在我的控制器

public function login() { 

    $rules = array(
     'employee_id'  => 'required', 
     'employee_password' => 'required' 
    ); 

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

    if($validate_login->fails()) { 

     $messages = $validate_login->messages(); 

     return Redirect::to('/')->withErrors($messages); 

    } else { 

     $userdata = array(
      'id'  => Input::get('employee_id'), 
      'password' => Hash::make(Input::get('employee_password')) 
     ); 

     $validateDetail = Employee::ValidateLogin($userdata); //pass parameter to model 

    } 

} 

這裏的模型功能

public function scopeValidateLogin($data) 
{ 
    fd($data); //fd() is my own custom helper for displaying array with exit() 
} 

的scopeValidateLogin()函數中,我打算使用查詢生成器來驗證登錄。

這裏是我的路線

Route::model('employee','Employee'); 

Route::get('/','[email protected]'); 
Route::get('/register', '[email protected]'); 

Route::post('/login','[email protected]'); 
Route::post('/handleRegister', function() 
      { 

       $rules = array(
        'emp_code'  => 'numeric', 
        'lastname'  => 'required|min:2|max:15', 
        'firstname'  => 'required|min:2|max:20', 
        'middlename' => 'min:1|max:20', 
        'password'  => 'required|min:8|max:30', 
        'cpassword'  => 'required|same:password' 
       ); 

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

       if($validate_register->fails()) { 

        $messages = $validate_register->messages(); 

        return Redirect::to('register') 
             ->withErrors($messages) 
             ->withInput(Input::except('password','cpassword')); 

       } else { 

        $employee = new Employee; 

        $employee->emp_code  = Input::get('emp_code'); 
        $employee->lastname  = Input::get('lastname'); 
        $employee->firstname = Input::get('firstname'); 
        $employee->middlename = Input::get('middlename'); 
        $employee->gender  = Input::get('gender'); 
        $employee->birthday  = Input::get('birthday'); 
        $employee->password  = Hash::make(Input::get('password')); 

        $employee->save(); 

        Session::flash('success_notification','Success: The account has been successfully created!'); 

        return Redirect::action('[email protected]'); 

       } 

      } 
     ); 

現在運行的FD($數據)後,我的瀏覽器加載一個系列陣列,然後它會崩潰。 我不知道發生了什麼,但我認爲它向我的模型發送了多個請求。

我在做正確的方式訪問控制器內的模型?或者有沒有最好的辦法呢?

回答

0

錯誤地使用了MVC框架。您應該評估輸入並在控制器內部登錄,而不是使用模型內的方法。

+0

所以你的意思是我在控制器內進行驗證?我對MVC的理解是你所創建的數據庫中的每個事務都應該在模型中。 – Jerielle 2014-10-29 04:44:21

+1

通常,模型表示一個表,但認證邏輯的實際執行應放置在控制器內。這是一個很好的Laravel認證演示:http://code.tutsplus.com/tutorials/authentication-with-laravel-4--net-35593 – 2014-10-29 04:45:04

相關問題