Cohaven
Cohaven

Reputation: 185

How to display error message to user in PHP MVC with Slim Router?

I'm using PHP with MVC pattern and the Slim 3 microframework for routing.

I need to show an error message to the user in the twig view. This error is regarding the username or password that they submitted being invalid.

The flow is like this:

  1. the Slim router accepts a GET request from the client on the "/login" route and sends it to the appropriate controller function which displays the login form.
  2. the Slim router accepts a POST request from the client on the "/login" route and sends it to the appropriate controller function for validation of user input and potentially authentication.

At this point I need to give user the feedback that their input was invalid. I can think of a few options, but I'm not sure if any of them are "optimal" or best practice.

  1. add the error message to the $_SESSION and redirect to the "/login" route, which will then display any error messages stored in the session in addition to the login form.
  2. add the error message to the Slim response (\Psr\Http\Message\ResponseInterface) and redirect to the "/login" route, which will then display any error messages stored in the request (or in args) in addition to the login form.
  3. execute the displayLoginForm($request, $response) controller function directly without doing any redirects, since the function is already in the same controller. the error message would be placed in the $request argument.

What would be the best practice method of handling this situation?

Upvotes: 0

Views: 278

Answers (1)

Pipe
Pipe

Reputation: 2424

A better solution would be to use the "Flash messages"

Documentation here: https://www.slimframework.com/docs/v3/features/flash.html

Upvotes: 0

Related Questions