I’m not sure if this method would work for you since you want everything to be in the index file. I’ll share how I would approach this, if it helps you great. I would use Class:Method container resolution to make this much easier. Separate your route definitions from your functions. Create a router.php configuration file and map your routes to classes.
class ServiceName
{
public function get($req, $res, $args)
{
// service 1
}
public function get2($req, $res, $args)
{
// service 2
}
public function get3($req, $res, $args)
{
// service 3
}
}
index.php
# if using composer
require_once '../vendor/autoload.php';
# include configs
require_once 'routes.php';
$app->run();
Regarding the routes.php file make sure to include your vendor autoload file if you’re using composer in your index.php file. Make sure to change the locations to suit your project.
# if using composer
require_once '##YOUR_LOCATION##/vendor/autoload.php';
# include configs
require_once '##YOUR_LOCATION##/routes.php';
$app->run();
Hi i’m new using Slim, i tried to follow your hint, but doesn’t work the Class part when i declared the methods, now i’m do the migration slim2 to slim 4, in the code for slim 2 my coworkers do the $app->post in one file, and in the index.php just include them and works just for one include