wemersonrv

Laravel: Validação no filter 'auth' e controller:detect()

Nov 28th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Opção 1... Usar group...
  2. Route::group(array('before' => 'auth'), function(){
  3.     Route::controller(Controller::detect());
  4. });
  5. // Fim opção 1
  6.  
  7. // Opção 2... Usar Filter para 'auth'
  8. Route::filter('pattern: *', 'auth');
  9. Route::controller(Controller::detect());
  10. // Fim opção 2
  11.  
  12. // Filter 'auth' com as validações
  13. Route::filter('auth', function()
  14. {
  15.     if(Auth::guest() && strpos(Request::route()->uri, 'auth') !== 0){
  16.         // NÃO Autenticado && NÃO INICIA com 'auth'
  17.         // Ação: Redireciona para 'auth'
  18.         return Redirect::to('auth');
  19.     }else if(!Auth::guest()){
  20.         // AUTENTICADO
  21.         // Ação: Valida caso auth/logout
  22.         // O restante deixa passar mesmo!
  23.         if( strpos(Request::route()->uri, 'auth') === 0){
  24.             if( !in_array('logout', Request::route()->parameters) ){
  25.                 return Redirect::to('dashboard');
  26.             }
  27.         }
  28.            
  29.     }
  30.  
  31.     // O restante que chegar aqui é pra deixar passar mesmo, pois o importante já foi validado
  32. });
Advertisement
Add Comment
Please, Sign In to add comment