2013-07-17 18 views
11

Zacząłem laravel kilka dni temu i jestem w obliczu tego problemu:kontroler laravel skonstruować

NO nigdy nie wrócili!

To jest Controller, czy masz jakiś pomysł, dlaczego?

Class TestController extends BaseController { 

    public function __construct() 
    { 
     if (!Auth::check()) return 'NO'; 
    } 

    public function test($id) 
    { 
     return $id; 
    } 
} 
+0

Sprawdź 'Auth :: check()' wartość – swapnesh

+0

Wydaje się, że nigdy nie jest stracony __construct od kiedy mam kontrolę nad funkcją testu (ID) to działa. –

+0

Co chcesz osiągnąć? Dlaczego chcesz zwrócić wartość "NO" z konstruktora kontrolera (wygląda na to, że źle do mnie podchodzi)? – Andreyco

Odpowiedz

16
<?php 

class BaseController extends Controller { 

    public function __construct() 
    { 
     // Closure as callback 
     $this->beforeFilter(function(){ 
      if(!Auth::check()) { 
       return 'no'; 
      } 
     }); 

     // or register filter name 
     // $this->beforeFilter('auth'); 
     // 
     // and place this to app/filters.php 
     // Route::filter('auth', function() 
     // { 
     // if(!Auth::check()) { 
     //  return 'no'; 
     // } 
     // }); 
    } 

    public function index() 
    { 
     return "I'm at index"; 
    } 
} 
+0

Dziękuję! działa jak czar –

+1

Wygląda na to, że Laravel właśnie przekształcił 'konstruktorów' w' filters': D –

+1

@RobinHood A teraz są one nazywane 'oprogramowaniem pośredniczącym'. – totymedli

Powiązane problemy