问题描述
我遇到了这个错误
我一整天都在寻找解决方案,包括尝试设置此处所说的正确目录,还包括所需的两个 .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 在 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() 所说
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();
?>