laravel - ระบบ login แบบเริ่มต้น

Route
Route::get('login', function()
{
return View::make('login');
});

Route::post('login_action', function()
{
$obj = new UserController();
return $obj->login();
});

Route::get('logout', function()
{
Auth::logout();
Session::flush();
return View::make('index');
});



View

@section('content')

{{ Form::open(array('url' => 'login_action', 'class' => 'form-inline')) }}
    <div><label>Email</label> <p>{{ Form::text('email') }}</p></div>
    <div><label>Password</label> <p>{{ Form::password('password') }}</p></div>
{{ Form::submit('Submit') }}
{{ Form::close() }}


@if (Session::has('message')) 
  {{ Session::get('message') }}

@endif


@if ($errors)
<div>
{{ implode($errors->all('<li>:message</li>') ) }}
</div>
@endif

@stop


UserController.php

  public function login()
  {
        $data = array(
            'email'      => Input::get('email'),
                    'password'  => Input::get('password')
    );

$rule = array(
'email'      => 'required|email',
'password'  => 'required|min:10'
);

$messages = array(
'email'   => 'กรุณากรอกรูปแบบอีเมล์ให้ถูกต้อง',
'required' => 'กรุณากรอก :attribute',
'min'      => 'กรุณาใส่ :attribute ความยาว :min',
);

$validator = Validator::make($data, $rule, $messages);
if ($validator->passes()) {

if (Auth::attempt($data)) {
User::login($data);
return Redirect::to('index');
} else {
return Redirect::to('login')->with('message', ' [!] Login Fail');
}

} else {
return Redirect::to('login')->withErrors($validator);
}
}




Model [User.php]

 public static function login($data) { $email = Input::get('email'); $result = DB::select('select * from users where email = ?', array($email)); foreach ($result as $r) { $name = $r->name; $id = $r->id; } Session::put(array('name' => $name, 'id' => $id)); }