dpy3846 2016-08-17 20:31
浏览 29

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?' .....
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿