如何在路由参数中发送 URL?

Rit*_*esh 1 php slim slim-3

我已经定义了这样的路线:

$app->map(['GET', 'POST'],'/abc/[{url}]', function ($request, $response, $args) {

    return $response;
})->add(new CustomMiddleware());
Run Code Online (Sandbox Code Playgroud)

当我传递一个 url 时它工作正常,http://但给了我一个404 page not found-Page with http://or https://。我也尝试过使用 url 编码的字符串,但给出了同样的错误:

$app->map(['GET', 'POST'],'/abc/[{url}]', function ($request, $response, $args) {

    return $response;
})->add(new CustomMiddleware());
Run Code Online (Sandbox Code Playgroud)

我正在使用 Slim 3.1 版。

jma*_*eis 5

在 url 中使用 url

当您使用斜线添加 url 时,路由不会执行,然后在 url 之后有额外的路径,该路径未在路由内定义:

例如,example.org/abc/test工作正常,但example.org/abc/http://x 只适用于这样的路由定义/abc/{url}//{other}

在 url 中使用编码的 url

出于安全原因,Apache 会以 404 Not Found 错误阻止url 中包含%5Cfor\%2Ffor 的所有请求/。因此,您不会从纤薄框架中获得 404,而是从您的网络服务器中获得。所以你的代码永远不会被执行。

您可以通过AllowEncodedSlashes On在您httpd.conf的 apache 中进行设置来启用此功能。

我的建议来解决这个问题

添加 url 作为获取参数,在不更改 apache 配置的情况下编码斜杠是有效的。

示例调用 http://localhost/abc?url=http%3A%2F%2Fstackoverflow.com

$app->map( ['GET', 'POST'], '/abc', function ($request, $response, $args) {
    $getParam = $request->getQueryParams();
    $url= $getParam['url']; // is equal to http://stackoverflow.com
});
Run Code Online (Sandbox Code Playgroud)