首页 > 解决方案 > Slim 4 抛出 Slim\Exception\HttpNotFoundException

问题描述

我有这个错误

错误日志

我整天都在寻找解决方案,包括尝试设置正确的目录,如此处所述,还包括所需的两个 .htaccess 文件,但没有任何效果

https://github.com/selective-php/basepath#installation

这是我的index.php

<?php
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    use Selective\BasePath\BasePathMiddleware;
    use Psr\Http\Message\ResponseInterface;
    use Slim\Exception\HttpNotFoundException;
    use Slim\Factory\AppFactory;
    use Selective\BasePath\BasePathDetector;

    require_once __DIR__ . '/../vendor/autoload.php';

    $app = AppFactory::create();



    // Add Slim routing middleware
    $app->addRoutingMiddleware();

    // Set the base path to run the app in a subdirectory.
    // This path is used in urlFor().

    /* setting this full path was a solution provided by another user here

        https://stackoverflow.com/a/61677171/6791921

    but doesn´t work for me */


    $app->setBasePath("/02.rest-slim-test/public/index.php");
    $app->addErrorMiddleware(true, true, true);

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

    // Run app
    $app->run();

?>

此外,我还将分享我的项目框架和 2 .htaccess,以及我的 composer.json

文件夹结构

公共/文件夹上的 .htaccess:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

.htaccess 在我的项目的根文件夹中:

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

作曲家.json

{
    "require": {
        "slim/slim": "4.*",
        "slim/psr7": "^1.4",
        "selective/basepath": "^2.0"
    }
}

我正在使用 XAMPP:

Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/8.0.5

加载模块如 phpinfo() 说

启用了 mods 的 apache

mod_rewrite 模块已启用,如 phpinfo() 所说

此外,我尝试更改此 apache 配置 httpd.conf 指令,但没有任何效果,因为我在某个地方看到过,但不起作用。

现在是这样。对于其他项目,我没有问题。

<Directory />
    AllowOverride none
    Require all denied
</Directory>

我不知道我可以说哪些更相关的信息。

我查看的其他资源:

Apache2 上的 PHP Slim4 给出 HttpNotFoundException

苗条\异常\HttpNotFoundException

https://odan.github.io/2019/11/05/slim4-tutorial.html

标签: phpapacheslim-4

解决方案


好的,我设法解决了,这是有效的代码。主要变化是关于路径参数SetBasePath()- 尽管我之前尝试过相同的方法,但现在它可以工作了。以防万一它对某人有用。

<?php
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    use Selective\BasePath\BasePathMiddleware;
    use Psr\Http\Message\ResponseInterface;
    use Slim\Exception\HttpNotFoundException;
    use Slim\Factory\AppFactory;
    use Selective\BasePath\BasePathDetector;

    require_once __DIR__ . '/../vendor/autoload.php';



    $app = AppFactory::create();



    // Add Slim routing middleware
    $app->addRoutingMiddleware();


    $app->setBasePath("/02.rest-slim-test/public");

    $app->addErrorMiddleware(true, true, true);

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

     // Run app
     $app->run();

?>

推荐阅读