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.

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

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站