Multiple API in same index file

I am trying to create an API that resembles the following scheme

http://172.16.27.190:5500/application/api/v1/ServiceName
http://172.16.27.190:5500/application/api/v1/ServiceName_2
http://172.16.27.190:5500/application/api/v1/ServiceName_3

Since ServiceName, ServiceName_2, ServiceName_3 all are at the same level how to I access different services in the framework.

So now, I have something like this in my index.php:

$app->post(’/ServiceName’, function(Request $request, Response $response) {

});
$app->run();

where would I add the logic for ServiceName_2, ServiceName_3 ?

$app->post('/ServiceName', function(Request $request, Response $response) {
    // ServiceName logic
});
$app->post('/ServiceName_2', function(Request $request, Response $response) {
    // ServiceName_2 logic
});
$app->post('/ServiceName_3', function(Request $request, Response $response) {
    // ServiceName_3 logic
});
$app->run();

If you want only one logic you can do:

$app->post('/ServiceName[_{id}]', function(Request $request, Response $response, $args) {
    // ServiceName logic
    // $args['id'] have id value
});

You can read Slim docs:
I get from there:
https://www.slimframework.com/docs/objects/router.html#how-to-create-routes

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.

routes.php

$app->group('/api', function() {
    $app->group('/v1', function() {
        $this->get('/ServiceName',    'ServiceName:get');
        $this->post('/ServiceName_2', 'ServiceName:get2');
        $this->post('/ServiceName_3', 'ServiceName:get3');
    }
}

ServiceName.php

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();

I couldn’t get the routes.php code to compile so I made it look like this:

$app->group(’/api’, function() {
$app->group(’/v1’, function() {
$this->post(’/ServiceName’, ‘ServiceName:get’);
$this->post(’/ServiceName_2’, ‘ServiceName:get2’);
$this->post(’/ServiceName_3’, ‘ServiceName:get3’);
});
});

and I receive this error : /ServiceName - Uncaught Error: Call to a member function group() on null ? Any ideas why I am seeing this error ?

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();

This may also fix your Uncaught Error.

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