php - Slim PSR-17 响应工厂
问题描述
编辑:已解决。我刚刚降级到slim 3。
我收到以下错误消息。我已经尝试过“composer 需要 slim/http”和“composer 需要 slim/psr7”。
未捕获的 RuntimeException:无法检测到任何 PSR-17 ResponseFactory 实现。请安装支持的实现以使用AppFactory::create()
. 有关支持的实现列表,请参阅https://github.com/slimphp/Slim/blob/4.x/README.md 。在 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/Factory/AppFactory.php:166 堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim /Factory/AppFactory.php(92): Slim\Factory\AppFactory::determineResponseFactory() #1 /Applications/XAMPP/xamppfiles/htdocs/MyApi/public/index.php(12): Slim\Factory\AppFactory::create () #2 {main} 在第 166 行的 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/Factory/AppFactory.php 中抛出
解决方案
我在 Slim 4 上遇到了同样的问题,但我通过与作曲家添加 slim / psr7 来解决它。尝试在此之后给出命令“composer dump”。以下是文件“composer.json”和“index.php”的内容
我的作曲家.json;
{
"name": ".../...",
"description": "....",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "....",
"email": "......."
}
],
"minimum-stability": "dev",
"require": {
"slim/slim": "4.*",
"slim/psr7": "dev-master"
}
}
}
我的 index.php;
<?php
// https://github.com/slimphp/Slim/blob/4.x/README.md
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
/**
* Instantiate App
*
* In order for the factory to work you need to ensure you have installed
* a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
* ServerRequest creator (included with Slim PSR-7)
*/
$app = AppFactory::create();
// Add Routing Middleware
$app->addRoutingMiddleware();
/**
* Add Error Handling Middleware
*
* @param bool $displayErrorDetails -> Should be set to false in production
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler
* @param bool $logErrorDetails -> Display error details in error log
* which can be replaced by a callable of your choice.
* Note: This middleware should be added last. It will not handle any exceptions/errors
* for middleware added after it.
*/
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
// My first Route
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello World!");
return $response;
});
// Run app
$app->run();
推荐阅读
- node.js - 如何在文本索引Mongodb中将精确短语与动态字符串匹配?
- typo3 - TYPO3 - TCA 迁移 - 信息还是待办事项?
- scala - Scala Apriori算法:如果第一个索引值相同,则从两个项目集列表生成三个项目集
- javascript - 在 NextJS SSR 流程中何时以及如何调用 React 生命周期方法?
- asp.net - LINQ to Entities 无法识别其中的方法调用
- angular - 刷新页面后没有得到相同的结果
- c# - 使用 WrapWithOverflow 调整 WPF TextBlock 的大小
- node.js - 带有 http 调用的 Oclif 动态自动完成功能
- javascript - forEach zero pads a string, but map does not
- javascript - 超链接 div 内的按钮