Jump to content
Jason Huck

main.inc, url segments, and Wire404Exception()

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?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

That's '"strange", probably something else is going on then. Maybe other, more experienced PHP developers can join in and help as I've never experienced something like this.

Share this post


Link to post
Share on other sites

Maybe adding ob_get_clean() and exit or $this->halt() would help before adding the exception. You could also set a custom 404 view file and set the 404 header manually.

Share this post


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

Share this post


Link to post
Share on other sites

Thanks for the report! Yeah, the default ProcessWire profiles do use the basic-page template for 404 too, and I recon this can lead to problems when it is forgotten. I did not think of it either in this case.

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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.
    • By MilenKo
      Hello my friends. Today I started working on my recipe website again and it was the turn to show recipes (pages) that has a specific category assigned in a field.
      In my NowKnow project for categories I used a parent page where inside of it I had the children and everything seemed to be super easy. This time, however, I decided to change the approach so I created a parent page Recipe categories and assigned to it my 'category' template. Inside the parent I added a few categories that I want to be able to select via PageReferrence field 'recipe_category'.
      The parent template would show all the categories represented by a title and an image - that part is done and works fine. 
      Now what I am trying to achieve is to have a few recipes having the 'recipe_category' field equal to Bakery for example, and then when I point to the category URL to get only the recipes that have Breakfast selected in. From what I know the perfect approach to achieve that would be to use $input->urlSegment as to select the name of the category from the URL and then filter the pages adding to selectors: recipe_category=$category.  Following Ryans earlier instructions about the urlSegment and an example found here in the forum, I got this code to fit my fields names:
      <?php if($input->urlSegment1 == 'category' && $input->urlSegment2) { $name = $sanitizer->pageName($input->urlSegment2); $category = $pages->get("template=categories-list, title=$name"); if($category->id) { $q = $pages->find("template=recipes-inner, recipe_category=$category"); } } ?> After adding the code, I enabled the URL segments for both: 'category' and 'category-list' templates. Browsing the URL for the Bakery category:
      http://food.pw/category/bakery/ (the domain name is not a typo, but PW is on my local server) I was supposed to get the $category to get the value of 'bakery'. However instead of that I am getting nothing.
      What am I missing in the big picture as I am sure it is again something silly but I spent almost the whole day trying to figure it out and still got no progress?
      P.S. trying to change the urlSegment number to 3 did not help either
×
×
  • Create New...