As this class is an utilitary class that proposes functions to display an error view, you should make this function static, and add a new Environment: $twig parameter;
As this class is an utilitary class that proposes functions to display an error view, you should make this function static, and add a new `Environment: $twig` parameter;
vivien.dufour marked this conversation as resolved
You can use $this->twig->display instead of echoing the result of render :
```diff
- echo $this->twig->render('error.html.twig', $error);
+ $this->twig->display('error.html.twig', $error);
```
vivien.dufour marked this conversation as resolved
This is not easily extensible, if we want to add more error codes and/or message, we will need to modify this file.
You can simply do something like that :
- {% if error == "404" %}
- <h2>Erreur 404 : cette page n'existe pas</h2>
- {% elseif error == "401" %}
- <h2>Erreur 401 : vous devez vous authentifier afin d'accèder à cette page</h2>
- {% elseif error == "403" %}
- <h2>Erreur 403 : vous n'avez pas les autorisations nécessaires pour accèder à cette page</h2>
- {% endif %}
+ <h2>{{ error_type }} : {{ error_message }} </h2>
You'll then need to pass those two new variables to the view's argument :
This is not easily extensible, if we want to add more error codes and/or message, we will need to modify this file.
You can simply do something like that :
```diff
- {% if error == "404" %}
- <h2>Erreur 404 : cette page n'existe pas</h2>
- {% elseif error == "401" %}
- <h2>Erreur 401 : vous devez vous authentifier afin d'accèder à cette page</h2>
- {% elseif error == "403" %}
- <h2>Erreur 403 : vous n'avez pas les autorisations nécessaires pour accèder à cette page</h2>
- {% endif %}
+ <h2>{{ error_type }} : {{ error_message }} </h2>
```
You'll then need to pass those two new variables to the view's argument :
```php
$twig->display("error.html.twig", ["error_type" => $error_type, "error_message" => $error_message])
```
vivien.dufour marked this conversation as resolved
maxime.batista
requested review from maxime.batista 1 year ago
Add an error view for error 404, 401 and 403
$router->setBasePath($basePath);
$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig);
$errorController = new \App\Controller\ErrorController(new FormResultGateway($con), $twig);
No need to instanciate a this class here
class ErrorController
{
private FormResultGateway $gateway;
why use a gateway in something that only displays an error view ?
Moreover, it is not used anywhere in the class, you should remove it
/**
* @param FormResultGateway $gateway
*/
public function __construct(FormResultGateway $gateway, Environment $twig)
Remove the constructor, the
$twig
will be passed to the displayError function directly, and made static.$this->twig = $twig;
}
public function displayError($error) {
As this class is an utilitary class that proposes functions to display an error view, you should make this function static, and add a new
Environment: $twig
parameter;public function displayError($error) {
$error = array("error" => $error);
try {
echo $this->twig->render('error.html.twig', $error);
You can use $this->twig->display instead of echoing the result of render :
<meta charset="UTF-8">
<title>Error</title>
{% block stylesheets %}
useless block use, you should remove it
h1 {
color: #da6110;
text-align: center;
margin-bottom: 15px}
h2 {
text-align: center;
margin-bottom: 15px;
margin-top: 15px}
<h2>Erreur 401 : vous devez vous authentifier afin d'accèder à cette page</h2>
{% elseif error == "403" %}
<h2>Erreur 403 : vous n'avez pas les autorisations nécessaires pour accèder à cette page</h2>
{% endif %}
This is not easily extensible, if we want to add more error codes and/or message, we will need to modify this file.
You can simply do something like that :
You'll then need to pass those two new variables to the view's argument :
I meant to request changes, not to approve it*.
4e94522295
to03945892df
1 year agovery good 👍
4aa2bde233
into master 1 year agoReviewers
4aa2bde233
.