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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料