odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
446 stars 80 forks source link

getParsedBody #31

Closed ledahu closed 3 years ago

ledahu commented 4 years ago

Hi

i have a middleware `<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Slim\Views\Twig; use Symfony\Component\HttpFoundation\Session\Session;

final class OldInputMiddleware implements MiddlewareInterface {

private Twig $view;

private Session $session;
/**
 * @param $request
 * @param $response
 * @param $next
 * @return mixed
 */
public function __construct(Session $session,Twig $view)
{
    $this->session = $session;
    $this->view= $view;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{

    $tmp=$request->getParsedBody();
     var_dump($tmp);

    return $handler->handle($request);
}

}`

when i submit a form POST , the dump is empty , the getMethod is "GET"

`<?php

use App\Middleware\OldInputMiddleware; use App\Middleware\SessionMiddleware; use App\Middleware\UrlGeneratorMiddleware; use App\Middleware\ValidationErrorsMiddleware; use Selective\BasePath\BasePathMiddleware; use Selective\Validation\Middleware\ValidationExceptionMiddleware; use Slim\App; use Slim\Csrf\Guard; use Slim\Middleware\ErrorMiddleware; use Slim\Views\TwigMiddleware;

return function (App $app) {

$app->add(ValidationExceptionMiddleware::class); $app->add(Guard::class); $app->add(UrlGeneratorMiddleware::class);

$app->addRoutingMiddleware();

$app->add(TwigMiddleware::class);
$app->add(OldInputMiddleware::class); // EMPTY getParsedBody
$app->add(ValidationErrorsMiddleware::class);
$app->add(SessionMiddleware::class);
$app->add(BasePathMiddleware::class);
$app->addBodyParsingMiddleware();
$app->add(ErrorMiddleware::class);

};`

any idea ?

ledahu commented 4 years ago

add: if i add this middleware to the pure skeleton, after a failed login i didnt get the Post Var still empty !

odan commented 4 years ago

Have you checked the Content-Type?

https://www.slimframework.com/docs/v4/middleware/body-parsing.html#media-type-detection

ledahu commented 4 years ago

yes, as you can see, the bodyParsingMiddleware is in the stack. Try it, you will see that after LoginSubmitAction, the var_dump stay empty

odan commented 4 years ago

I have already seen that you have added the BodyParsingMiddleware. That's why my question was if you checked the "Content-Type" inside the request for one of the supported media type. The BodyParsingMiddleware supports only POST requests.

So it will expect the body data to standard form format (application/x-www-form-urlencoded) by default. If you are sending JSON to this route, then you need to set the Content-type header to application/json.

ledahu commented 4 years ago

i do not send json. I just want that after a post submit , the middleware push the fiels in addGlobals to let it set as default value in twig field is submit raise validation errors

so, in slim3 , i had a

`<?php

namespace App\Middleware;

class OldinputMiddleware extends Middleware { /**

now, to succesfull do it, i must set the session in the Action, but in theory i would like to get the parsedbody in a middleware may it come frome the use of responder->redirect ? the point i dont understant is that if i dump $request, i get the pasedbody array in it the dump (protected) but if i dump $request->getParsedBody it is empty

how to check the content-type by a dump ?

odan commented 4 years ago

After a HTTP redirect the HTTP method is GET (and not POST anymore), so the submitted form data will always be empty after a redirect. HTTP is designed like this. If you want to keep a state from the last request you have to store it for example in a session. As long as your form is in POST mode, you can render the form data directly into template again, without sessions. A "better" option would be to send only the form data via Ajax to the server for input validation and storage in the database. Generally I would try to avoid saving form data into a session, but I don't have enough context to give an accurate answer.

ledahu commented 4 years ago

It's just to let the user don't have to rewrite if form fail. By the way, ok, i understand that redirect is GET.

odan commented 4 years ago

Hi @ledahu Could you find a solution?