Jump to content

Check to see if page exists...


LedZepmann
 Share

Recommended Posts

Thanks to @Joss, I'm now using URL Segments to populate my page content with local information by county.

So for example, neighborhoodsewerdrain.com renders the base site, and neighborhoodsewerdrain.com/macomb/ will pull in the county name, local phone number, etc. Fantastic!

I created a template with no template file, and pages for individual counties that use this template. Each page has fields that hold the title, county name, page content, and county phone number. The title matches the URL Segment I want to target.

This is the code in my _init.php file that modifies template content and I'm using similar Hanna Code for page content:

if($input->urlSegment1) {
	$localpath = str_replace("","","$input->urlSegment1"); //adds url segment to navigation path
	$localphone = $pages->get("/service-area/counties/$input->urlSegment1/")->countyphone;
	$localphoneshort = str_replace("-","","$localphone");
	$namecounty = $pages->get("/service-area/counties/$input->urlSegment1/")->countyname;
}else{
	$localpath = ""; //if no urlSegment1, then add nothing to navigation path
	$localphone = $pages->get("/site-common/")->phone;
	$localphoneshort = str_replace("-","","$localphone");
	$namecounty = $pages->get("/service-area/counties/")->countyname;
}

Currently, if someone happened to type in "neighborhoodsewerdrain.com/foobar/," then "Foobar County" appears in the page content and nothing populates in the template - I don't want that!

What I would like to do is check to see if the urlSegment1 matches up with a county page, then execute the code above, and if not, then refresh the browser back to the base domain and remove the bogus url segment from the path displayed in the browser URL bar. Another option would be to throw a 404.  I have spent about 6 hours combing through forum posts and tutorials and (as a newbie) came up with some partial solutions - just need a little help to solve it completely.

Thanks!

Link to comment
Share on other sites

Hey @Ledzepman,

here is a good reading about how to deal with urlSegments: https://processwire.com/docs/tutorials/how-to-use-url-segments/

And if you not already know it, best search results do you get with google like this:

// searches the whole PW site
https://www.google.com/search?q=site:processwire.com+urlsegment

// only search in forum posts
https://www.google.com/search?q=site:processwire.com/talk+urlsegment

---

regarding to the check if a county page exists and taking the example from ryans tutorial, you can do it like this:

// we are only using 1 URL segment, so send a 404 if there's more than 1
if($input->urlSegment2) throw new Wire404Exception();

// now check if there is no urlSegment1
if($input->urlSegment1 == '') {
  // display main content
  ...

} else {

  // get you a PageArray of all county pages using a appropriate selector. This depends on how you have setup things
  $countyPages = $pages->get("/path/to/countyparentpage/")->children();

  // build an array of what you need to validate, titles or names ?  (I use their names in the example here)
  $validCounties = array();
  foreach($countyPages as $cp) {
    $validCounties[$cp->name] = $cp->name;
  }

  // now check if it is a valid county
  if(isset($validCounties[$input->urlSegment1])) {
    // display county content
    ...

  } else {
    // unknown URL segment, send a 404
    throw new Wire404Exception();
  }
}
Edited by horst
added example code
  • Like 4
Link to comment
Share on other sites

Thank you @horst!

Here is my final code and it's working perfectly:

$countyPages = $pages->get("/service-area/counties/")->children();
foreach ($countyPages as $cp) {
	$validCounties[$cp->title] = $cp->title;
}

if(isset($validCounties[$input->urlSegment1])) {
	$localpath = str_replace("","","$input->urlSegment1");
	$localphone = $pages->get("/service-area/counties/$input->urlSegment1/")->countyphone;
	$localphoneshort = str_replace("-","","$localphone");
	$namecounty = $pages->get("/service-area/counties/$input->urlSegment1/")->countyname;

} else if($input->urlSegment1 == '') {
	$localpath = "";
	$localphone = $pages->get("/site-common/")->phone;
	$localphoneshort = str_replace("-","","$localphone");
	$namecounty = $pages->get("/service-area/counties/")->countyname;

} else {
	$nunya = $pages->get("/http404/");
	$session->redirect($nunya->url);
} 

if($input->urlSegment2) {
	$nunya = $pages->get("/http404/");
	$session->redirect($nunya->url);
}

Note the session redirects - for some reason, the "throw new Wire404Exception();" is resulting in an internal server error rather than redirecting to the 404 page. So I came up with this work around.  Anyone with suggestions for correcting the problem of throw new Wire404Exception(); not working are welcome to comment here :)

Thanks again horst!

Link to comment
Share on other sites

  • 10 months later...

FWIW, I am seeing the same issue with throwing the exception. I suspect it has something to do with the fact that I'm using the "main.php" templating approach, as the first error I saw was from trying to redeclare a function that only appears at the top of main.php. Wrapping that in a function_exists() conditional results in a different error, at the point where I'm including the view for the current page:

    ob_start();
    include('./views/'.$view.'/'.$view.'.inc'); // 404's error on this line now
    $layout = ob_get_clean();

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

    echo $template;

Redirecting to the 404 page is (arguably) better than nothing, but ideally you'd just return a 404 status code directly on the originally requested URL.

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.
×
×
  • Create New...