Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

[Dúvida] Não consigo definir o Psr17Factory nem o ServerRequestInterface

Warning: Undefined variable $request in C:\Users\User\Desktop\projetos php\psrs\public\index.php on line 39

Fatal error: Uncaught TypeError: Alura\Cursos\Controller\FormularioInsercao::processaRequisicao(): Argument #1 ($request) must be of type Psr\Http\Message\ServerRequestInterface, null given, called in C:\Users\User\Desktop\projetos php\psrs\public\index.php on line 39 and defined in C:\Users\User\Desktop\projetos php\psrs\src\Controller\FormularioInsercao.php:11 Stack trace: #0 C:\Users\User\Desktop\projetos php\psrs\public\index.php(39): Alura\Cursos\Controller\FormularioInsercao->processaRequisicao(NULL) #1 {main} thrown in C:\Users\User\Desktop\projetos php\psrs\src\Controller\FormularioInsercao.php on line 11

<?php

require __DIR__ . '/../vendor/autoload.php';

use Alura\Cursos\Controller\InterfaceControladorRequisicao;
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();
}

session_start();

/**$ehRotaDeLogin = stripos($caminho, 'login');
if (!isset($_SESSION['logado']) && $ehRotaDeLogin === false) {
    header('Location: /login');
    exit();
}**/

$psr17Factory = new Psr17Factory();

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

$serverRequest = $creator->fromGlobals();

$classeControladora = $rotas[$caminho];
/** @var InterfaceControladorRequisicao $controlador */
$controlador = new $classeControladora();
$resposta = $controlador->processaRequisicao($request);

foreach ($resposta->getHeaders() as $name => $values) {
    foreach ($values as $value) {
        header(sprintf('%s: %s', $name, $value), false);
    }
}

echo $resposta->getBody();
<?php

namespace Alura\Cursos\Controller;

use Nyholm\Psr7\Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class FormularioInsercao implements InterfaceControladorRequisicao
{
    public function processaRequisicao(ServerRequestInterface $request): ResponseInterface
    {
        $html = 'Teste';
        return new Response(200, [], $html);
    }
}

Obs. Instalei todas as dependências, não consigo identificar o motivo.

1 resposta
solução!

Cade????