Jump to content

main.inc, url segments, and Wire404Exception()


Jason Huck
 Share

Recommended Posts

I have a project that uses a variant of the "main.inc" template strategy. All templates are set to use a single "main.php" file. That file uses output buffering to include page-specific views and insert them into a "base" template. Generally, this works great and allows me to structure my files exactly the way I want. However, I've found that if I want to manually throw a Wire404Exception, it just bubbles up uncaught rather than being handled.

The 404 handler works fine in instances where I'm not calling it manually, but fails otherwise, specifically when I'm dealing with urlSegments. I'm not that familiar with PHP's exception handling in general nor ProcessWire's exception handling in particular, so I'm not sure how to further troubleshoot.

Some abbreviated pseudo-code to illustrate:

main.php:

ob_start();
include('./views/'.$view.'/'.$view.'.inc');
$layout = ob_get_clean();

ob_start();
include('./views/base/base.inc');
$template = ob_get_clean();

echo $template;

With this setup, if I try to throw a Wire404Exception within any "view", the exception isn't handled, so logged in admins see the error trace, and regular users get an ISE. e.g.,

view.inc:

if($input->urlSegment1){
    // look for stuff...
    if(!$match){
        throw new Wire404Exception();
    }
}

Any thoughts?

Link to comment
Share on other sites

Hi Jason, I think it must be related to PHP"s output buffering, which I do not use so sorry for not being able to help more here, but I guess you should examine the way it works on the server in question.

Note that I opted for wireRenderFile() to do the "buffering", that is to store rendered template partials in variables so that the complete page can be assembled later on.

Link to comment
Share on other sites

4 hours ago, szabesz said:

Hi Jason, I think it must be related to PHP"s output buffering, which I do not use so sorry for not being able to help more here, but I guess you should examine the way it works on the server in question.

Note that I opted for wireRenderFile() to do the "buffering", that is to store rendered template partials in variables so that the complete page can be assembled later on.

That was a good thought, but replacing output buffering with wireRenderFile() didn't change the behavior.

Link to comment
Share on other sites

Got this sorted by moving the 404 page to its own, separate template. In this particular project, I had set the basic-page template to allow urlsegments. The 404 page relies on those as well, to display the original URL while showing the 404 page, so my custom logic was conflicting with the default routine. I'm sure there was a good chance of creating some recursion issues with that setup, too.

 

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

  • Similar Content

    • By SwimToWin
      I have a nested page structure that fails for users without superuser permissions:
      Works for superusers / non-superusers:
          - foo
          -- bar
          --- page (status: published)
          ---- page (status: published)
          --- page (status: published)
      Fails for non-superusers (Works for superusers):
          - foo
          -- bar
          --- page (status: published)
          ---- page (status: published)
          --- page (status: unpublished <- apparently the template structure fails when there's one unpublished page)
      Template:
      <?php foreach ($page->children('include=all') as $p): #Fails for non-superusers ?> <?=$p->render()?> <?php endforeach; ?>     a) Works for non-superusers when I grant Page Edit permissions (on the template) to their assigned role/s.
          b) When I remove 'include=all' or 'include=unpublished' then it also works for non-superusers:
      <?php foreach ($page->children() as $p): #Works for non-superusers ?> <?=$p->render()?> <?php endforeach; ?> Error message (non-superusers)
          Internal Server Error
              The server encountered an internal error or misconfiguration and was unable to complete your request.
              Error has been logged.
          /www/site/assets/logs/errors.txt
              /foo/bar/baz/    Error:     Exception: Page '/foo/bar/baz/quz/' is not currently viewable.
          /www/site/assets/logs/exceptions.txt
              /foo/bar/baz/    Page '/foo/bar/baz/quz/' is not currently viewable. (in /wire/modules/PageRender.module line 410)
      Debug (non-superusers)
          Error: Exception: Page '/foo/bar/baz/quz/' is not currently viewable. (in wire/modules/PageRender.module line 410)
          #0 wire/core/Wire.php (397): PageRender->___renderPage(Object(HookEvent))
          #1 wire/core/WireHooks.php (823): Wire->_callMethod('___renderPage', Array)
          #2 wire/core/Wire.php (465): WireHooks->runHooks(Object(PageRender), 'renderPage', Array)
          #3 wire/core/WireHooks.php (924): Wire->__call('renderPage', Array)
          #4 wire/core/Wire.php (465): WireHooks->runHooks(Object(Page), 'render', Array)
          #5 site/templates/template.php (191): Wire->__call('render', Array)
       
    • By pwFoo
      How to enable urlSegments runtime instead of persistent in template settings? And where to be loaded before the current page returns a 404 error?
      https://processwire.com/docs/front-end/how-to-use-url-segments/#how-do-you-enable-url-segments
       
    • By iNoize
      Hello, 
      i have an Photography page based on PW. Now is the Problem that the loading time is to long.
      Idea was to split every 20 images in to pages for Ajaxloading. 
      I dont know how generate all 20 images a new URLsegment.
      I just cant  get it.  
      Thanks for advice 
    • By Dennis Spohr
      Hi all,
      with 
      $config->adminEmail it's possible to send out an email for fatal errors (for example a syntax-error).
      It would be nice to get also an email in case of an unhandled exception. There could be a situation of an wrong upload and exceptions on the live-page.
      Is this possible?
      Also it could be a very handy feature to be able to choose specific logs which are sent our via email automatically.
      Thanks for your feedback,
      Dennis
    • By foxcraft_aw
      Hello!
      I have a strange problem with the "delete" method of the "pages" API-variable: When I insert a parent- and a child-page, and then immediately try to delete first the child- and then the parent-page, an exception is thrown telling me that the parent cannot be deleted, because it still contains a child-page.
      You can reproduce this behaviour using the following code:
      <?php require 'index.php'; $parentPage = new ProcessWire\Page; $parentPage->name = 'parent'; $parentPage->template = 'basic-page'; $parentPage->title = 'parent'; $parentPage->parent = $pages->get('/'); $parentPage->save(); $childPage = new ProcessWire\Page; $childPage->name = 'child'; $childPage->template = 'basic-page'; $childPage->title = 'child'; $childPage->parent = $pages->get('/parent'); $childPage->save(); $pages->delete($pages->get('/parent/child')); $pages->delete($pages->get('/parent')); (I put this code into a file called "experiments.php" in the root directory of the site, because I do not know of any better way for quickly testing out code in ProcessWire yet)
       
      Can you tell me why this code throws an exception?
      What am I missing?
      I am using ProcessWire 3.
      Thanks for your help!
       
      PS: Yes, I know about the second parameter in the "$pages->delete" method, but it should not be necessary to be set in this scenario if I`m correct.
×
×
  • Create New...