php - Slim 4 抛出 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
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:
# 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]
作曲家.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>
我不知道我可以说哪些更相关的信息。
我查看的其他资源:
解决方案
好的,我设法解决了,这是有效的代码。主要变化是关于路径参数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();
?>
推荐阅读
- c# - C# Protobuf:“协议消息包含带有无效线路类型的标签。”
- c# - Automapper 缺少类型映射配置
- jquery - 使用最近的标题标签添加属性
- c++ - 在Qt中关闭串口之前发送串口消息
- swift - SwiftUI 应用程序中的 SQLite.swift 将无法编译
- python - 如何为 Windows 安装 opus?
- mysql - 如何将两个查询合并为一个子查询
- wordpress - WooCommerce - 在购物车中显示多个变体名称作为产品名称
- node.js - 在 Express 应用程序中处理“预期”HTTP 标头
- perforce - 从 p4 仓库路径中提取分支名称