【问题标题】:Php Slim framework rooter is not workingPhp Slim 框架 rooter 不工作
【发布时间】:2016-10-13 22:56:46
【问题描述】:

我不知道为什么我的超薄应用表现得很奇怪,所有 url 都被视为索引“/”,示例:

我有这 3 个网址:

$app->get('/', function ($request, $response){return "index";});
$app->get('/user', function ($request, $response){return "user";});
$app->get('/superuser', function ($request, $response){return "superuser";});

如果我去 localhost 或 localhost/user 或 localhost/superuser 或事件任何其他 url localhost/ANYTHING ;我总是使用 HTTP STATUS 200 获得索引

请帮忙

【问题讨论】:

    标签: php frameworks slim


    【解决方案1】:

    你的回调应该返回实现Psr\Http\Message\ResponseInterface的对象,而它们返回字符串。

    因此而不是

    $app->get('/', function ($request, $response){return "index";});
    

    你应该有

    $app->get('/', function ($request, $response) {
        return $response->write('index');
    });
    

    我还建议显示错误,至少对于开发版本。这是link, that describes how to do that

    【讨论】:

    • 是的,您需要返回带有修改后状态码的 Response 对象。
    【解决方案2】:

    感谢大家的建议,但我发现问题是由于我运行内置 php 服务器的命令错误我正在运行此命令: php -S 0.0.0.0:8080 public/index.php 而不是这个:php -S 0.0.0.0:8080 -t public public/index.php 如果有人可以向我解释 -t public 我会做什么:)

    【讨论】:

    • -t 选项用于指定显式文档根。如果未提供,且 URI 未指定文件,则返回 index.php 或 index.html。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2015-10-15
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2017-07-23
    相关资源
    最近更新 更多