Jump to content

[Solved] Hook ProcessPageView::pageNotFound - rendering other page


esl
 Share

Recommended Posts

Hello!

I'm trying to render a specific page (based on the URL) if the page is not found, using a hook in my module. But now I am faced with the fact that I can not use the "echo $p->render()" inside the hook because of incompatibilities with other modules (MarkupSEO).

How and where do I hook "Page::render" to avoid rendering directly inside hook "ProcessPageView::pageNotFound".

In short, I need to catch 404 error and instead of rendering error page - render another page with a status of 200, while using the hook "Page::render".

public function init() {
    parent::init();
    $this->addHookBefore('ProcessPageView::pageNotFound', $this, 'checkForceURLs');
}

public function checkForceURLs($event) {
    $event->replace = true;
    // ... Defining $p - page i need to render
    echo $p->render();
    return;
}

 

Link to comment
Share on other sites

Maybe I am not fully understanding, but it sounds like maybe you just want a $session->redirect($p->url).

Although be aware of redirecting 404's to valid pages in general - are you limiting this to certain pages for a specific reason?

Link to comment
Share on other sites

Just now, adrian said:

Maybe I am not fully understanding, but it sounds like maybe you just want a $session->redirect($p->url).

Although be aware of redirecting 404's to valid pages in general - are you limiting this to certain pages for a specific reason?

No, Adrian, I need to render a specific page on a non-existent address, with the address should not change.

Link to comment
Share on other sites

I guess I am not sure what the problem with the render approach is then. You say you want a 200 header. Can you add this before render()

header("HTTP/1.1 200 OK");

I don't know what the incompatibility with the MarkupSEO module is, but maybe you can workaround it by setting the priority of your hook. See an example in my 404Search module: https://github.com/adrianbj/Process404Search/blob/4d9bd7a773d14bc29524c0798ad500050d969685/Process404Search.module#L58

  • Like 1
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...