SamC Posted December 14, 2017 Share Posted December 14, 2017 I'm writing a tutorial about URL segments and I have it working like this (ignore overly verbose comments). Cats (cat-index template) - cat 1 (cat-entry template) - cat 2 - cat 3 Type (cat-attribute template, each child is a page ref field on cat-entry template) - Burmese (cat-attribute template) - Tabby - Persian - etc. Invalid is 2nd URL segment and anything else in URL segment 1 that isn't the page name under /type/. Valid /cats/tabby/ /cats/persian/ etc... Invalid /cats/first/ /cats/first/second/ <?php // only 1 URL segment should be allowed if ($input->urlSegment2) { throw new Wire404Exception(); } // create a string that will be our selector $selector = "template=cat-entry"; // get URL segment 1 $segment1 = $input->urlSegment1; // if there is a URL segment 1 if ($segment1) { // get array of cat type page names $catTypes = $pages->get("/type/")->children->each("name"); // name may be assumed here, not sure // if URL segment 1 is in cat types array if (in_array($segment1, $catTypes)) { // add this to the selector $selector .= ",catType=$segment1"; } else { // invlid URL segment 1 throw new Wire404Exception(); } } // find the pages based on our selector $catPages = $pages->find($selector); foreach ($catPages as $catPage): ?> Now this works, 404 for anything invalid. But I was looking at https://processwire.com/api/ref/wire-array/has/ and it seems to be similar. Is the code above a suitable way to achieve this? (bearing in mind this is a tutorial so trying to stick with best practices) And how would I use has() in the above example? Not 100% how to use that method i.e. is ->has() used inside a loop? (unfortunately can't link to the actual tut as it's unpublished) Link to comment Share on other sites More sharing options...
Robin S Posted December 14, 2017 Share Posted December 14, 2017 I would do the validation a little differently, to avoid loading any more pages than you need to: // if there is a URL segment 1 if($segment1) { // get catType page $catType = $pages->findOne("parent=/type/, name=$segment1"); // if catType page exists if($catType->id) { // add this to the selector $selector .= ", catType=$catType"; } else { // invalid URL segment 1 throw new Wire404Exception(); } } Not sure in what way you were considering using has(), but that method is for checking against a PageArray/WireArray that is already loaded to memory - better to load just the page (category) you want than to load many just to filter it down again. 1 1 Link to comment Share on other sites More sharing options...
SamC Posted December 14, 2017 Author Share Posted December 14, 2017 6 minutes ago, Robin S said: // get catType page$catType = $pages->findOne("parent=/type/, name=$segment1"); Absolutely this, I like it! 6 minutes ago, Robin S said: Not sure in what way you were considering using has(), but that method is for checking against a PageArray/WireArray that is already loaded to memory - better to load just the page (category) you want than to load many just to filter it down again. I couldn't work out the use for it, I see now. Thanks 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