【问题标题】:Redirecting to another page on error with Slim FrameworkSlim 框架出错时重定向到另一个页面
【发布时间】:2017-04-26 08:49:08
【问题描述】:

我想根据我网站表单中的信息重定向到一个页面(例如error.php)。我设法记录了这样的错误:

if ($date > $curdate) {
    return $response
        ->withStatus(406)
        ->withHeader('Content-Type', 'text/html')
        ->write('You can\'t select dates in the future!');
}

我该怎么做才能将您发送到特别是该错误的页面,而不是在网络选项卡中记录/请求它?

现在我明白了:

Request URL:http://raspberrypi/chartAPI/api/{id}/{date1}/{date2}
Request Method:POST
Status Code:406 Not Acceptable
Remote Address:192.168.0.80:80
Referrer Policy:no-referrer-when-downgrade

这几乎可以按预期工作。我想要做的是将我发送到“http://raspberrypi/chartAPI/error/406”(例如),并在名为 406.php(或 error406.php,或 error.php 或其他任何名称)的文件中显示内容。

我设法用这个做点什么:

return $response->withRedirect("error.php");

但我明白了:

Request URL:http://raspberrypi/chartAPI/api/{id}/{date1}/error.php
Request Method:GET
Status Code:405 Method Not Allowed

slim 会抛出这个错误:

Method not allowed. Must be one of: POST

为什么要删除 {date2}?为什么它要求使用 POST 方法?

这个问题是this one的延续,但是由于这个网站如何处理这些“老”问题,我的问题被推倒了,不管我做了多少编辑,它都不再受到关注了。

【问题讨论】:

标签: php slim slim-3


【解决方案1】:

当您使用withRedirect 时,使用路由 URL 而不是文件来浏览

错误是关于在路由需要 POST 的地方使用 GET 方法

我认为这是因为你使用一个文件重定向到,那么方法将是 Only GET 而不是 POST 或其他类似浏览器的方法

我不知道你为什么在 URL 中传递参数时使用 POST ?!

【讨论】:

    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多