Redirect with error in Slim Framework

I want to redirect to the page (error.php or maybe 404 / 406.php, regardless of the error) depending on the information in the form on my website. I managed to register the following error:

if ($date > $curdate) {
    return $response
        ->withStatus(406)
        ->withHeader('Content-Type', 'text/html')
        ->write('You can\'t select dates in the future!');
}

How can I do this so that he sends you to a page with this error, in particular, instead of registering / requesting it on the network tab?

Edit for further explanation . Now I get the following:

Request URL:http://raspberrypi/chartAPI/api/{id}/{date1}/{date2}
Request Method:POST
Status Code:406 Not Acceptable
Remote Address:192.168.0.80:80
Referrer Policy:no-referrer-when-downgrade

It almost works as intended. What I want to do is send me to " http: // raspberrypi / chartAPI / error / 406 " (for example) and display the contents in a file called 406. php (or error406.php or whatever you want to name) .

Edit2: - :

return $response->withRedirect("error.php");

:

Request URL:http://raspberrypi/chartAPI/api/{id}/{date1}/error.php
Request Method:GET
Status Code:405 Method Not Allowed

slim :

Method not allowed. Must be one of: POST

{date2}? POST?

+3
2

NotFound

<?php
namespace App\Action;

use Slim\Handlers\AbstractHandler; 
use Slim\Views\Twig; 
use Psr\Http\Message\ServerRequestInterface; 
use Psr\Http\Message\ResponseInterface;

class CustomErrorHandler extends AbstractHandler {

private $view;

public function __construct(Twig $view) { 
    $this->view = $view; 
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { 
    parent::__invoke($request, $response);
    $status = $response->getStatusCode();

    $this->view->render($response, $status . '.twig');

    return $response->withStatus($status);
}

Slim v3, PSR-7 :

// $c is the DI container of Slim
$app->add(function ($request, $response, $next) use ($c) {
// First execute anything else
$response = $next($request, $response);

// Have multiple if for each status code or use a switch based on $response->getStatusCode()
if (404 === $response->getStatusCode()) {
    // A 404 should be invoked
    $handler = $c['customErrorHandler'];
    return $handler($request, $response);
}

// Any other request, pass on current response
return $response;

}

,

+1

Slim . Slim, , .

$slimErrorHandler = new Slim\Container;

// exception handler
$slimErrorHandler['errorHandler'] = function() {
    return function($request,$response,$exception) {
        $exceptionMessage = $exception->getMessage();
        $json = (array)json_decode($exceptionMessage);
        $error = array(
            'message' => $json['message'],
            'code' => $exception->getCode(),
            'errors' => (array)$json['errors']
        );

        $view = new Slim\Views\Twig(__DIR__);
        return $view->render($response,'error.html',$error);
    };
};

// page not found handler
$slimErrorHandler['notFoundHandler'] = function () {
    return function ($request,$response) {
        return $response->write('Page not found!');
    };
};

$app = new Slim\App($slimErrorHandler);

$app->get('/',function($request,$response) {
    $error = array(
        'message' => "Errors were found:",
        'errors' => array(
            "email" => "Please enter a valid email",
            "date" => "You can't select dates in the future!"
        )
    );

    throw new Exception(json_encode($error),409);
});

$app->run();

:

<h1>Whoops!</h1>

<p>{{ code }} : {{ message }}</p>

<ul>
    {% for error in errors %}
        <li>{{ error }}</li>
    {% endfor %}
</ul>
+1

Source: https://habr.com/ru/post/1675687/


All Articles