Jump to content

Issue with intercepting 404s and render another page based on URL


Jonathan Lahijani
 Share

Recommended Posts

I'm trying to intercept a 404 error page and have it render another page based on some general logic:

The following is in /site/init.php:

$this->addHookBefore('ProcessPageView::pageNotFound', function($event) {
  $url = $event->arguments('url');
  
  // ensure trailing slashes
  if($url[-1] !== "/") $this->session->redirect(substr(wire("config")->urls->root, 0, -1).$url."/");

  $p = wire('pages')->get("/sites/".wire('config')->site.$url);
  if($p->id) {
    $event->replace = true;
    $event->return = $p->render();
  } else {
    $event->replace = false;
  }

});

Given my setup, $p->render() will cause an error related to it not finding a custom function I have defined in my /site/templates/_init.php:

Call to undefined function myWeirdFunction() 

How do I properly render the file and make sure the chain of included files is correct?  This is always a little confusing to me.

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...