苗条 4 次投掷 Slim\Exception\HttpNotFoundException

Slim 4 throws 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

        

    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 在 public/ 文件夹上:

# 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/ [L]

composer.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() 所说

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

我也曾尝试更改此 apache 配置 httpd.conf 指令,但没有任何效果,因为我在某处看到过,但没有用。

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

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

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

我查看的其他资源:

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

好的,我设法解决了这个问题,这是有效的代码。主要变化是 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();

?>