純粹用routes.php頁面來測試auth的驗證功能,分為四個部分:

1.登入用 login頁面(get - /login)

2.驗證login用 (post - /login)

3.登入完成的Dashboard頁面 (get - /dash)

4.登出頁面 (get - /logout)

Route::get('/login', function(){
  echo "";
  echo "";
  echo "";
  echo "";
  echo "";
});

Route::post('/login', function(){
  $username = Input::get('username');
  $userpass = Input::get('userpass');

  $attempt = Auth::attempt(array('user_account' => $username,
  'password' => $userpass));
  if ($attempt) {
   echo "Hi~".Auth::user()->name;
  }
   return Redirect::to('/login')
   ->withErrors(['fail'=>'Email or password is wrong!']);
});


Route::get('/logout', array('before' => 'auth' , 'uses' => function(){
  echo "Bye~".Auth::user()->name;
  Auth::logout();
  return Redirect::to('/login');
}));

Route::get('/dash', array('before' => 'auth' , 'uses' => function(){
  echo "this is dashboard!!! 
"; echo "bye"; }));

進入 dashboard及logout頁面前須要先進行驗證,使用 'before'及 'uses'關鍵字 當符合 auth驗證條件才運行function的內容

另外再auth.php內 有兩種"認證驅動"可選: eloquent 及 database ,預設是 : eloquent 

便是用 model : 'User'; (與 /model下連動)

若將參數改為 database , 就可以直接指定 $table = '[tablename]'; 

來做驗證。

文章標籤
全站熱搜
創作者介紹
創作者 CccKaAsS 的頭像
CccKaAsS

ccckaass的部落格

CccKaAsS 發表在 痞客邦 留言(0) 人氣(745)