I read the documentation here about creating middleware. But which folder or file i must be create it? Documentation is not contain this information.
Under the src folder i have middleware.php
.
For example i want to get post information like this:
$app->post('/search/{keywords}', function ($request, $response, $args) {
$data = $request->getParsedBody();
//Here is some codes connecting db etc...
return json_encode($query_response);
});
i made this under the routes.php but i want to create class or middleware for this. How can i do? Which folder or file i must be use.
Slim3 does not tie you to a particular folder structure, but it does (rather) assume you use composer and use one of the PSR folder structures.
Personally, that's what I use (well, a simplified version):
in my index file /www/index.php:
In /src/My/Slim/Application.php:
I define all the dependency injections in DI/services.php and all the route definitions in config/slim-routes.php. Note that since I include the routes inside the Application constructor, they will have $this refer to the application inside the include file.
Then in DI/services.php you can have something like
in config/slim-routes.php something like
and finally your controller /src/My/Slim/Controller/HomeController.php
Also, the best way to return json is with
return $response->withJson($toReturn)