CoreUrl

Filepath: phork/php/core/CoreUrl.class.php and phork/php/app/AppUrl.class.php

The CoreUrl class parses the URL into segments and filters. It's called from the CoreBootstrap class and its most important function is being used by the bootstrap to determine the controller, and then by the controller to determine the display method.

URL Segments

The URL segments are the parts of the URL after the domain. For example if the URL is http://www.example.org/foo/bar/ then foo and bar are the segments. See the chapter on routing for more information on how these segments are used to determine the controller and how to re-route URLs to other controllers.

URL Filters

The URL filters are a hybrid between query string variables and URL segments. If any part of the URL has an equals sign in it then it will be treated as a filter rather than a segment. For example if the URL is http://www.example.org/foo/bar/page=1/ then page would be considered a filter with a value of 1.

Filters can appear anywhere in a URL and they won't affect the segments which determine the controller. Filters are not considered segments and are not available in the segments array.

Forwarding

It's possible to forward from one controller to another by explicitly setting a new URL and re-running CoreUrl's init() method followed by redetermining and changing the controller in the bootstrap. This method of forwarding has the benefit of the URL object having the new forwarded URL in it so that a call to something like getCurrentUrl() would return the forwarded URL. The other method of forwarding, which doesn't change the URL, is detailed in the CoreBootstrap chapter.

Query String URLs

If the mod-rewrite rule uses a query string then the useQueryString() method needs to be called before init().


Instantiation Example (from the bootstrap)

//initantiate and register the URL object
AppRegistry::register('Url', $objUrl = new AppUrl(AppConfig::get('BaseUrl')));

//if mod rewrite uses a query string rewrite rule then parse the URL from that
if (AppConfig::get('RewriteQueryString', false)) {
    $objUrl->useQueryString();
}

//add the routes defined in the site config
$objUrl->setRoutes(AppConfig::get('Routes', false));

Instantiation Example (from the ApiHelper)

//cloning the existing URL object will keep the existing base URL and routes
$objGetUrl = clone AppRegistry::get('Url');
$objGetUrl->init('GET', '/api/foo/get.json', array(
    'num' => 10,
    'p'   => 1
));

$objPostUrl = clone AppRegistry::get('Url');
$objPostUrl->init('POST', '/api/foo/post.json', array(
    'foo' => 42,
    'bar' => 1
));

Usage Example

//in this example the url is http://www.example.org/foo/bar/page=1/user=phork/index.xml?num=3
$objUrl = AppRegistry::get('Url');

//get the first url segment which is foo
$strSegment = $objUrl->getSegment(0);

//get the second url segment which is bar
$strSegment = $objUrl->getSegment(1);

//get all the url segments which is an array with foo and bar
$arrSegments = $objUrl->getSegments();

//get the page number which will be returned as 1
$intPage = $objUrl->getFilter('page');

//get all the filters which is an associative array
$arrFilters = $objUrl->getFilters();

//get the file extension (eg. css, js, xml, json, etc)
$strExtension = $objUrl->getExtension();

//get the num GET variable which is 3
$intNum = $objUrl->getVariable('num');

Forwarding Example

//this will result in the app thinking it's at http://www.example.org/the/forwarded/url/
$objUrl = AppRegistry::get('Url');
$objUrl->init('GET', '/the/forwarded/url/');
                                            
$objBootstrap = AppRegistry::get('Bootstrap');
$objBootstrap->changeController($objBootstrap->determineController());