user695800
user695800

Reputation:

PHP Silex - Service Controller - passing a parameter

According to :

Silex - Service Controller Doc

I can define a route like this (after a couple of extra code of corse):

$app->get('/posts.json', "posts.controller:indexJsonAction");

But ... how can I pass the url used to the indexJsonAction function?

Upvotes: 1

Views: 307

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

You should be mapping that directly to the route, such as:

$app->get('/posts.json/{param1}/{param2}, 'posts.controller:indexJsonAction');

This way, in your controller, you can expect those parameters:

public function indexJsonAction($param1, $param2) {
    //now you have access to these variables.
}

Furthermore, silex uses Symfony's request under the hood, so you could also just inject the Request into the controller and get any input from the Request;

public function indexJsonAction(Request $request) {
    // use $request->get('param1'); etc
}

Upvotes: 2

Related Questions