Jump to content

Detect if a URL will hit a URL Segment


guy
 Share

Recommended Posts

Hey,

Is there an API method to detect if a URL will match a URL Segment?

i.e. If a template serving /foo/ allows segments, and you request /foo/bar/ 

Just need a boolean to indicate a hit or not.

$segmentPage = $pages->get('/foo/bar/'); // returns nullPage

At present I can't think of anything short of requesting the page and checking it's http status code:

$responseHeaders = wireHttp->head('/foo/bar/');

Both slow and doesn't seem very reliable as the templates output could be manipulating this...

Link to comment
Share on other sites

Haven't found an API method to support this but have a simple enough workaround if you have the $page at the root of the segments.

/**
* is the URL Segment valid for the page
* @param {Page} $rootPage
* @param {Array} $segments
* @return {boolean}
*
*/
function validSegments($rootPage, $segments = []) {

  if (!count($segments) ||
      !$rootPage ||
      !$rootPage->id ||
      !$rootPage->template->urlSegments ||
      (is_numeric($config->maxUrlSegments) && count($segments) > $config->maxUrlSegments)) return false;

  if ($rootPage->template->urlSegments === 1) {

      return true;

  } else if (is_array($rootPage->template->urlSegments)) {

      foreach ($segments as $segment) {

          if (array_search($segment, $rootPage->template->urlSegments) === false) {

              return false;

          }

      }

      return true;

  }

  return false;

}

 

Link to comment
Share on other sites

3 hours ago, guy said:

Is there an API method to detect if a URL will match a URL Segment?

Yes.

if($input->urlSegment1 == 'bar') {
  // display photo gallery
} else if($input->urlSegment1 == 'foo2') {
  // display map
} else {
  // display main content
}

// OR..

if($input->urlSegment2 && $input->urlSegment2 == 'whatever') {
	// do whatever
}
else {
	// blah blah
}

Have a look the the tut here, especially the second section.

 

Link to comment
Share on other sites

@guy You code will break for urlSegments which list multiple segments in one line ("some/subsegment") as well as for regex segments ("regex:[0-9]{4,5}")

And there's no good way to detect a valid url in processwire without doing a http request or faking one by manually running ProcessPageView. All the logic for finding the page one is on and the validity of urlSegments and so on is hidden inside that module and there's hardly a way to reuse if for custom purposes.

Link to comment
Share on other sites

@kongondo thanks for looking into it, but I was after an API method which could be called from outside of the page, thus $input would not have the right context for the page you are interested in. More like

$pages->get('/foo/bar');

when bar is a segment. Only really needed to know if the segment was valid.

@LostKobrakai If you are testing for multiple segments then you'd usually enter them as separate strings within the $segments array. However if you've configured segment paths on your template you could add "some/subsegment" as a single string rather than breaking it apart. Really it needs expanding for this use case as that wasn't what we required from it. Regex tests and more complex lookups are somewhat out of scope.

For our use case it worked nicely but that was multiple strings that are not segment paths.

Might be interesting to play with the ProcessPageView method though...

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...