Exemplo n.º 1
0
 /**
  * Route request
  * 
  * Warn! no exceptions
  * 
  * @return bool false if no routes found
  * @throws controller_exception, router_exception
  */
 function route($parts)
 {
     $this->_uri = implode('/', $parts);
     if (is_callable(array($this, 'route_before'))) {
         $this->route_before($parts);
     }
     core::dprint(array('[route] %s using defaut router, mod: %s', $this->_uri, $this->context->get_name()));
     // give up loading routes if set in routers class
     if (empty($this->_routes)) {
         $this->_routes = $this->load_routes();
     }
     if (empty($this->_routes)) {
         core::dprint('Empty routes in ' . get_class($this), core::E_ERROR);
         return false;
     }
     foreach ($this->_routes as $id => $route) {
         // normalize
         if (!isset($route['match']) && !isset($route['regex'])) {
             $route['match'] = $id;
         }
         if (!isset($route['action'])) {
             $route['action'] = $id;
         }
         if (!isset($route['type'])) {
             $route['type'] = 'method';
         }
         // class
         if (!isset($route['template'])) {
             $route['template'] = $id;
         }
         if ($route['action'] instanceof Closure) {
             $route['type'] = 'inline';
         }
         if ($route['type'] == 'method') {
             $route['action'] = str_replace('/', '_', $route['action']);
         }
         // append section to match if any
         // if (isset($route['section']) && !empty($route['match']))    $route['match']     = $route['section'] . '/' . $route['match'];
         $this->_filters = array();
         $back_uri = $this->_uri;
         // match filters
         // all filters created before dispatch!
         $this->match_filters($route);
         // route
         $params = null;
         if ($this->_debug) {
             core::dprint('.. route ' . $id);
             core::dprint_r($route);
         }
         if ($this->_is_route_matched($route, $params)) {
             // pure ajax routes
             if (isset($route['ajax']) && true !== loader::in_ajax()) {
                 throw new router_exception('Invalid query. Code.A6299');
             }
             if (isset($route['auth']['level'])) {
                 if ($route['auth']['level'] > core::lib('auth')->get_user()->level) {
                     throw new router_exception('Access denied. Code.A6298');
                 }
             }
             core::dprint(array('Route matched "%s"', $id));
             $this->_route = $route;
             $this->context->get_controller()->run($route, $params);
             $this->run_filters();
             return true;
         }
         // restore uri, loop again?
         $this->_uri = $back_uri;
     }
     return false;
 }