Use on-route controller method with Slim

1

I have a controller called CardController with several methods inside it, but I need to pass some data to these methods via parameter. Currently I can only access the methods directly as the second example

$app->group('/v1', function() {

    /**
     * Dentro de v1, o recurso /card
     */
    $this->group('/card', function($app) {
        $this->get('', function (Request $request, Response $response) use ($app){
            //chamar a funcao getcard do controller aqui passando parametros
        });

        $this->post('', '\App\v1\Controllers\CardController:getcard'); //só consigo chamar assim
    
asked by anonymous 26.06.2018 / 21:44

1 answer

1

The question got a bit confusing, but I'll try to help based on what I understand.

You can create a controller with the __invoke() method and receive the $request and $ respond objects in this method. Then you pass this created controller in place of the anonymous function that you declared as a parameter of the $this->get() method.

For you to pass the data to the methods you will have to pass the parameters to them. Remembering that only one method is executed in a controller for a given route. The other methods will be called from within the controller, or else they will be associated with other routes.

    
26.06.2018 / 22:51