2014-10-29 72 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

相关问题