2013-08-15 14 views
22

フィルタ内のルートパラメータにアクセスできますか?引数をフィルタに渡す - Laravel 4

私は$ agencyIdパラメータにアクセスしたい:

Route::group(array('prefix' => 'agency'), function() 
{ 

    # Agency Dashboard 
    Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\[email protected]')); 

}); 

私は私のフィルタ内この$ agencyIdパラメータにアクセスしたい:

Route::filter('agency-auth', function() 
{ 
    // Check if the user is logged in 
    if (! Sentry::check()) 
    { 
     // Store the current uri in the session 
     Session::put('loginRedirect', Request::url()); 

     // Redirect to the login page 
     return Redirect::route('signin'); 
    } 

    // this clearly does not work..? how do i do this? 
    $agencyId = Input::get('agencyId'); 

    $agency = Sentry::getGroupProvider()->findById($agencyId); 

    // Check if the user has access to the admin page 
    if (! Sentry::getUser()->inGroup($agency)) 
    { 
     // Show the insufficient permissions page 
     return App::abort(403); 
    } 
}); 

ただ、参考のために、私のような私のコントローラでこのフィルタを呼び出します。

class AgencyController extends AuthorizedController { 

    /** 
    * Initializer. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     // Apply the admin auth filter 
     $this->beforeFilter('agency-auth'); 
    } 
... 
+2

あなたはこの '$を使用することができますagencyId = Request :: segment(2) 'フィルターで' agencyId'を取得する –

答えて

28

Input::getは、GETまたはPOST(など)の引数のみを取得できます。

Route::filter('agency-auth', function($route) { ... }); 

とGETパラメータ(あなたのフィルターで)::

$route->getParameter('agencyId'); 

(ルート・パラメータを取得するには

は、あなたはこのように、あなたのフィルターでRouteオブジェクトをつかむために持っていますちょうど楽しみのため) あなたのルートに

Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\[email protected]')); 

コンストラクタではなく、'before' => 'YOUR_FILTER'のパラメータ配列で使用できます。

14

Laravel 4.1のメソッド名がparameterに変更されました。例えば、RESTfulなコントローラで:

$this->beforeFilter(function($route, $request) { 
    $userId = $route->parameter('users'); 
}); 

別のオプションを使用すると、ルートの外にあるときに便利ですRouteファサードを通じてパラメータを取得することです:

$id = Route::input('id');