dongma6326 2015-11-07 21:09
浏览 6
已采纳

php中的更多路由服务

is theoretically possible use more services for routing?

For example if somebody use Silex and has this code:

$app = new Silex\Application();

$app->get('/test/{id}', function ($id) {
    // ...
});

$app->run();

And i create api using Slim like that:

$app = new \Slim\Slim();

$app->get('/api/' . $version . 'something', function () use ($app){
        $data = $app->request->params();
    });

$app->run();

How user could implement my API withou rewrite Slim route function to Silex route function?

Thank you very much.

  • 写回答

1条回答 默认 最新

  • dongmo1708 2015-11-08 08:32
    关注

    Giving a quick though, the way I see it you have 3 options:

    Refactor controller closures to a named function

    Both, Silex and Slim[1] can use any form of callable, so instead of passing a closure just pass a function name (or an array with class name, method name) or any other callable. This way with 1 declaration you'll be able to call it from both Slim and Silex (yes, you have to define the routes on both sides).

    This has its drawbacks as the controller signature is different for the 2 frameworks, so you'll need to hook into silex flow and change the parameters (you have the Kernel.controller event to do that).

    Also you'll need to redefine all your services and use the container wisely (i.e, don't use it as a service locator in your controllers).

    This is probably the most inflexible way.

    Define the routes in Silex and instatiate a Slim APP inside to call and return that

    You'll need to define the api routes again in Silex and in each route you can try to instatiate the Slim APP (using something as require "path/to/the/slim/app/php/file.php") and then force the run method with the silent option on[2]

    You'll probably need to refactor your Slim application a bit, you'll have to define your app in a file and call the run method in another.

    Create a Silex middleware to handle all the /api/ calls

    The easiest (?) way that I can think of is redefine the second option and create a catch all incoming request to the /api/ mount point by creating a Silex middleware that checks for each incoming request if the request has the /api/ string inside the request. Then you'll simply forawrd the request to the Slim application as I've told you in the second option.

    Using this method you don't need to redefine the routes in Silex as everything under the /api/ point will be forwarded to the Slim application.

    NOTE

    In all cases you'll probably need to transform the response given from Slim to a Symfony response. Slim 3 uses the new PSR-7 HTTP interface so you have a solution here already. Slim 2 echos the response directly so you'll need to catch that and put it inside a Symfony response (new Response(ob_get_clean())


    [1] This is for the Slim3, Slim2 may also be able to do that but I'm not sure (and the signature is different!)

    [2] Again this is for Slim3, Slim2 does not has this option, so you'll need to figure something to get the response (maybe ob_get_clean()?)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题