【问题标题】:PHP Composer throwing 404 URL not foundPHP Composer 抛出 404 URL 未找到
【发布时间】: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


【解决方案1】:

我没有尝试将文件添加到/var/www/html/sample,而是转到了我的项目路径:

cd /path/to/project

然后我跑了:

php -S localhost:8000

现在,我可以通过以下方式访问我的端点:

localhost:8000/hello/foo

就是这样!

【讨论】:

    猜你喜欢
    • 2013-02-07
    • 2014-12-29
    • 1970-01-01
    • 2013-12-28
    • 2017-11-17
    • 2019-07-21
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    相关资源
    最近更新 更多