dynamic URIs and pages - PhpGt/WebEngine GitHub Wiki

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 wouldn't 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 adding a 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 prefixed 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 return 2009, and $this->dynamicPath->get("title") would return "example-blog-title".

As with all Page Logic classes, the class namespace must relate to the path of the file so that it can be autoloaded correctly. For dynamic URIs, the following namespace and class naming convention must be used:

namespace App\Page\_DynamicDirectory;

class _DynamicPage extends \Gt\Logic\Page {
}

For the blog example above, we would have the following Page Logic class:

namespace App\Page\Blog\_Year\_Month\_Day;

class _TitlePage extends \Gt\Logic\Page {
	function go() {
		$year = $this->dynamicPath->get("year");
		$month = $this->dynamicPath->get("month");
		$day = $this->dynamicPath->get("day");
		$title = $this->dynamicPath->get("title");
	}
}
⚠️ **GitHub.com Fallback** ⚠️