Viktorija Spasenovska
Viktorija Spasenovska

Reputation: 147

Symfony2/Silex middlewares

I am using silex to create an api and my routes look something similar to this:

$api = $app['controllers_factory'];
$users = $app['controllers_factory'];

$users->match('/', UsersController::action);

$api->mount('/users', $users);
$app->mount('/api', $api);

So the route would be baseurl/api/users

What I want to do now is to attach a before() to the $api controller group and enforce validation for the api, so any link prefixed with /api/...users, posts would require validation. But it seems that is not the way it works, when i put a before to the $api, it only works for the root of /api, not api/users or api/posts or api/categories, they require their own middlewares.

So my question is : how can I enforce everything after baseurl/api/... to require validation in a Silex enviroment.

Upvotes: 2

Views: 140

Answers (1)

Francis Eytan Dortort
Francis Eytan Dortort

Reputation: 1447

You can add before() to your $app instance (see http://silex.sensiolabs.org/doc/middlewares.html#before-middleware).

And test the $request object to see if authentication is required.

You could also use the SecurityServiceProvider, but that may be overkill depending on your use case.

Upvotes: 1

Related Questions