/** * Sets up Router tests */ function setupRouter(Benchmark $benchmark, $routes, $args) { $name = 'Router'; $argString = implode('/', array_map(function ($i) { return ':arg' . $i; }, range(1, $args))); $str = $firstStr = $lastStr = ''; $router = new \Router(); for ($i = 0; $i < $routes; $i++) { list($pre, $post) = getRandomParts(); $str = '/' . $pre . '/' . $argString . '/' . $post; if (0 === $i) { $firstStr = str_replace(':', '', $str); } $lastStr = str_replace(':', '', $str); $router->get($str, 'handler' . $i); } $benchmark->register(sprintf('%s - last route (%s routes)', $name, $routes), function () use($router, $lastStr) { $route = $router->resolve('GET', $lastStr, array()); }); $benchmark->register(sprintf('%s - unknown route (%s routes)', $name, $routes), function () use($router) { $route = $router->resolve('GET', '/not-even-real', array()); }); }
/** * Run Controller * */ private static function runController(Router $router) { $res = $router->resolve(); $ctrl = ucfirst($res['controller'] !== null ? $res['controller'] : static::$defaultController); $action = $res['action'] !== null ? $res['action'] : static::$defaultAction; //instantiate the controller $ctrl = '\\Controller\\' . $ctrl; $controller = new $ctrl(['params' => $res['params'], 'request' => static::rqst()]); if (method_exists($controller, $action)) { return $controller->{$action}(); } else { return $controller->{static::$defaultAction}(); } }
/** * Run the application. * * @api */ public function dispatch() { # get current context from URL try { Router::resolve(isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'); Router::updateHtaccess(); #Router::updateRules(); } catch (\Exception $e) { Session::addErrorMsg($e->getMessage()); } // from here we'll capture every output ob_start(); // we need the session this early, (feel free to remove the following line and get stuck with session problems) Session::getInstance(); $module = Router::getModule(); $action = Router::getAction(); $view = $this->handleRequest($module, $action); // threat all unwanted output as error output $errors = ob_get_clean(); if (trim($errors) !== '') { Session::addErrorMsg($errors); } // finally deliver page $view->display(); }
<?php // Route resolver // Load configs require_once '../../config/config.inc.php'; ContentProcessor::pre(); Session::start(); SiteStructure::initialize(); Router::initialize(); Render::initialize(); // Resolve the incoming request $parsed_request = Router::resolve($_SERVER['REQUEST_METHOD'], SiteStructure::get_request_uri()); if ($parsed_request) { Ctrl::process($parsed_request); } else { Render::error_404(); }
<?php namespace Router; require_once 'vendor/autoload.php'; $router = new Router(); $router->setDefaultModule('index'); $router->setDefaultController('index'); $router->setDefaultAction('index'); $router->add('/admin/controllers/user/show/:param', ['module' => 'Lib\\Controllers', 'controller' => 'user', 'action' => 'show', 'param1' => 1], ['method' => 'get']); $router->add('/', ['controller' => 'index', 'action' => 'index'], ['method' => 'get | post']); $router->add('/admin/:controller/:action', ['controller' => 1, 'action' => 2], ['method' => 'post']); $router->add('/admin/controllers/user/show/:param/:param', ['module' => 'Controllers', 'controller' => 'user', 'action' => 'show', 'param1' => 1, 'param2' => 2], ['method' => 'get']); $router->add('/admin/:module/users/show/:param', ['module' => 1, 'controller' => 'users', 'action' => 'show', 'param1' => 1], ['method' => 'get']); $router->add('/admin/users/show/:name', ['controller' => 'users', 'action' => 'show', 'name1' => 1], ['method' => 'get']); $router->add('/admin/cont/:action/:param/:param', ['controller' => 'cont', 'action' => 1, 'param1' => 2, 'param2' => 3], ['method' => "get | post"]); $route = $router->resolve(); foreach ($route as $key => $value) { echo $key . ': '; if (is_string($value)) { echo $value; } elseif (is_array($value)) { foreach ($value as $key2 => $value2) { echo '<br><span style="margin-left: 50px"><b>' . $key2 . ': ' . $value2 . '</b></span><br>'; } } echo '<br>'; }