Jump to content

Can't throw 404 in autoload module


Recommended Posts

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 by JayGee
typo
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...