Use route controller method with Slim

Asked

Viewed 282 times

1

I have a controller called CardController with various methods within it, however I need to pass some data for 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
  • 1

    Confusing. Has how to explain the problem with text, not being comments in the code?

  • I tried to explain better in the text

1 answer

1

The question got a little fuzzy, but I’ll try to help based on what I understand.

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

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

  • 1

    I changed my stop parameters to public Function getcard(Request $request,Response $Response, $args) and it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.