2013-07-17 11 views
11

며칠 전에 laravel로 시작한이 문제에 직면하고 있습니다.Laravel 컨트롤러 구성

NO은 반환되지 않습니다.

이것은 Controller입니다. 이유는 무엇입니까?

Class TestController extends BaseController { 

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

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

확인 '인증 :: 검사()'값 – swapnesh

+0

IT는 __construct 내가 그것을 작동 시험 (ID) 기능을 통해 검사를 수행 할 때 이후 실행되지 않을 것 같다. –

+0

달성하고자하는 것은 무엇입니까? 컨트롤러의 생성자에서 'NO'값을 반환하려는 이유 (나에게 나쁜 접근 방법 인 것처럼 보입니까?) – Andreyco

답변

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

감사합니다! 매력처럼 작동합니다. –

+1

Laravel이 '필터'로 '생성자'를 설정 한 것처럼 보입니다. D –

+1

@RobinHood 그리고 지금 그들은 미들웨어라고 불린다. – totymedli