Slim 4 抛出 Slim\Exception\HttpNotFoundException

如何解决Slim 4 抛出 Slim\Exception\HttpNotFoundException

我遇到了这个错误

error log

我一整天都在寻找解决方案,包括尝试设置此处所说的正确目录,还包括所需的两个 .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

folder structure

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

mods enabled apache

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

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

现在是这样。其他项目我都没有问题。

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

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

我查看的其他资源:

PHP Slim4 on Apache2 gives HttpNotFoundException

Slim\Exception\HttpNotFoundException

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

?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?