2
respostas

Undefined type Nyholm\Psr7\Factory\Psr17Factory

Instalei as dependencias do Nyholm para criar request e responses, porém as mesmas não não reconhecidas pelo vscode, segue code abaixo:

Código da index

<?php

require_once __DIR__ . "/../vendor/autoload.php";

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;

$caminho = $_SERVER['PATH_INFO'];

$rotas = require __DIR__ . "/../config/routes.php";

if(!array_key_exists($caminho, $rotas)){
    http_response_code(404);
    exit();
}

// iniciado session em todas as requisições
session_start();

$rotaLogin = stripos($caminho,'login');

if(!isset($_SESSION['logado']) && $rotaLogin === false){
    header('Location: /login');
    exit();

}

$psr17Factory = new Psr17Factory();

$creator = new ServerRequestCreator(
    $psr17Factory, // ServerRequestFactory
    $psr17Factory, // UriFactory
    $psr17Factory, // UploadedFileFactory
    $psr17Factory  // StreamFactory
);

$request = $creator->fromGlobals();

$classe = $rotas[$caminho];
/** @var InterfaceControladorRequisicao $controlador */
$controlador = new $classe();

$response = $controlador->processaRequisicao($request);

echo $response->getBody();

Codigo após executar composer require nyholm/psr7-server

composer require nyholm/psr7-server
Using version ^1.0 for nyholm/psr7-server
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
ocramius/package-versions: Generating version class...
ocramius/package-versions: ...done generating version class
21 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
2 respostas

Meu composer.json abaixo:


{
    "autoload": {
        "psr-4": {
            "Alura\\Cursos\\": "src/"
        }
    },
    "require": {
        "doctrine/orm": "^2.6",
        "psr/http-message": "^1.0",
        "nyholm/psr7": "^1.3",
        "nyholm/psr7-server": "^1.0"
    }
}

Bruno, quando você executa a aplicação, qual a saída? Você recebe algum erro ou tudo funciona normal?