How to send 404 from a package controller
Permalink
Hi Everyone
Please someone point me in the right direction.
I am building a simple package to redirect anyone attempting to access the login page from outside of a IP white list. For the most part I have done this. However, I can't seem to work out how to produce a 404 error page in the event that a user is not in the white list.
I have noticed that C5 does not redirect to a 404 page but rather replaces the template being loaded. I want to replicate this. I have managed to use PHP's header location but this is undesired as it will change the url.
This is the on_start() function (from my package controller) I have so far. It uses the 'on_before_render' hook.
Please someone point me in the right direction.
I am building a simple package to redirect anyone attempting to access the login page from outside of a IP white list. For the most part I have done this. However, I can't seem to work out how to produce a 404 error page in the event that a user is not in the white list.
I have noticed that C5 does not redirect to a 404 page but rather replaces the template being loaded. I want to replicate this. I have managed to use PHP's header location but this is undesired as it will change the url.
This is the on_start() function (from my package controller) I have so far. It uses the 'on_before_render' hook.
public function on_start() { Events::addListener('on_before_render', function($event) { $cID = $event['view']->controller->c->cID; $c = Page::getByID($cID); if($c->cID == 141){ // render 404 here } }); }
Thanks for your reply. I will consider middleware.
Your solution, however did not work. It changes the browser title but not the contents of the page. I still see the login form.
Your solution, however did not work. It changes the browser title but not the contents of the page. I still see the login form.
I _think_ if you want to render the actual 404 page, you need to load the PageNotFound controller, and return it's response. Otherwise you'd maybe redirect to the page_not_found page.
PS. I think ideally you'd want to use Middleware for this.