【问题标题】:PHP Slim\Exception\HttpNotFoundException 404 Not Found and nothing is helpingPHP Slim\Exception\HttpNotFoundException 404 Not Found,没有任何帮助
【发布时间】:2022-01-09 01:48:41
【问题描述】:

我正在创建一个网络应用程序,但由于 Slim 4,我无法移动。它显示此错误:

Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:91 Stack trace: #0 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #4 C:\xampp\htdocs\projectfolder\app\index.php(16): Slim\App->run() #5 {main} thrown in C:\xampp\htdocs\projectfolder\app\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 91

我正在使用 xampp,有 apache 服务器,我认为问题出在.htacces 文件中,但是没有......

这是它的外观

我已经尝试修复此问题超过 4-5 小时,我尝试了我在 google、stackoverflow、Slim 的 Github、YouTube 上找到的所有内容。没有任何效果。

.htacces

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSL,L]

我的目录

composer.json

index.php 如下:

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

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

$app = AppFactory::create();

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

$app->run();

我不知道该怎么做了,也许有人可以帮助我和其他成千上万找不到答案的人,或者移动并尝试另一个框架会更好..

【问题讨论】:

标签: php apache xampp slim slim-4


【解决方案1】:

为 Slim 4 设置 URL 基本路径检测器。 按照这里的步骤https://github.com/selective-php/basepath

<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Selective\BasePath\BasePathMiddleware;
use Slim\Factory\AppFactory;

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().
$app->add(new BasePathMiddleware($app));

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

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

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

这应该可以正常工作。

【讨论】:

    【解决方案2】:

    我看到了一些可能导致问题的问题。

    • vendor/目录属于项目根目录
    • scr/有错别字,应该是src/
    • index.php(前端控制器)应该放在一个单独的目录中,例如公开/
    • 您正在 webservers documentRoot 的子目录中运行您的应用程序。因此,您需要在项目根目录中创建第二个 .htaccess 文件。
    • 然后您需要将 Slim basePath 配置为“projectFolder/”,或者为此使用 BasePathMiddleware。

    在我的博文Slim Framework Tutorial我详细解释了这一切。

    【讨论】:

    • 还是不行,谢谢你的时间
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 2014-09-29
    • 2016-04-17
    • 2015-06-27
    • 2021-12-27
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多