I'm working on an API based system from scratch, and I'm using Slim 3.0.
I want to have different classes that implement \Slim\App as part of it to separate the logic of different API's yet have a unique access point to any of the routes of any of the classes in the API rest.
To accomplish this, I created a base API class that handles the \Slim\App object as part of it. Also, I created several API classes that inherit from the base class. The next is the base class called "ApiRest".
use Slim\App as ApiSlim;
class ApiRest
{
protected $app;
protected $actualConfiguration;
protected $middlewares;
public function __construct($config = [], $policies=[])
{
// ..... previous configuration
$this->app = new ApiSlim($this->actualConfiguration);
}//end __construct()
public function run($silent = false) {
$this->app->run($silent);
}
public function getAllRoutes()
{
// Slim/Router
return $this->app->getContainer()->get('router')->getRoutes();
}
public function getApp()
{
return $this->app;
}
}//end class
Then, one of the children class is next:
class ChildApiRest extends ApiRest{
public function __construct() {
parent::__construct([], 'auth');
$this->app->get('hello/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
//return $response;
});
}
}
So, the unique access point, just say "api.php" file, is the next one:
$globalRouter = new \Slim\App();
$globalRouter->group('/services/', function () use($globalRouter){
$this->group('oneService/', function () use($globalRouter){
// At this point I want to "publish" the routes of the specified API
$apiChild = new ChildApiRest();
$apiChild->run();
});
});
$globalRouter->run();
I expect to add as many groups as child API's that I have. With these, I have the routes separated for functionality, but yet remain accessible from one single point (but it's not working).
Finally, when i try to get access to the "hello" api througth this
"http://myserver/api.php/services/oneService/hello/world", Slim response with 404 error, and the next Fatal error
I get this error:
Fatal error: Uncaught exception 'RuntimeException' with message
'Unexpected data in output buffer.
Maybe you have characters before an opening <?php tag?' .....