JayGee Posted September 25, 2019 Share Posted September 25, 2019 I'm using URL segments to pull some dynamic content into a page. The template uses the URL segment variable to check the content exists in another part of the site tree, if not I'm trying to throw a 404. Looking at other threads on here I think the below should do the job. throw new PageNotFoundException(); But I'm getting the following error: Fatal Error: Uncaught Error: Class 'PageNotFoundException' not found in... Is anyone able to shed any light on how I'm using this wrong? The code is currently in an include which seemed to have previously triggered some debate, but I get the same error even if I move it to the top of the parent page. I've also created a plain 404 template that doesn't include any other template elements just to rule out a code loop. Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 Have you seen this tutorial/guide? https://processwire.com/docs/front-end/how-to-use-url-segments/#best-practices 1 Link to comment Share on other sites More sharing options...
JayGee Posted September 25, 2019 Author Share Posted September 25, 2019 13 minutes ago, wbmnfktr said: Have you seen this tutorial/guide? https://processwire.com/docs/front-end/how-to-use-url-segments/#best-practices You're my guru today! Lol! ? Yes have seen this guide and have used segments loads of times. I know the script it working and breaking at the right places because if I drop the throw new PageNotFoundException(); call and just echo out a message instead it works fine. It's just the 404 call that seems to error. Doesn't seem to be able to find the class. Here's the snippet if it helps: //URL Routing for category pages and tags if($currentTemplate == 'category') { ################################################### ## Routing only required for category and tag pages ################################################### //Segment 1 == category/sector $currentCat = $sanitizer->text($input->urlSegment1); //Segment 2 == content tag e.g. marketing $currentTag = $sanitizer->text($input->urlSegment2); //Check requested category exists (in hidden categories structure) //404 if not, otherwise continue rendering page $catPage = $pages->get('/categories/')->children("name=$currentCat,include=hidden,limit=1"); $catPage = $catPage[0]; if(!$catPage->id) { throw new PageNotFoundException(); } } Probably doing something stupid. Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 You use PageNotFoundException() while I only know and see Wire404Exception() in examples. Does it make any difference? Additional sidenote: Very old (2010 and 2013) thread but still... interesting. Link to comment Share on other sites More sharing options...
JayGee Posted September 25, 2019 Author Share Posted September 25, 2019 2 minutes ago, wbmnfktr said: You use PageNotFoundException() while I only know and see Wire404Exception() in examples. Does it make any difference? Nope, gives the same error: Fatal Error: Uncaught Error: Class 'Wire404Exception' not found in... Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 So... I just played with it a bit. Does NOT work: throw new PageNotFoundException(); Does Work: throw new Wire404Exception(); Tested it on a ProcessWire 3.0.133 installation. Are you maybe running the DEV 3.0.140 version? 1 Link to comment Share on other sites More sharing options...
JayGee Posted September 25, 2019 Author Share Posted September 25, 2019 2 minutes ago, wbmnfktr said: So... I just played with it a bit. Does NOT work: throw new PageNotFoundException(); Does Work: throw new Wire404Exception(); Tested it on a ProcessWire 3.0.133 installation. Are you maybe running the DEV 3.0.140 version? Interesting - yes am running the latest Dev version for this project as want to play with the new cookies api ? - shouldn't be an issue should it? Unless something has been changed/deprecated? Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 The 3.0.140 brought some issues with 3rd party modules and I try to avoid the DEV version at the moment for that reason. Therefore this was just a guess but maybe the DEV version also affects core features like this. Try the latest Master (3.0.123) if possible. If that's the fix we have found the suspect. By now your code snippet seems fine to me - at least the modified (other if statements and Wire404Exception) version works as expected here. 1 Link to comment Share on other sites More sharing options...
JayGee Posted September 25, 2019 Author Share Posted September 25, 2019 1 minute ago, wbmnfktr said: The 3.0.140 brought some issues with 3rd party modules and I try to avoid the DEV version at the moment for that reason. Therefore this was just a guess but maybe the DEV version also affects core features like this. Try the latest Master (3.0.123) if possible. If that's the fix we have found the suspect. By now your code snippet seems fine to me - at least the modified (other if statements and Wire404Exception) version works as expected here. Yes, you're probably right. I do usually use Master for most projects, just working on something that allows for a little more experimentation. I'll try it on another install and see if it is the dev version causing this. Thanks for your assistance (again!) @ryan - don't know if you can think of anything off the top of your head in the latest dev that might cause the issues above? If I can confirm it later in another install I log it on github. Link to comment Share on other sites More sharing options...
alxndre Posted September 25, 2019 Share Posted September 25, 2019 The same error is being discussed in this thread where throwing a 404 exception in an include file throws an exception (well, a different one). In that thread it seems that there are different scenarios that can trigger this. What I'd suggest right now is to install the Tracy module if you haven't yet to give you a better look at what's happening. 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 3.0.140 works fine in this case. Just tested it here with wire404Exception(). PageNotFoundException() doesn't work - tested that as well. Can you please doublecheck $catPage. Maybe... I don't know... there is a glitch. Link to comment Share on other sites More sharing options...
BitPoet Posted September 25, 2019 Share Posted September 25, 2019 1 hour ago, Guy Incognito said: It's just the 404 call that seems to error. Doesn't seem to be able to find the class. Is your include file namespaced? If not, throw new \ProcessWire\PageNotFoundException(); should do the trick. Or adding "namespace ProcessWire;" at the top, obviously. 2 Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 Good advice. For the record: In my testing site namespace wasn't set. Just added it. Works the same. No difference. Link to comment Share on other sites More sharing options...
JayGee Posted September 25, 2019 Author Share Posted September 25, 2019 12 minutes ago, BitPoet said: Is your include file namespaced? If not, Ok - good call on the namespacing. Can confirm the following combo of using wire404Exception() instead of PageNotFoundException() and namespacing works as follow: throw new \ProcessWire\wire404Exception(); Thanks @wbmnfktr and @BitPoet - got there in there end! 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 25, 2019 Share Posted September 25, 2019 That's wild... why is there such a different behaviour? Nonetheless... it's working now. ? Link to comment Share on other sites More sharing options...
szabesz Posted September 25, 2019 Share Posted September 25, 2019 7 hours ago, wbmnfktr said: The 3.0.140 brought some issues with 3rd party modules and I try to avoid the DEV version at the moment for that reason. Currently I stick to 3.0.138, looks like that is the last DEV version safe to update to. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now