Php Slim 4抛出Slim\Exception\HttpNotFoundException

Php Slim 4抛出Slim\Exception\HttpNotFoundException,php,apache,slim-4,Php,Apache,Slim 4,我犯了这个错误 我一整天都在寻找解决方案,包括像这里说的那样尝试设置正确的目录,还包括两个所需的.htaccess文件,但没有任何效果 这是我的index.php <?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Selective\BasePath\BasePathMiddle

我犯了这个错误

我一整天都在寻找解决方案,包括像这里说的那样尝试设置正确的目录,还包括两个所需的.htaccess文件,但没有任何效果

这是我的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();

?>
.htaccess在我的项目的根文件夹上:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [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 config httpd.conf指令,但没有任何效果,因为我在某个地方看到过,但没有效果

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

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

不允许超限
要求全部拒绝
我不知道我能说哪些更相关的信息

我查看的其他资源:


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

<?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();

?>