Router object
This object is located inside the main $mv object and is responsible for working with the passed URL and determining the route.
- getUrl() - returns the requested URL, if the index page is requested, it will return '/'
- getUrlParts() - returns an array of URL parts
- getUrlPart($index) - returns the part of the URL under the desired ordinal number, starting with 0
- getRoute() - returns the file name of the included template
- isIndex() - checks whether the current page is the main page
Closely related to the Router object is the checkUrlPart($index [,$extra_check]) method in the $mv object, which is described in the section MV object. This method checks if there is a part of the URL under the specified index and if there is, it returns it, and if not, it shows a 404 error. $extra_check is passed as an additional parameter, if its value is 'numeric', then this part of the URL must be numeric.
//We try to extract the third part from the URL, a request like '/articles/32/83'
$article_id = intval($mv -> router -> getUrlPart(2));
//If there is no such element, then the page was not found
$mv -> display404($article_id);
//If the main page is requested, we include the left column
if($mv -> router -> isIndex())
include $mv -> views_path.'parts/left-column.php';
//Redirect with a parameter containing the return path
$mv -> redirect('order/create?from='.$mv -> router -> getUrl());
//Check if the URL like '/user/652' has a numeric second part,
//if not, show a 404 error
$user_id = $mv -> checkUrlPart(1, 'numeric');
Previous section
General principles of templates