首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Slim 4抛出Slim\Exception\HttpNotFoundException

Slim 4抛出Slim\Exception\HttpNotFoundException
EN

Stack Overflow用户
提问于 2021-05-28 17:57:55
回答 1查看 621关注 0票数 0

我犯了这个错误

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

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

这是我的index.php

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

我的项目根目录上的.htaccess:

代码语言:javascript
复制
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

composer.json

代码语言:javascript
复制
{
    "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()所述,加载的模块

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

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

现在是这样了。在剩下的项目中,我没有问题。

代码语言:javascript
复制
<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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-28 20:51:33

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

代码语言:javascript
复制
<?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();

?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67743604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档