/** * Dispatches and invokes given URL, handing over control to the involved controllers, and then renders the results (if autoRender is set). * * If no controller of given name can be found, invoke() shows error messages in * the form of Missing Controllers information. It does the same with Actions (methods of Controllers are called * Actions). * * @param string $url URL information to work on. * @param array $additionalParams Settings array ("bare", "return"), * which is melded with the GET and POST params. * @return boolean Success */ function dispatch($url, $additionalParams = array()) { $params = array_merge($this->parseParams($url), $additionalParams); $missingController = false; $missingAction = false; $missingView = false; $privateAction = false; $this->base = $this->baseUrl(); if (empty($params['controller'])) { $missingController = true; } else { $ctrlName = Inflector::camelize($params['controller']); $ctrlClass = $ctrlName . 'Controller'; if (!loadController($ctrlName)) { $pluginName = Inflector::camelize($params['action']); if (!loadPluginController(Inflector::underscore($ctrlName), $pluginName)) { if (preg_match('/([\\.]+)/', $ctrlName)) { return $this->cakeError('error404', array(array('url' => strtolower($ctrlName), 'message' => 'Was not found on this server', 'base' => $this->base))); } elseif (!class_exists($ctrlClass)) { $missingController = true; } else { $params['plugin'] = null; $this->plugin = null; } } else { $params['plugin'] = Inflector::underscore($ctrlName); } } else { $params['plugin'] = null; $this->plugin = null; } } if (isset($params['plugin'])) { $plugin = $params['plugin']; $pluginName = Inflector::camelize($params['action']); $pluginClass = $pluginName . 'Controller'; $ctrlClass = $pluginClass; $oldAction = $params['action']; $params = $this->_restructureParams($params); $this->plugin = $plugin; loadPluginModels($plugin); $this->base = $this->base . '/' . Inflector::underscore($ctrlName); if (empty($params['controller']) || !class_exists($pluginClass)) { $params['controller'] = Inflector::underscore($ctrlName); $ctrlClass = $ctrlName . 'Controller'; if (!is_null($params['action'])) { array_unshift($params['pass'], $params['action']); } $params['action'] = $oldAction; } } if (empty($params['action'])) { $params['action'] = 'index'; } if (defined('CAKE_ADMIN')) { if (isset($params[CAKE_ADMIN])) { $this->admin = '/' . CAKE_ADMIN; $url = preg_replace('/' . CAKE_ADMIN . '(\\/|$)/', '', $url); $params['action'] = CAKE_ADMIN . '_' . $params['action']; } elseif (strpos($params['action'], CAKE_ADMIN) === 0) { $privateAction = true; } } if ($missingController) { return $this->cakeError('missingController', array(array('className' => Inflector::camelize($params['controller'] . "Controller"), 'webroot' => $this->webroot, 'url' => $url, 'base' => $this->base))); } else { $controller =& new $ctrlClass(); } $classMethods = get_class_methods($controller); $classVars = get_object_vars($controller); if ((in_array($params['action'], $classMethods) || in_array(strtolower($params['action']), $classMethods)) && strpos($params['action'], '_', 0) === 0) { $privateAction = true; } if (!in_array($params['action'], $classMethods) && !in_array(strtolower($params['action']), $classMethods)) { $missingAction = true; } if (in_array(strtolower($params['action']), array('object', 'tostring', 'requestaction', 'log', 'cakeerror', 'constructclasses', 'redirect', 'set', 'setaction', 'validate', 'validateerrors', 'render', 'referer', 'flash', 'flashout', 'generatefieldnames', 'postconditions', 'cleanupfields', 'beforefilter', 'beforerender', 'afterfilter'))) { $missingAction = true; } if (in_array('return', array_keys($params)) && $params['return'] == 1) { $controller->autoRender = false; } $controller->base = $this->base; $base = strip_plugin($this->base, $this->plugin); if (defined("BASE_URL")) { $controller->here = $base . $this->admin . $url; } else { $controller->here = $base . $this->admin . '/' . $url; } $controller->webroot = $this->webroot; $controller->params = $params; $controller->action = $params['action']; if (!empty($controller->params['data'])) { $controller->data =& $controller->params['data']; } else { $controller->data = null; } if (!empty($controller->params['pass'])) { $controller->passed_args =& $controller->params['pass']; $controller->passedArgs =& $controller->params['pass']; } else { $controller->passed_args = null; $controller->passedArgs = null; } if (!empty($params['bare'])) { $controller->autoLayout = !$params['bare']; } else { $controller->autoLayout = $controller->autoLayout; } $controller->webservices = $params['webservices']; $controller->plugin = $this->plugin; if (!is_null($controller->webservices)) { array_push($controller->components, $controller->webservices); array_push($controller->helpers, $controller->webservices); $component =& new Component($controller); } $controller->_initComponents(); $controller->constructClasses(); if ($missingAction && !in_array('scaffold', array_keys($classVars))) { $this->start($controller); return $this->cakeError('missingAction', array(array('className' => Inflector::camelize($params['controller'] . "Controller"), 'action' => $params['action'], 'webroot' => $this->webroot, 'url' => $url, 'base' => $this->base))); } if ($privateAction) { $this->start($controller); return $this->cakeError('privateAction', array(array('className' => Inflector::camelize($params['controller'] . "Controller"), 'action' => $params['action'], 'webroot' => $this->webroot, 'url' => $url, 'base' => $this->base))); } return $this->_invoke($controller, $params, $missingAction); }
/** * Enter description here... * * @param array $loaded * @param array $components * @return loaded components * @access private */ function &__loadComponents(&$loaded, $components) { foreach ($components as $component) { $parts = preg_split('/\\/|\\./', $component); if (count($parts) === 1) { $plugin = $this->__controller->plugin; } else { $plugin = Inflector::underscore($parts['0']); $component = $parts[count($parts) - 1]; } $componentCn = $component . 'Component'; if (in_array($component, array_keys($loaded)) !== true) { if (!class_exists($componentCn)) { if (is_null($plugin) || !loadPluginComponent($plugin, $component)) { if (!loadComponent($component)) { $this->cakeError('missingComponentFile', array(array('className' => $this->__controller->name, 'component' => $component, 'file' => Inflector::underscore($component) . '.php', 'base' => $this->__controller->base))); exit; } } if (!class_exists($componentCn)) { $this->cakeError('missingComponentClass', array(array('className' => $this->__controller->name, 'component' => $component, 'file' => Inflector::underscore($component) . '.php', 'base' => $this->__controller->base))); exit; } } if ($componentCn == 'SessionComponent') { $param = strip_plugin($this->__controller->base, $this->__controller->plugin) . '/'; } else { $param = null; } $this->__controller->{$component} =& new $componentCn($param); $loaded[$component] =& $this->__controller->{$component}; if (isset($this->__controller->{$component}->components) && is_array($this->__controller->{$component}->components)) { $loaded =& $this->__loadComponents($loaded, $this->__controller->{$component}->components); } } } return $loaded; }
/** * Redirects to given $url, after turning off $this->autoRender. * Please notice that the script execution is not stopped after the redirect. * * @param string $url * @param integer $status * @access public */ function redirect($url, $status = null) { $this->autoRender = false; $pos = strpos($url, '://'); $base = strip_plugin($this->base, $this->plugin); if ($pos === false) { if (strpos($url, '/') !== 0) { $url = '/' . $url; } $url = FULL_BASE_URL . $base . $url; } if (function_exists('session_write_close')) { session_write_close(); } if (!empty($status)) { $codes = array(100 => "HTTP/1.1 100 Continue", 101 => "HTTP/1.1 101 Switching Protocols", 200 => "HTTP/1.1 200 OK", 201 => "HTTP/1.1 201 Created", 202 => "HTTP/1.1 202 Accepted", 203 => "HTTP/1.1 203 Non-Authoritative Information", 204 => "HTTP/1.1 204 No Content", 205 => "HTTP/1.1 205 Reset Content", 206 => "HTTP/1.1 206 Partial Content", 300 => "HTTP/1.1 300 Multiple Choices", 301 => "HTTP/1.1 301 Moved Permanently", 302 => "HTTP/1.1 302 Found", 303 => "HTTP/1.1 303 See Other", 304 => "HTTP/1.1 304 Not Modified", 305 => "HTTP/1.1 305 Use Proxy", 307 => "HTTP/1.1 307 Temporary Redirect", 400 => "HTTP/1.1 400 Bad Request", 401 => "HTTP/1.1 401 Unauthorized", 402 => "HTTP/1.1 402 Payment Required", 403 => "HTTP/1.1 403 Forbidden", 404 => "HTTP/1.1 404 Not Found", 405 => "HTTP/1.1 405 Method Not Allowed", 406 => "HTTP/1.1 406 Not Acceptable", 407 => "HTTP/1.1 407 Proxy Authentication Required", 408 => "HTTP/1.1 408 Request Time-out", 409 => "HTTP/1.1 409 Conflict", 410 => "HTTP/1.1 410 Gone", 411 => "HTTP/1.1 411 Length Required", 412 => "HTTP/1.1 412 Precondition Failed", 413 => "HTTP/1.1 413 Request Entity Too Large", 414 => "HTTP/1.1 414 Request-URI Too Large", 415 => "HTTP/1.1 415 Unsupported Media Type", 416 => "HTTP/1.1 416 Requested range not satisfiable", 417 => "HTTP/1.1 417 Expectation Failed", 500 => "HTTP/1.1 500 Internal Server Error", 501 => "HTTP/1.1 501 Not Implemented", 502 => "HTTP/1.1 502 Bad Gateway", 503 => "HTTP/1.1 503 Service Unavailable", 504 => "HTTP/1.1 504 Gateway Time-out"); if (is_string($status)) { $codes = array_combine(array_values($codes), array_keys($codes)); } if (isset($codes[$status])) { $code = ife(is_numeric($status), $status, $codes[$status]); $msg = ife(is_string($status), $status, $codes[$status]); $status = "HTTP/1.1 {$code} {$msg}"; } else { $status = null; } } if (!empty($status)) { header($status); } header('Location: ' . $url); if (!empty($status) && ($status >= 300 && $status < 400)) { header($status); } }
/** * Finds URL for specified action. * * Returns an URL pointing to a combination of controller and action. Param * $url can be: * + Empty - the method will find adress to actuall controller/action. * + '/' - the method will find base URL of application. * + A combination of controller/action - the method will find url for it. * * @param string $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4" * @param boolean $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT. * @return mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. * @access public */ function url($url = null, $return = false) { if (isset($this->plugin)) { $base = strip_plugin($this->base, $this->plugin); } else { $base = $this->base; } if (empty($url)) { return $this->here; } elseif ($url[0] == '/') { $output = $base . $url; } else { $output = $base . '/' . Inflector::underscore($this->params['controller']) . '/' . $url; } return $this->output($output, $return); }
function url($url = null) { if ($this->is_11()) { $base = strip_plugin($this->controller->base, $this->controller->plugin); if (empty($url)) { return $this->controller->here; } elseif ($url[0] == '/') { $output = $base . $url; } else { $output = $base . '/' . strtolower($this->controller->params['controller']) . '/' . $url; } return preg_replace('/&([^a])/', '&\\1', $output); } else { return Router::url($url, false); // for 1.2 } }