JayGee Posted June 21, 2024 Posted June 21, 2024 (edited) I have an autoload module with the follow (abridged) code. The problem is that I cannot seem to throw the 404 error below. There's no page output prior to this point, so I can't see why this isn't working. Hoping someone else can! Hook via init() <?php public function init() { $this->addHookBefore('Page::render', $this, 'renderOutput'); } my function: <?php public function renderOutput($event) { $variant = 'defaults'; if ($this->input->urlSegment1) { if (array_key_exists($this->input->urlSegment1,$allVariantData)) { $variant = $this->input->urlSegment1; } else { throw new Wire404Exception(); //throw 404 to prevent made up variants resolving dynamic default data for 'pages' that don't exist } } } I've taken out the code not relevant to the issue but can confirm all the other logic and that around the url segments is all running as expected. I just get an exception when I try to call the 404 exception! Edited June 21, 2024 by JayGee typo
JayGee Posted June 21, 2024 Author Posted June 21, 2024 DW - Found it ?♂️ Had to add some logic to detect if we're already on the 404 page as my array check was broken because the data isn't present on the 404 (created a loop). if (array_key_exists($this->input->urlSegment1,$allVariantData)) I'm now doing if($page->id != 27) { //Wrap in condition to ensure this logic doesn't affect 404 pages is this the best way to detect a 404 page? Does the 404 ID ever change, or is this solid enough to rely on?
BitPoet Posted June 21, 2024 Posted June 21, 2024 11 minutes ago, JayGee said: Does the 404 ID ever change, or is this solid enough to rely on? It's not likely to change without the developer consciously doing so, but $config->http404PageID is much more expressive than "27" anyway. 2
JayGee Posted June 24, 2024 Author Posted June 24, 2024 On 6/21/2024 at 4:08 PM, BitPoet said: It's not likely to change without the developer consciously doing so, but $config->http404PageID is much more expressive than "27" anyway. Thanks @BitPoet - exactly what I was looking for. ?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now