This is not a question about changing or preprocessing the twig template of the 403 page.
I have a controller with an access
function called by the _custom_access
key of my route.
In some circumstance, this function is returning AccessResult::forbidden("a good reason");
which lead to my default 403
page.
Most of the case, this is the behavior I would like to have, but in few cases, I would like to have another title and another message for this page like "You can not access because you eat too much chocolate"
Should I create a new custom error route and send a new response to this route from my access
function? (hint: I tried without success)
Any idea?
Edit to add: Thanks to 4k4, I have added a custom ExceptionHtmlSubscriber
extending DefaultExceptionHtmlSubscriber
in order to override the on403()
method.
Here is my code:
class TXSExceptionHtmlSubscriber extends DefaultExceptionHtmlSubscriber {
/**
* {@inheritdoc}
*/
public function on403(ExceptionEvent $event) {
$my_condition=extract_from_event($event);
if($my_condition){
// I am not sure it is what should be done here
$this->makeSubrequest($event, '/my_custom_error_path/my_custom_error_code', Response::HTTP_FORBIDDEN);
}else {
parent::$this->on403($event);
}
}
/**
* {@inheritdoc}
*/
protected static function getPriority() {
return 1000;
}
}
So far, I have 2 questions:
1-Unfortunately my custom on403()
method is never called (on the other hand, the on403()
method DefaultExceptionHtmlSubscriber
is called): What I am missing?
2-Let say that my custom on403()
method is called: what should be the code to redirect the user on my custom 403
page containing my custom message (ie: it could be nice to display the reason of the AccessResult AccessResult::forbidden("a good reason");
but in few cases
– What cases exactly? What conditions?