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语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?