Jump to content

Recommended Posts

Posted

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.

Posted

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
Posted

$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
Posted

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.

Posted

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

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

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
  • Recently Browsing   0 members

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