Slim Framework 3 - 在另一个Slim API中创建API或从不同路由共享路由?

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?' .....
duan198727
duan198727 谢谢你的帮助。实际上,错误不是由前面的“<?php”标签的某些字符的空格触发的。当我试图访问myserver/api.php/services/oneService/hello/world时,Slim响应有404错误,而Fatal错误引用了php标签之前的字符
大约 4 年之前 回复
douchanxiu5636
douchanxiu5636 如果在类声明之前或之后有空格或其他字符,则会显示此错误。请在开始PHP标记<?php和结束标记后面的空格之前检查源文件的空白区域(如果有的话,我建议不要使用它们)
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐