2013-08-15 17 views
22

Czy można uzyskać dostęp do parametrów trasy w filtrze?Podawanie argumentów do filtra - Laravel 4

np. Chcę uzyskać dostęp do parametru $ agencyId:

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

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

}); 

Chcę uzyskać dostęp do tego parametru $ agencyId w moim filtr:

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); 
    } 
}); 

Tylko dla odniesienia zgłoszę ten filtr w moim kontrolera jako takie:

class AgencyController extends AuthorizedController { 

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

można użyć tego '$ agencyId = Request :: segment (2) ', aby uzyskać' agencyId' w filtrze –

Odpowiedz

28

Input::get może tylko pobierać argumenty w postaci GET lub POST (i tak dalej).

Aby uzyskać parametry trasy, trzeba chwycić Route obiekt w filtrze, tak:

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

I uzyskać parametry (w filtrze):

$route->getParameter('agencyId'); 

(dla zabawy) Na twojej trasie

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

można użyć w tablicy parametrów 'before' => 'YOUR_FILTER' zamiast wyszczególniać go w swoim konstruktorze.

14

Nazwa metody zmieniła się w Laravel 4.1 na parameter. Na przykład, w spokojnej regulatora:

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

Inną opcją jest pobranie parametru poprzez elewacji Route, co jest przydatne, gdy jesteś poza trasą:

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