首页 > 解决方案 > 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 中抛出

标签: phpxamppslim

解决方案


我在 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();

推荐阅读