2013-07-17 9 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

__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は 'コンストラクタ 'を' filters'に変換したようです:D –

+1

@RobinHoodそして今彼らはミドルウェアと呼ばれています。 – totymedli

関連する問題