简体   繁体   English

PHP Slim REST Framework,.404 未找到错误

[英]PHP Slim REST Framework, .404 not found error

I am trying to use the Slim framework.我正在尝试使用 Slim 框架。 and found weird behavior.并发现了奇怪的行为。

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../../vendor/autoload.php';
require_once '../include/db_handler.php';

$app = new \Slim\App;

$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write("Hello, api v1");
    return $response;
});


$app->get('/chat', function (Request $request, Response $response) {
    $response->getBody()->write("Hello, chat");
    return $response;
});



$app->run();
?>

Then If I point to那么如果我指向

http://some.ip.address/v1/

I get我得到

Hello, api v1

If point to如果指向

http://some.ip.address/v1/chat

I got 404 not found error.我收到 404 not found 错误。

Why??为什么??

After 30seconds, I posted the question. 30 秒后,我发布了问题。 I found an answer to this.我找到了这个问题的答案。

http://www.slimframework.com/docs/v3/tutorial/first-app.htmlhttp://www.slimframework.com/docs/v3/tutorial/first-app.html

You need .htaccess file.你需要.htaccess文件。

Don't forget to restart your server process now you've changed the configuration!现在您已经更改了配置,请不要忘记重新启动您的服务器进程!

I also have a .htaccess file in my src/public directory;我的 src/public 目录中也有一个 .htaccess 文件; this relies on Apache's rewrite module being enabled and simply makes all web requests go to index.php so that Slim can then handle all the routing for us.这依赖于启用 Apache 的重写模块,并简单地将所有 Web 请求发送到 index.php,以便 Slim 可以为我们处理所有路由。 Here's my .htaccess file:这是我的 .htaccess 文件:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM