/** * Finds the route and gets the closure or controller * @return boolean doesn't answer anything, just to quit the method from running. * Errors that can be thrown here. * Code Desc * C_100 Controller file not found. * C_101 Controller classname not valid. * C_102 Methodname not valid. */ public function getPage() { $exp = explode('/', Url::uri()); $i = count($exp); $_data = null; while ($i) { $s = implode('/', $exp); if (Url::uri() == '') { $s = '/'; } $_data = Router::find($this->method, $s); if ($_data !== false) { break; } unset($exp[$i - 1]); $i--; } // Try to find a route that contains a wildcard. // Router::find_with_wildcard(); $data = $_data; if ($data === false) { Error::register('http_404'); return false; } if ($data['closure'] == true) { $this->view = $data['func'](); if (!$this->view instanceof View) { $this->isView = false; } } else { // Getting the correct controller and method starts here. if (strpos($data['func'], '@') === false) { $this->content = $data['func']; return true; } list($controller, $method) = explode('@', $data['func']); if (!is_file(APP . 'controller/' . $controller . '.php')) { return Error::register('controller_100'); } require_once APP . 'controller/' . $controller . '.php'; $controller_name = $controller . '_Controller'; if (!class_exists($controller_name)) { return Error::register('controller_101'); } $ct = new $controller_name(); $restful = isset($ct->restful) ? $ct->restful : false; $r_method = $restful === true ? $this->method . '_' : ''; $method_name = $r_method . $method; if (!method_exists($controller_name, $method_name)) { return Error::register('controller_102'); } $this->view = call_user_func(array($ct, $method_name)); if (!$this->view instanceof View) { $this->isView = false; } if ($this->view instanceof Json) { $this->isJson = true; } } }
/** * Router elindítása */ private function load_router() { $router = new Router(); $router->find($this->registry->uri_path, $this->registry->area); $this->registry->controller = $router->controller; $this->registry->action = $router->action; $this->registry->params = $router->params; }
public static function run() { try { self::setup(); $ctx = Context::last(); if ('admin/install' != $ctx->query() and !$ctx->config->isOk()) { $ctx->redirect('admin/install'); } $router = new Router(); $result = $router->poll($ctx)->dispatch($ctx); if ($result instanceof Response) { $result->send(); } elseif (false === $result) { header('HTTP/1.1 404 Not Found'); header('Content-Type: text/plain; charset=utf-8'); die('Route Not Found.'); } else { list($handler, $args) = $router->find($ctx); if (false === $handler) { $method = '?unknown?'; } elseif (is_array($handler['call'])) { $method = implode('::', $handler['call']); } else { $method = $handler['call']; } $message = t('<h1>Внутренняя ошибка</h1><p>Обработчик пути <tt>%path</tt> (<tt>%func</tt>) должен был вернуть объект класса <a href="@class">Response</a>, а вернул %type.</p><hr/><a href="@home">Molinos CMS v%version</a>', array('%path' => $ctx->query(), '%type' => gettype($result), '%func' => $method, '@class' => 'http://code.google.com/p/molinos-cms/wiki/Response_Class', '%version' => MCMS_VERSION, '@home' => 'http://molinos-cms.googlecode.com/')); header('HTTP/1.1 500 Internal Server Error'); header('Content-Type: text/html; charset=utf-8'); die($message); } } catch (Exception $e) { Logger::trace($e); header('HTTP/1.1 500 FUBAR'); header('Content-Type: text/plain; charset=utf-8'); die(sprintf('%s: %s.', get_class($e), rtrim($e->getMessage(), '.'))); } }
static function find($name) { return array_get(head((array) \Router::find($name)), static::OPTION); }
<!DOCTYPE html> <html> <head> <?php echo $wf->fetch('components.head'); ?> </head> <body class="<?php echo $wf->classes(); ?> "> <header> <?php echo $wf->fetch('layouts.header'); ?> </header> <main> <?php echo $wf->find(); ?> </main> <footer> <?php echo $wf->fetch('layouts.footer'); ?> </footer> <?php echo $wf->fetch('components.scripts'); ?> </body> </html>