Skip to content
Greg Bowler edited this page Sep 1, 2018 · 19 revisions

A URI (Universal Resource Identifier) is a sequence of characters that identify the World Wide Web request being made. Sometimes referred to as a URL (Universal Resource Locator) or URN (Universal Resource Name), with extremely subtle differences, we shall use the term URI for consistency with PHP's internal naming conventions.

A simple URI https://www.example.com/colour/red is made up of the following parts:

  • https - the URI scheme / protocol being used to make the request
  • www.example.com - the host (otherwise referred to as the hostname)
  • /colour/red - the path

A complex URI https://henry:s3cr3t@www.example.com:8301/colour/red?sort=price&filter=new#options is made up of the following parts:

  • https - the URI scheme / protocol being used to make the request
  • henry:s3cr3t - the user info
  • www.example.com - the host (otherwise referred to as the hostname)
  • 8301 - the port
  • /colour/red - the path
  • sort=price&filter=new - the query or querystring
  • option - the fragment

Throughout PHP.Gt, wherever a URI is referenced, an object that implements PSR-7's UriInterface is used.

Routing a URI to an application page view and logic

In WebEngine, dynamic responses are built using a Page View (HTML) and Page Logic (PHP).

A common technique to match incoming requests with the correct areas of code is to use a concept called "routing". While there is a Router within WebEngine, it is handled for you automatically, so you don't need to write logic to "load" your code or link the request to the correct HTML source, for instance.

In WebEngine applications, routing is done by following a naming convention: the URI path that is requested is used to identify which page view and logic to load. For example, a request to https://www.example.com/about has a URI path of /about, which means that the HTML source for the request will be defined in your application source page/about.html, and the optional page logic will be defined in page/about.php.

All requests should have a page view to respond with, which can utilise DOM templates to remove repetition, and can have an optional logic class to make the page dynamic.

Continue reading about Page view and Page logic.

Accessing the current URI in a WebEngine application

In WebEngine applications, all superglobals are protected, which means the $_SERVER superglobal is not available. Instead, the values of $_SERVER are exposed through the Server class (part of PHP.Gt/Http) and encapsulated within your Page Logic.

From within any Page Logic class, you can obtain the current requested URI using the getRequestUri function of the Server object, as follows:

public function checkCurrentURI() {
	$uri = $this->server->getRequestUri();
	$host = $uri->getHost();
	$path = $uri->getPath();

	$this->doSomethingWithUriParts($host, $path);
}

Dynamic URIs

Having the router match all URIs to View and Logic resources is limiting when dynamic content is requested from a URI, such as a blog article at the URI /blog/2009/08/04/example-blog-title. It would not be feasible to have a directory path for every single year, month and day!

A special syntax can be used to indicate to the router that a part of the requested URI is dynamic. The above URI can then link to the Page View file located at page/blog/@year/@month/@day/@title.html. The @title.html file will be served for any request under the blog directory that matches the URI pattern of /blog/year/month/day/title.

The Page View can be made dynamic by a adding Page Logic file at page/blog/@year/@month/@day/@title.php, which can be used to bind the correct data to the document and handle 404 errors for non-matching content.

Each part of the dynamic URI that is prefixes with an @ symbol can be retrieved by the dynamicPath property of the PageLogic class. For example, in the URI /blog/2009/08/04/example-blog-title, $this->dynamicPath->get("year")would return2009, and $this->dynamicPath->get("title")would return"example-blog-title"`.

Clone this wiki locally