PHP 作曲家抛出 404 URL 未找到
PHP Composer throwing 404 URL not found
我开始学习 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
但是在我的情况下,问题仍然存在。
我没有尝试将文件添加到 /var/www/html/sample
,而是转到我的项目路径:
cd /path/to/project
然后我运行:
php -S localhost:8000
现在,我可以访问我的端点:
localhost:8000/hello/foo
就是这样!
我开始学习 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
但是在我的情况下,问题仍然存在。
我没有尝试将文件添加到 /var/www/html/sample
,而是转到我的项目路径:
cd /path/to/project
然后我运行:
php -S localhost:8000
现在,我可以访问我的端点:
localhost:8000/hello/foo
就是这样!