Best practices

When you have URL segments enabled for your template, any segments can be used to arrive at the same page. As a result, it is a best practice to respond to the segments you want, and deny all others. We recommend that you trigger a "404 not found" error when you come across an unrecognized URL segment. Lets take the example from the previous section and modify it to account for this:

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

if($input->urlSegment1 == 'photos') {
  // display photo gallery

} else if($input->urlSegment1 == 'map') {
  // display map

} else if($input->urlSegment1) {
  // unknown URL segment, send a 404
  throw new Wire404Exception();

} else {
  // display main content
}

Another way of doing the same thing is to use PHP's switch statement:

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

switch ($input->urlSegment1){
  case '':
    // Segment 1 is empty so display main content
    break;

  case 'photos':
    // Display photo gallery
    break;

  case 'map':
    // Display map
    break;

  default:
    // Anything else? Throw a 404
    throw new Wire404Exception();
}

Arguably this method is easier to follow and to add further options, but it is a matter of personal preference.

Next: Page numbers and URL segments »


  1. What are URL segments?
  2. How and where to use them
  3. Best practices
  4. Page numbers and URL segments

Comments

  • Chris

    Chris 3 years ago 32

    These url segments, although cleaner, seem just like a query string. how do they differ?

    • ryan

      ryan 3 years ago 182

      Some reasons to use URL segments rather than query strings:

      1. Pages rendered from URL segments can be cached, whereas pages rendered as the result of a query string cannot.

      2. URL segments are search engine friendly, whereas query strings are not necessarily.

      3. URL segments are sanitized automatically, since they have to follow PW page name format, whereas query strings are not.

      4. URL segments are nicer to look at, easier to read, and easier to type.

      But should you replace your use of query strings with URL segments? Nope. I would always still use query strings in these situations:

      1. The potential values you accept are unlimited. You would not want such values to benefit from caching (per item 1 above) since a cache with no limits can fill up a drive.

      2. You are in an environment where things like search friendliness and readability aren't really a factor. For instance web applications with a logged in user is likely to derive more benefit from using query strings.

      3. You need true "key=value" relationships with variables. Keep in mind URL segments are just the 'value' part. You might translate /key1/value1/key2/value2, etc., but that's more work and things to keep track of than a nice simple key=value query string.

      4. One of the biggest: if you need to allow values outside PW page name format. Keep in mind PW page names are limited to this set of characters: -_.a-z0-9. So you aren't going to want to use URL segments for a text search engine for example.

      5. The quantity of variables in the URL is unknown. URL segments are naturally limited to just a few. Whereas you could have any number of variables present in a query string.

      • Chris Falkenstein

        Chris Falkenstein 3 years ago 63

        Ryan, thanks for the informative response. and thank you for your generous offering here, pw. The more I use it the more I like it. Keep up the excellent work.

  • Alan

    Alan 3 years ago 11

    Coming from WordPress it seems to me this Would be an optimal way to use category/tags. But what can be a better use for this? =)

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.