One of my plugins does some processing and has to generate a 404 response to the user. How can this be done so the user sees the standard error page from the error plugin? I tried to do throw new \RuntimeException("Unable to load catalog resource ", 404); but this will not show the error plugin.
Community guidelines
Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.
Archive
Hi @hmaier,
the cleanest way to issue a 404 error from within a plugin is by calling the onPageNotFound event i.e.
PHP
$this->grav->fireEvent('onPageNotFound');
This will trigger the error page from the error plugin. If you want to show a customized error message you may include the following snippet into your plugin
PHP
$event = $this->grav->fireEvent('onPageNotFound');
$msg = "Unable to load catalog resource.";
if (isset($event->page)) {
$event->page->content($msg);
} else {
throw new \RuntimeException($msg, 404);
}
This is just one way implementing a custom error message in Grav. There are others and differ in the difficulty e.g. you can provide a custom error template wih a custom error md page and link to that or implement you own error page manager... Really there are no limits :-)
Log in to reply.
Suggested topics
| Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| 0 | 1280 | 9 years ago | ||
| 2 | 888 | 9 years ago | ||
| 2 | 4016 | 9 years ago | ||
| 1 | 2892 | 9 years ago | ||
| 3 | 1076 | 9 years ago |