【发布时间】:2018-09-19 03:35:36
【问题描述】:
我开始学习 PHP,我正在尝试创建一个端点。
我安装了 Apache 2 并将我的文件放在:/var/www/html/ 下,但是当我尝试通过 localhost/hello/foo 访问它时,我得到一个 404 错误页面:
Not Found
The requested URL /hello/foo was not found on this server.
这是我的composer.json
{
"require": {
"slim/slim": "^3.0"
}
}
还有我的index.php:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
还有我的.htaccess 文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
我还尝试将这些文件与其他一些文件一起添加到 /var/www/html/sample 内的文件夹中,并尝试以 localhost/sample/hello/foo 的身份访问它,但结果相同。
有人可以提供任何建议吗?正如我所说,我是 PHP 新手,可能我遗漏了一些东西,但我不知道它是什么。
编辑
在链接的问题中作为可能的重复,他们说他们可以通过以下方式访问他们的端点:localhost/index.php/hello/foo 但是在我的情况下,即使是这条路径,问题仍然存在。
【问题讨论】:
-
@Frakcool。您可能需要先检查 Apache 配置。只需使用简单的 index.html 创建一个新文件夹(比如说 projectFolder)。看看是否可以调用 localhost/projectFolder 并获得响应。如果是,那么我们可以进一步调试。
-
-
@user10089632 请看我的编辑
-
如果你不想运行 PHP Web 服务器,答案很可能是这条评论:stackoverflow.com/questions/9747640/…
标签: php composer-php slim