dongzhuo6137 2017-01-02 20:04
浏览 91
已采纳

如何在Slim 3或nikic / FastRoute参数中允许点?

I am using Slim 3, which uses nikic/FastRoute, and am having trouble with an endpoint like so:

$app->get('/count/{url}', function ($request, $response) use ($curl) {
    $controller = new Proximate\Controller\CountUrl($request, $response);
    $controller->setCurl($curl);
    return $controller->execute();
});

My plan is to pass an urlencoded URL into {url} and inject that into the controller. For example, for http://www.example.com, the request would be:

curl \
    --verbose \
    http://localhost:8080/count/http%3A%2F%2Fwww.example.com%2F

However, this fails with a 404, so is evidently not matched. This also fails:

curl \
    --verbose \
    http://localhost:8080/count/http%3A%2F%2Fwww.

However, oddly, this does match (i.e. without the trailing dot):

curl \
    --verbose \
    http://localhost:8080/count/http%3A%2F%2Fwww

I originally thought it was the urlencoded slashes that was confusing it (%2F) but having tried it without these characters, I find that in fact it is the dot, anywhere in the string. Why does this not match, and do I need a regex match in order to get this to work?

I am using the PHP built-in web server for this app.

  • 写回答

1条回答 默认 最新

  • dtgv52982 2017-01-02 20:19
    关注

    After a bit more digging, I find that this is caused by the PHP built-in web server, and is not a Slim or FastRoute issue at all. Here is the Slim bug report and here is one for PHP.

    Sadly the core PHP team have marked as Won't fix, since the server is only for testing. Personally, I think the warnings about not using this server for production are a bit overblown (my Docker containers remain of a reasonably manageable size because I am not throwing Apache in there as well).

    Thankfully there is a fix for this - specifying the script name in the URL will cause PHP to pass the rest of it correctly to the routing system. Like so:

    curl \
        --verbose \
        http://localhost:8080/index.php/count/http%3A%2F%2Fwww.example.com%2F
    #                         ^^^^^^^^^
    #                         Script name
    

    Of course, this is not very elegant, so I may yet switch to another solution. I've not tried it yet, but this PHP-only web server looks quite promising.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效