<?php
require_once(dirname(__FILE__)."/UrlPatternManager.php");
echo "<pre>";
echo "How to parse the attributes of a friendly URL:<br>";
echo "Create an instance of UrlPatternManager<br>";
echo "Add Patterns:<br>";
$upm = new UrlPatternManager();
$pattern = "ctrlr/:controller/*";
$rules = array("controller"=>"[a-z_\-0-9]+", "action"=>"[a-z_\-0-9]+");
$defaults = array('default'=>'test');
$upm->addPattern('test1', $pattern, $rules, $defaults);
$pattern = "ctrlr/:controller/acn/:action";
$rules = array("controller"=>"[a-z_\-0-9]+", "action"=>"[a-z_\-0-9]+");
$defaults = array('default'=>'test', 'action'=>'acnnn');
$upm->addPattern('test2', $pattern, $rules, $defaults);
$pattern = "ctrlr/:controller/acn/:action/aaa/:bbb";
$rules = array();
$defaults = array('controller'=>'index', 'action'=>'index', 'bbb'=>'ccc');
$upm->addPattern('test3', $pattern, $rules, $defaults);
echo "Set the subject friendly URL part:<br>";
$upm->setSubjectUrl('ctrlr/index/dsadsad/tyutu/aaaa/bbbb');
echo "Lastly find out the attributes:<br>";
echo "Attributes of ctrlr/index/dsadsad/tyutu/aaaa/bbbb"."<br>";
print_r($upm->getAttributes());
$upm->setSubjectUrl('ctrlr/acn/aaa');
echo "Attributes of ctrlr/acn/aaa"."<br>";
print_r($upm->getAttributes());
$upm->setSubjectUrl('ctrlr/books/acn/addBook/');
echo "Attributes of ctrlr/books/acn/addBook/"."<br>";
print_r($upm->getAttributes());
echo "</pre>";
echo "Now how to generate friendly URLS:<br>";
$suppliedAttributes = array('controller'=>'books', 'action'=>'addBook');
echo "First supply a stored pattern name and attributes<br>";
$furlPart = $upm->makeUrl('test3', $suppliedAttributes);
echo "Generated friendly URL part: ".$furlPart." for the pattern ctrlr/:controller/acn/:action/aaa/:bbb<br>";
echo "Then concatanate with base URL to find actual friendly URL<br>";
echo "for example: \$furl = \$baseUrl + \$furlPart;";
?>
|