-1

I would like structure the API in order to separate both the routing organization from actions in separate files.

The current code does not return any errors, but the parameters are not collected correctly.

Is there a simple way to organize into functions without the need for classes, or __invoke?, the application does not require it.

public/index.php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;

foreach (glob("../src/middleware/*.php") as $middleware) {
  require $middleware;
}

require '../src/routes/routes.php';

$app->run();

src/routes/routes.php

$app->group('/v1', function () use ($app) {
    $app->post('/register', 'registerParticipant');        
});

src/middleware/registerParticipant.php

require '../lib/qrlib/vendor/qrlib.php';

function registerParticipant($request, $response, $args) {

  // demo for testing
  $foo= $request->post('foo'); 

  echo "foo= ".$foo;

  // more app logic

}
Qu4k3
  • 173
  • 2
  • 15
  • When you say `parameters are not collected correctly`, are you referring to post parameters? This code does raise an error: `Call to undefined method Slim\Http\Request::post()` – Nima Jan 09 '20 at 14:58

1 Answers1

0

Replacing $foo= $request->post('foo'); with $foo= $request->getParam('foo'); did the trick.

src/middleware/registerParticipant.php

require '../lib/qrlib/vendor/qrlib.php';

function registerParticipant($request, $response, $args) {

  // demo for testing
  $foo= $request->getParam('foo'); 

  echo "foo= ".$foo;

  // more app logic

}
Qu4k3
  • 173
  • 2
  • 15