Jump to content
owzim

404 when template file not present

Recommended Posts

Hi,

is there a way to change that behavior? Can I hook into that somehow?

 

Thanks!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Maybe it would help to know a little more about your scenario. Perhaps you could try Page::viewable - it checks to see if a template file exists?

  • Like 2

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I am worried that there won't be any 404 responses to any requests to your site though. It still feels like it's the wrong solution to whatever the problem is, but maybe I am not understanding the use case.

  • Like 1

Share this post


Link to post
Share on other sites

I want to render pages without the need of a template file. 404s still work.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
21 minutes ago, owzim said:

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

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? 

  • Like 1

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...