dongtuo8170 2017-03-07 10:53
浏览 91
已采纳

使用Slim在组中路由REST API

I'm building a rest api with slim 3 but I'm having some trouble understanding how to do the routing. I initially had the get method working correctly at api.com/v1/companies/get and api.com/v1/companies/get/id and post method at api.com/v1/companies/post, but I refactored so all the methods would be at api.com/v1/companies/id and after this refactoring I get a 405 error on post requests saying that only the get method exists.

So I did a little more researching; the amount of small, but breaking inconsistencies I've found in other slim 3 guides has been a bit annoying, but it looks like my solution is the map() function, only I have no idea how to use it, and even the official docs skip over the part I don't understand.

This is how the code looked after the refactor that broke it:

$app->group('/v1', function() use ($app) {
    $app->group('/companies', function() use ($app) {
        $app->get('/{code}', function($request, $response, $args) {...}
        $app->get('', function($request, $response, $args) {...}
        $app->post('', function($request, $response, $args) {...}
    });
});

And my first attempts at using map():

$app->group('/v1', function() use ($app) {
    $app->map(['GET', 'POST', 'PUT', 'DELETE'], '/companies/{code}', function($request, $response, $args) use ($app) {
        //here's where I don't know what to do
        if($request->isGet()) {
            //What goes here? And I seem to be having problems accessing my parameters?
        }
     }
}
  • 写回答

1条回答 默认 最新

  • dongqiaochi2711 2017-03-07 11:54
    关注

    This code works for me:

    <?php
    
    use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;
    
    require '../vendor/autoload.php';
    
    $app = new \Slim\App;
    
    $app->group('/v1', function() {
        $this->map(['GET', 'POST', 'PUT', 'DELETE'], '/companies/{code}', function($request, $response, $args) {
    
            if($request->isGet()) {
                $response->getBody()->write("it's GET");
            }
    
            if($request->isPost()) {
                $response->getBody()->write("it's POST");
            }
    
            if($request->isPut()) {
                $response->getBody()->write("it's PUT");
            }
    
            if($request->isDelete()) {
                $response->getBody()->write("it's DELETE");
            }
    
            return $response;
        });
    });
    
    $app->run();
    

    Please don't use $app inside group. In docs you can see that $this inside group point to instance of 'Slim\App' already. Also check your .htaccess file if it's configured as described in Slim3 documentation.

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

报告相同问题?

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振