Jump to content

404 when template file not present


owzim
 Share

Recommended Posts

Thanks, unfortunately this gives my only ''access not allowed" as a $reason, so if I would overwrite the behavior via hook, based on that, I would mess with all kinds of access management.

Link to comment
Share on other sites

$reason parameter looks like it's for internal use

$page = $this->checkAccess($page); 
if(!$page || $_page->id == $config->http404PageID) {
    return $this->pageNotFound($_page, $this->requestURL, true, 'access not allowed');
}

Why do you need $reason for?, if you want to narrow down 404s to missing template files than you can use

wire()->addHookBefore('ProcessPageView::pageNotFound', function (HookEvent $e) {
    $page = $e->arguments('page');
    if ($page->id && !$page->template->filenameExists()) {
        // page exists, but template file does not
    }
});

 

  • Like 2
Link to comment
Share on other sites

Thanks,

ProcessPageView::pageNotFound is definitely the right spot, I came up with this method, don't know if it has any access rights implications:

public function hook_ProcessPageView_pageNotFound(HookEvent $event)
{
    $page = $event->arguments('page');

    $page->template->filename = 'path/to/existing/file';

    if ($page->viewable()) {
        $event->return = $page->render();
        $event->replace = true;
    }
}

Page render actually outputs something, because I hook into it. But just not with a template file.

Link to comment
Share on other sites

24 minutes ago, owzim said:

$event->return = $page->render();

4 minutes ago, owzim said:

I want to render pages without the need of a template file.

If you want to use $page->render(), then $page has to have a template file, but if a page is viewable (i.e. has a template file) you won't get a 404 error. I'm not sure you can have both.

If you want the  same template file for multiple templates, then you can set it so in template settings > File tab

  • Like 1
Link to comment
Share on other sites

Just now, Robin S said:

Instead of the hook, wouldn't it be better to set this as the "Alternate template filename" only on the templates you need it for? 

That is also an option I thought about, but this is for a module I am creating and I don't want users of that module to jump through those hoops.

Link to comment
Share on other sites

3 minutes ago, abdus said:

If you want to use $page->render(), then $page has to have a template file, but if a page is viewable (i.e. has a template file) you won't get 404 error. I'm not sure you can have both.

If you want to use same template file for multiple templates, then you can set it so in template settings > File tab

The rendering ($page->render()) is taken care of elsewhere.

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