예제 #1
0
파일: Rule.class.php 프로젝트: theratg/miao
 public function makeUrl(array $params = array(), $method = null)
 {
     if (empty($method)) {
         $metod = Miao_Router::getRequestMethod();
     }
     $uri = array();
     $parts = $this->_parts;
     foreach ($parts as $key => $paramName) {
         if ($this->_isParam($paramName)) {
             $index = substr($paramName, 1);
             if (isset($params[$index])) {
                 $uri[] = $params[$index];
                 unset($params[$index]);
             } else {
                 $message = sprintf('Require param (%s) does not exists in $params', $index);
                 throw new Miao_Router_Rule_Exception($message);
             }
         } else {
             $uri[] = $paramName;
         }
     }
     $uri = implode('/', $uri);
     $check = $this->match($uri, $method);
     if (false === $check) {
         $message = sprintf('Uri maked (%s) but did not validate', $uri);
         throw new Miao_Router_Rule_Exception($message);
     }
     $query = http_build_query($params);
     if (!empty($query)) {
         $uri .= '?' . http_build_query($params);
     }
     return $uri;
 }
예제 #2
0
 public function testSimilarRoutesAndCurrentRoute()
 {
     $config = array('main' => 'Main', 'error' => '404', 'route' => array(array('rule' => '/', 'view' => 'Main'), array('rule' => '/photo', 'view' => 'Photo_List'), array('rule' => '/photo/:section', 'view' => 'Photo_List', 'validator' => array('type' => 'Regexp', 'param' => 'section', 'pattern' => '^[a-zA-Z_]+$')), array('rule' => '/photo/:page', 'view' => 'Photo_List', 'validator' => array('type' => 'Regexp', 'param' => 'page', 'pattern' => 'p([0-9]+)')), array('rule' => '/photo/:section/:page', 'view' => 'Photo_List', 'validator' => array(array('type' => 'Regexp', 'param' => 'page', 'pattern' => 'p([0-9]+)'), array('type' => 'Regexp', 'param' => 'section', 'pattern' => '^[a-zA-Z_]+$')))));
     $router = Miao_Router::factory($config);
     $uri = '/photo/p2';
     $rule = $router->getRuleByUri($uri);
     $this->assertEquals(array('page'), $rule->getParams());
 }