23

Is it possible to access route parameters within a filter?

e.g. I want to access the $agencyId parameter:

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

    # Agency Dashboard
    Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardController@getIndex'));

});

I want to access this $agencyId parameter within my filter:

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

Just for reference i call this filter in my controller as such:

class AgencyController extends AuthorizedController {

    /**
     * Initializer.
     *
     * @return void
     */
    public function __construct()
    {
        // Apply the admin auth filter
        $this->beforeFilter('agency-auth');
    }
...
1
  • 2
    you can use this $agencyId=Request::segment(2) to get the agencyId in filter Commented Aug 15, 2013 at 10:59

2 Answers 2

28

Input::get can only retrieve GET or POST (and so on) arguments.

To get route parameters, you have to grab Route object in your filter, like this :

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

And get parameters (in your filter) :

$route->getParameter('agencyId');

(just for fun) In your route

Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardController@getIndex'));

you can use in the parameters array 'before' => 'YOUR_FILTER' instead of detailing it in your constructor.

Sign up to request clarification or add additional context in comments.

Comments

14

The method name has changed in Laravel 4.1 to parameter. For example, in a RESTful controller:

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

Another option is to retrieve the parameter through the Route facade, which is handy when you are outside of a route:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.