예제 #1
0
파일: Gear.php 프로젝트: brussens/cogear2
/**
 * Ярлык для объекта пути
 *
 * @param int $id
 * @param string    $param
 * @return  mixed
 */
function route($id = NULL, $param = 'id')
{
    if ($id) {
        $route = new Router_Route();
        $route->{$param} = $id;
        if ($route->find()) {
            return $route;
        }
    }
    return $id !== NULL ? NULL : new Router_Route();
}
예제 #2
0
파일: Router.php 프로젝트: acidline/rocket
 /**
  * Match URL against a specific given route
  */
 protected function routeMatch(Router_Route $route, $method, $url)
 {
     $params = array();
     // Static route - no PREG overhead
     if ($route->isStatic()) {
         $routeUrl = $route->route();
         // Match? (already cleaned/trimmed)
         if ($routeUrl == $url) {
             // Return defaults + HTTP method params
             $params = array_merge($route->defaults(), $route->methodDefaults($method));
         }
         // Store matched route name
         $this->_matchedRouteName = $route->name();
         // Match params
     } else {
         $result = preg_match($route->regexp(), $url, $matches);
         if ($result) {
             // Store matched route name
             $this->_matchedRouteName = $route->name();
             // Shift off first "match" result - full URL input string
             array_shift($matches);
             // Only named params, leaving off optionals
             $namedParams = array_merge($route->namedParams(), $route->optionalParamDefaults());
             $namedParamsNotOptional = array_diff_key($namedParams, $route->optionalParamDefaults());
             $namedParamsMatched = $namedParamsNotOptional;
             // Equalize matched params, rely on matching order
             // @todo Switch all routes to named captures to avoid this. Man, all these regex woes make my head hurt.
             // @link http://www.regular-expressions.info/named.html
             $namedParamsIndexed = array_keys($namedParams);
             $mi = count($namedParamsNotOptional);
             while (count($matches) > $mi) {
                 $namedParamsMatched[$namedParamsIndexed[$mi]] = $namedParams[$namedParamsIndexed[$mi]];
                 $mi++;
             }
             //var_dump($route->name(), $matches, $namedParamsMatched, $namedParams);
             // Combine params
             if (count($namedParamsMatched) != count($matches)) {
                 // Route has inequal matches to named params
                 throw new \InvalidArgumentException("Error matching URL to route params: matched(" . count($matches) . ") != named(" . count($namedParamsMatched) . ")");
             }
             $params = array_combine(array_keys($namedParamsMatched), $matches);
             if (strtoupper($method) != "GET") {
                 // 1) Determine which actions are set in $params that are also in 'methodDefaults'
                 // 2) Override the 'methodDefaults' with the explicitly set $params
                 $setParams = array_filter(array_intersect_key($params, $route->methodDefaults($method)));
                 $methodParams = array_merge($route->namedParams(), $route->defaults(), $params, $route->methodDefaults($method), $setParams);
                 $params = $methodParams;
             } else {
                 $params = array_merge($route->namedParams(), $route->defaults(), $route->methodDefaults($method), $params);
             }
             //$params = array_merge($route->namedParams(), $route->defaults(), $route->methodDefaults($method), $params);
         }
     }
     return array_map('urldecode', $params);
 }