Exemplo n.º 1
0
 /**
  * Routes like {@link Lvc_RewriteRouter} does, with the additional check to
  * routes for specifying custom routes based on regular expressions.
  * 
  * @param Lvc_HttpRequest $request A request object to route.
  * @return boolean
  * @author Anthony Bush
  * @since 2007-05-08
  **/
 public function route($request)
 {
     $params = $request->getParams();
     if (isset($params['get']['url'])) {
         // Use mod_rewrite's url
         $url = $params['get']['url'];
         $matches = array();
         foreach ($this->routes as $regex => $parsingInfo) {
             if (preg_match($regex, $url, $matches)) {
                 // Check for redirect action first
                 if (isset($parsingInfo['redirect'])) {
                     $redirectUrl = preg_replace($regex, $parsingInfo['redirect'], $url);
                     // Pass query strings along
                     $q = $params['get'];
                     unset($q['url']);
                     $q = !empty($q) ? '?' . http_build_query($q) : '';
                     header('Location: ' . $redirectUrl . $q);
                     exit;
                 }
                 // Get controller name if available
                 if (isset($parsingInfo['controller'])) {
                     if (is_int($parsingInfo['controller'])) {
                         // Get the controller name from the regex matches
                         $request->setControllerName(@$matches[$parsingInfo['controller']]);
                     } else {
                         // Use the constant value
                         $request->setControllerName($parsingInfo['controller']);
                     }
                 }
                 // Get action name if available
                 if (isset($parsingInfo['action'])) {
                     if (is_int($parsingInfo['action'])) {
                         // Get the action from the regex matches
                         $request->setActionName(@$matches[$parsingInfo['action']]);
                     } else {
                         // Use the constant value
                         $request->setActionName($parsingInfo['action']);
                     }
                 }
                 // Get action parameters
                 $actionParams = array();
                 if (isset($parsingInfo['action_params'])) {
                     foreach ($parsingInfo['action_params'] as $key => $value) {
                         if (is_int($value)) {
                             // Get the value from the regex matches
                             if (isset($matches[$value])) {
                                 $actionParams[$key] = $matches[$value];
                             } else {
                                 $actionParams[$key] = null;
                             }
                         } else {
                             // Use the constant value
                             $actionParams[$key] = $value;
                         }
                     }
                 }
                 if (isset($parsingInfo['additional_params'])) {
                     if (is_int($parsingInfo['additional_params'])) {
                         // Get the value from the regex matches
                         if (isset($matches[$parsingInfo['additional_params']])) {
                             $actionParams = $actionParams + explode('/', $matches[$parsingInfo['additional_params']]);
                         }
                     }
                 }
                 $request->setActionParams($actionParams);
                 return true;
             }
             // route matched
         }
         // loop through routes
     }
     // url _GET value set
     return false;
 }
Exemplo n.º 2
0
 /**
  * Routes like {@link Lvc_RewriteRouter} does, with the additional check to
  * routes for specifying custom routes based on regular expressions.
  * 
  * @param Lvc_HttpRequest $request A request object to route.
  * @return boolean
  * @author Anthony Bush
  * @since 2007-05-08
  **/
 public function route($request)
 {
     $params = $request->getParams();
     if (isset($params['uri'])) {
         $url = $params['uri'];
         $matches = array();
         foreach ($this->routes as $regex => $parsingInfo) {
             if (preg_match($regex, $url, $matches)) {
                 // Check for redirect action first
                 if (isset($parsingInfo['redirect'])) {
                     $redirectUrl = preg_replace($regex, $parsingInfo['redirect'], $url);
                     // Output any custom headers, e.g. "HTTP/1.1 301 Moved Permanently"
                     if (isset($parsingInfo['headers'])) {
                         if (is_array($parsingInfo['headers'])) {
                             foreach ($parsingInfo['headers'] as $header) {
                                 header($header);
                             }
                         } else {
                             header($parsingInfo['headers']);
                         }
                     }
                     header('Location: ' . $redirectUrl);
                     exit;
                 }
                 // Get controller name if available
                 if (isset($parsingInfo['controller'])) {
                     if (is_int($parsingInfo['controller'])) {
                         // Get the controller name from the regex matches
                         $request->setControllerName(@$matches[$parsingInfo['controller']]);
                     } else {
                         // Use the constant value
                         $request->setControllerName($parsingInfo['controller']);
                     }
                 }
                 if (isset($parsingInfo['sub_path'])) {
                     if (is_int($parsingInfo['sub_path'])) {
                         $request->setControllerSubPath(@$matches[$parsingInfo['sub_path']]);
                     } else {
                         // Use the constant value
                         $request->setControllerSubPath($parsingInfo['sub_path']);
                     }
                 }
                 // Get action name if available
                 if (isset($parsingInfo['action'])) {
                     if (is_int($parsingInfo['action'])) {
                         // Get the action from the regex matches
                         $request->setActionName(@$matches[$parsingInfo['action']]);
                     } else {
                         // Use the constant value
                         $request->setActionName($parsingInfo['action']);
                     }
                 }
                 // Get action parameters
                 $actionParams = array();
                 if (isset($parsingInfo['action_params'])) {
                     foreach ($parsingInfo['action_params'] as $key => $value) {
                         if (is_int($value)) {
                             // Get the value from the regex matches
                             if (isset($matches[$value])) {
                                 $actionParams[$key] = $matches[$value];
                             } else {
                                 $actionParams[$key] = null;
                             }
                         } else {
                             // Use the constant value
                             $actionParams[$key] = $value;
                         }
                     }
                 }
                 if (isset($parsingInfo['additional_params'])) {
                     if (is_int($parsingInfo['additional_params'])) {
                         // Get the value from the regex matches
                         if (isset($matches[$parsingInfo['additional_params']])) {
                             $actionParams = $actionParams + explode('/', $matches[$parsingInfo['additional_params']]);
                         }
                     }
                 }
                 $request->setActionParams($actionParams);
                 return true;
             }
             // route matched
         }
         // loop through routes
     }
     // uri value set
     return false;
 }