public function index() { // Taken from repos # The DOC_ROOT and APP_PATH constant have to happen in the actual app # Document root, ex: /path/to/home/app.com/../ (uses ./ on CLI) define('DOC_ROOT', empty($_SERVER['DOCUMENT_ROOT']) ? './' : realpath($_SERVER['DOCUMENT_ROOT']) . '/../'); # App path, ex: /path/to/home/app.com/ define('APP_PATH', realpath(dirname(__FILE__)) . '/'); # Environment require_once DOC_ROOT . 'environment.php'; # Where is core located? define('CORE_PATH', $_SERVER['DOCUMENT_ROOT'] . "/../core/"); # Load app configs require APP_PATH . "/config/config.php"; require APP_PATH . "/config/feature_flags.php"; # Bootstrap require CORE_PATH . "bootstrap.php"; # Routing Router::$routes = array('/' => '/index'); # Match requested uri to any routes and instantiate controller Router::init(); # Display environment details require CORE_PATH . "environment-details.php"; echo "This is the index page"; }
/** * Добавить маршрут * Add Route */ public static function AddRoute($route, $destination = null) { if ($destination != null && !is_array($route)) { $route = array($route => $destination); } self::$routes = array_merge(self::$routes, $route); }
/** * Destroy the test environment */ public function tearDown() { Config::set('application.url', ''); Config::set('application.index', 'index.php'); Router::$names = array(); Router::$routes = array(); }
/** * Destroy the test environment. */ public function tearDown() { // @todo clear httpfoundation request data Config::set('session.driver', ''); Router::$routes = array(); Router::$names = array(); URL::$base = ''; Config::set('application.index', 'index.php'); Session::$instance = null; }
/** * @covers Zepto\Router::routes() */ public function testRoutes() { $this->router->get('/get', function () { return 'This is a get route'; }); $this->router->post('/post', function () { return 'This is a post route'; }); $routes = $this->router->routes(); $this->assertArrayHasKey('GET', $routes); $this->assertArrayHasKey('#^/get/$#', $routes['GET']); $this->assertInstanceOf('Zepto\\Route', $routes['GET']['#^/get/$#']); $this->assertArrayHasKey('POST', $routes); $this->assertArrayHasKey('#^/post/$#', $routes['POST']); $this->assertInstanceOf('Zepto\\Route', $routes['POST']['#^/post/$#']); }
public static function start() { if (!empty($_GET['routes'])) { $r = $_GET['routes']; unset($_GET['routes']); } else { $r = ''; } self::$routes = explode('/', $r); if (count(self::$routes) <= 2) { self::getCtrlNames(); self::model(); self::controller(); } else { self::module(); } }
public function testRoutableContentTypes() { $Type = ClassRegistry::init('Taxonomy.Type'); $type = $Type->create(array('title' => 'Press Release', 'alias' => 'press-release', 'description' => '')); $Type->save($type); $type = $Type->findByAlias('press-release'); CroogoRouter::routableContentTypes(); $params = array('url' => array(), 'plugin' => 'nodes', 'controller' => 'nodes', 'action' => 'index', 'type' => 'press-release'); $result = Router::reverse($params); $this->assertEquals('/nodes/nodes/index/type:press-release', $result); $type['Type']['params'] = 'routes=1'; $Type->save($type); Router::$routes = array(); CroogoRouter::routableContentTypes(); $result = Router::reverse($params); $this->assertEquals('/press-release', $result); }
public static function initialize() { global $HTTP_RAW_POST_DATA; global $_PUT; global $_PST; self::$routes = SiteStructure::get_routes(); require_once CONFIG . 'routes.php'; self::admin_routes(); // Format PUT/POST data as an associative array if ($_SERVER['REQUEST_METHOD'] === 'PUT') { $_PUT = json_decode(file_get_contents('php://input'), TRUE); } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { $_PST = json_decode($HTTP_RAW_POST_DATA, TRUE); foreach ($_POST as $key => $value) { $_PST[$key] = $value; } } }
public static function start() { $r = !empty($_GET['routes']) ? $_GET['routes'] : ''; self::$routes = explode('/', $r); $params = self::$routes; unset(self::$params[0], self::$params[1]); switch (self::$routes[0]) { case 'module1': case 'module2': unset(self::$params[2]); self::$params = array_values($params); self::module(); break; default: self::$params = array_values($params); self::getCtrlNames(); self::model(); self::controller(); } }
public function testAddingARoute() { $route = Router::get('/', function () { }); $this->assertContains($route, Router::routes()); }
<?php namespace NinjaWars\core; /* * Adding a default entry helps for when the action is not found or not * provided, otherwise a 404 will occur. */ Router::$routes = ['api' => ['type' => 'controller', 'actions' => ['default' => 'nw_json']], 'signup' => ['type' => 'controller'], 'login' => ['type' => 'controller', 'actions' => ['login_request' => 'requestLogin']], 'clan' => ['type' => 'controller', 'actions' => ['new' => 'create', 'default' => 'listClans', 'list' => 'listClans']], 'list' => ['type' => 'controller', 'actions' => []], 'shop' => ['type' => 'controller', 'actions' => ['purchase' => 'buy']], 'work' => ['type' => 'controller', 'actions' => ['request_work' => 'requestWork']], 'shrine' => ['type' => 'controller', 'actions' => ['heal_and_resurrect' => 'healAndResurrect']], 'doshin' => ['type' => 'controller', 'actions' => ['Bribe' => 'bribe', 'Offer Bounty' => 'offerBounty']], 'duel' => ['type' => 'controller', 'actions' => []], 'stats' => ['type' => 'controller', 'actions' => ['change_details' => 'changeDetails', 'update_profile' => 'updateProfile']], 'npc' => ['type' => 'controller', 'actions' => []], 'events' => ['type' => 'controller', 'actions' => []], 'messages' => ['type' => 'controller', 'actions' => ['delete_clan' => 'deleteClan', 'delete_messages' => 'deletePersonal', 'delete_message' => 'deletePersonal', 'send_clan' => 'sendClan', 'send_personal' => 'sendPersonal', 'personal' => 'viewPersonal', 'message' => 'viewPersonal', 'clan' => 'viewClan', 'default' => 'viewPersonal']], 'account' => ['type' => 'controller', 'actions' => ['show_change_email_form' => 'showChangeEmailForm', 'change_email' => 'changeEmail', 'show_change_password_form' => 'showChangePasswordForm', 'change_password' => 'changePassword', 'show_confirm_delete_form' => 'deleteAccountConfirmation', 'delete_account' => 'deleteAccount']], 'inventory' => ['type' => 'controller', 'actions' => ['use' => 'useItem', 'self_use' => 'selfUse']], 'skill' => ['type' => 'controller', 'actions' => ['use' => 'useSkill', 'self_use' => 'selfUse', 'post_use' => 'postUse', 'post_self_use' => 'postSelfUse']], 'quest' => ['type' => 'controller', 'actions' => []], 'consider' => ['type' => 'controller', 'actions' => ['add' => 'addEnemy', 'delete' => 'deleteEnemy']], 'assistance' => ['type' => 'controller', 'actions' => []], 'password' => ['type' => 'controller', 'actions' => ['post_email' => 'postEmail', 'post_reset' => 'postReset', 'reset' => 'getReset']], 'chat' => ['type' => 'controller', 'actions' => ['receive' => 'receive']], 'map' => ['type' => 'controller', 'actions' => ['view' => 'index']], 'player' => ['type' => 'controller', 'actions' => []], 'news' => ['type' => 'controller', 'actions' => []], 'error' => ['type' => 'controller', 'action' => []], 'rules' => ['type' => 'simple', 'title' => 'Rules'], 'staff' => ['type' => 'simple', 'title' => 'Staff'], 'public' => ['type' => 'simple', 'title' => 'Public Discussion'], 'interview' => ['type' => 'simple', 'title' => 'Interview with John Facey'], 'about' => ['type' => 'simple', 'title' => 'About NinjaWars'], 'intro' => ['type' => 'simple', 'title' => 'Intro to the game']]; Router::$controllerAliases = ['doshin_office' => 'doshin', 'village' => 'chat', 'enemies' => 'consider', 'duel' => 'rumor', 'item' => 'inventory'];
<?php # The DOC_ROOT and APP_PATH constant have to happen in the actual app # Document root, ex: /path/to/home/app.com/../ (uses ./ on CLI) define('DOC_ROOT', empty($_SERVER['DOCUMENT_ROOT']) ? './' : realpath($_SERVER['DOCUMENT_ROOT']) . '/../'); # App path, ex: /path/to/home/app.com/ define('APP_PATH', realpath(dirname(__FILE__)) . '/'); # Environment require_once DOC_ROOT . 'environment.php'; # Where is core located? define('CORE_PATH', $_SERVER['DOCUMENT_ROOT'] . "/../core/"); # Load app configs require APP_PATH . "/config/config.php"; require APP_PATH . "/config/feature_flags.php"; # Bootstrap require CORE_PATH . "bootstrap.php"; # Routing Router::$routes = array('/' => '/index', '/proposal' => '/index/p4'); # Match requested uri to any routes and instantiate controller Router::init(); # Display environment details require CORE_PATH . "environment-details.php";
public function loadDefaultRoutes() { require CODE_ROOT_DIR . "config/routes.php"; self::$routes = $routes; }
public static function setRoutes() { self::$routes = explode('/', $_SERVER['REQUEST_URI']); }
/** * */ public function tearDown() { Router::$routes = array(); }
/** * test the parsing of routes. * * @return void */ public function testParsing() { $route = new RedirectRoute('/home', array('controller' => 'posts')); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/home'); $header = $route->response->header(); $this->assertEquals(Router::url('/posts', true), $header['Location']); $route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index')); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/home'); $header = $route->response->header(); $this->assertEquals(Router::url('/posts', true), $header['Location']); $this->assertEquals(301, $route->response->statusCode()); $route = new RedirectRoute('/google', 'http://google.com'); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/google'); $header = $route->response->header(); $this->assertEquals('http://google.com', $header['Location']); $route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302)); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/posts/2'); $header = $route->response->header(); $this->assertEquals(Router::url('/posts/view', true), $header['Location']); $this->assertEquals(302, $route->response->statusCode()); $route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true)); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/posts/2'); $header = $route->response->header(); $this->assertEquals(Router::url('/posts/view/2', true), $header['Location']); $route = new RedirectRoute('/posts/*', '/test', array('persist' => true)); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/posts/2'); $header = $route->response->header(); $this->assertEquals(Router::url('/test', true), $header['Location']); $route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true)); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/my_controllers/do_something/passme/named:param'); $header = $route->response->header(); $this->assertEquals(Router::url('/tags/add/passme/named:param', true), $header['Location']); $route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add')); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/my_controllers/do_something/passme/named:param'); $header = $route->response->header(); $this->assertEquals(Router::url('/tags/add', true), $header['Location']); $route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang'))); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/nl/my_controllers/'); $header = $route->response->header(); $this->assertEquals(Router::url('/tags/add/lang:nl', true), $header['Location']); Router::$routes = array(); // reset default routes Router::connect('/:lang/preferred_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang'))); $route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang'))); $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/nl/my_controllers/'); $header = $route->response->header(); $this->assertEquals(Router::url('/nl/preferred_controllers', true), $header['Location']); }
/** * Passing a plugin name should map only for that plugin * * @return void */ public function testMapResourcesPlugin() { $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS; App::build(array('Plugin' => array($path)), App::RESET); CakePlugin::load('TestPlugin'); Router::reload(); Router::$routes = array(); ApiListener::mapResources('TestPlugin'); $expected = array('GET index /test_plugin/test_plugin', 'GET view /test_plugin/test_plugin/:id', 'POST add /test_plugin/test_plugin', 'PUT edit /test_plugin/test_plugin/:id', 'DELETE delete /test_plugin/test_plugin/:id', 'POST edit /test_plugin/test_plugin/:id', 'GET index /test_plugin/tests', 'GET view /test_plugin/tests/:id', 'POST add /test_plugin/tests', 'PUT edit /test_plugin/tests/:id', 'DELETE delete /test_plugin/tests/:id', 'POST edit /test_plugin/tests/:id'); $return = $this->_currentRoutes(); $this->assertSame($expected, $return, 'test plugin contains a test plugin and tests controller'); }
<?php Router::$welcome_route = "welcome"; Router::$routes = array('/index.php' => 'welcome', '/hello-world' => 'HelloWorld', '/hello-world/say-hi' => 'hello_world:say_hello', '/hello-world/[action]' => 'hello_world', '/admin/projects/[action]' => 'admin/projects', '/non-existent-controller' => 'non_existent_controller', '/projects' => 'projects', '/projects/(project)' => 'projects:view', '/projects/(project)/delete' => 'projects:delete', '/projects/(project)/items/(item)' => 'projects:view_item', '/friends/:user/:friend' => 'friends:view_friend');
<?php # The DOC_ROOT and APP_PATH constant have to happen in the actual app # Document root, ex: /path/to/home/app.com/../ (uses ./ on CLI) define('DOC_ROOT', empty($_SERVER['DOCUMENT_ROOT']) ? './' : realpath($_SERVER['DOCUMENT_ROOT']) . '/../'); # App path, ex: /path/to/home/app.com/ define('APP_PATH', realpath(dirname(__FILE__)) . '/'); # Environment require_once DOC_ROOT . 'environment.php'; # Where is core located? define('CORE_PATH', $_SERVER['DOCUMENT_ROOT'] . "/../core/"); # Load app configs require APP_PATH . "/config/config.php"; require APP_PATH . "/config/feature_flags.php"; # Bootstrap require CORE_PATH . "bootstrap.php"; # Routing Router::$routes = array('/' => '/index'); # Match requested uri to any routes and instantiate controller Router::init(); # Display environment details require CORE_PATH . "environment-details.php"; ?>
public function setUp() { parent::setUp(); Router::$routes = array(); include CAKE . 'Config' . DS . 'routes.php'; }
/** * Runs the callback for the given request */ public static function dispatch() { $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); $replaces = array_values(static::$patterns); self::$routes = str_replace('//', '/', self::$routes); $found_route = false; // check if route is defined without regex if (in_array($uri, self::$routes)) { $route_pos = array_keys(self::$routes, $uri); foreach ($route_pos as $route) { if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') { $found_route = true; //if route is not an object if (!is_object(self::$callbacks[$route])) { $parts = explode('@', self::$callbacks[$route]); $file = strtolower('app/controllers/' . $parts[0] . '.php'); //try to load and instantiate model if (file_exists($file)) { require $file; } //grab all parts based on a / separator $parts = explode('/', self::$callbacks[$route]); //collect the last index of the array $last = end($parts); //grab the controller name and method call $segments = explode('@', $last); //instanitate controller $controller = new $segments[0](); //call method $controller->{$segments}[1](); if (self::$halts) { return; } } else { //call closure call_user_func(self::$callbacks[$route]); if (self::$halts) { return; } } } } } else { // check if defined with regex $pos = 0; foreach (self::$routes as $route) { $route = str_replace('//', '/', $route); if (strpos($route, ':') !== false) { $route = str_replace($searches, $replaces, $route); } if (preg_match('#^' . $route . '$#', $uri, $matched)) { if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') { $found_route = true; array_shift($matched); //remove $matched[0] as [1] is the first parameter. if (!is_object(self::$callbacks[$pos])) { $parts = explode('@', self::$callbacks[$pos]); $file = strtolower('app/controllers/' . $parts[0] . '.php'); //try to load and instantiate model if (file_exists($file)) { require $file; } //grab all parts based on a / separator $parts = explode('/', self::$callbacks[$pos]); //collect the last index of the array $last = end($parts); //grab the controller name and method call $segments = explode('@', $last); //instanitate controller $controller = new $segments[0](); $params = count($matched); //call method and pass any extra parameters to the method switch ($params) { case '0': $controller->{$segments}[1](); break; case '1': $controller->{$segments}[1]($matched[0]); break; case '2': $controller->{$segments}[1]($matched[0], $matched[1]); break; case '3': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2]); break; case '4': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3]); break; case '5': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3], $matched[4]); break; case '6': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3], $matched[4], $matched[5]); break; case '7': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3], $matched[4], $matched[5], $matched[6]); break; case '8': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3], $matched[4], $matched[5], $matched[6], $matched[7]); break; case '9': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3], $matched[4], $matched[5], $matched[6], $matched[7], $matched[8]); break; case '10': $controller->{$segments}[1]($matched[0], $matched[1], $matched[2], $matched[3], $matched[4], $matched[5], $matched[6], $matched[7], $matched[8], $matched[9]); break; } if (self::$halts) { return; } } else { call_user_func_array(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } } } $pos++; } } // run the error callback if the route was not found if ($found_route == false) { if (!self::$error_callback) { self::$error_callback = function () { header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found"); echo '404'; }; } $parts = explode('@', self::$error_callback); $file = strtolower('app/controllers/' . $parts[0] . '.php'); //try to load and instantiate model if (file_exists($file)) { require $file; } if (!is_object(self::$error_callback)) { //grab all parts based on a / separator $parts = explode('/', self::$error_callback); //collect the last index of the array $last = end($parts); //grab the controller name and method call $segments = explode('@', $last); //instanitate controller $controller = new $segments[0]('No routes found.'); //call method $controller->{$segments}[1](); if (self::$halts) { return; } } else { call_user_func(self::$error_callback); if (self::$halts) { return; } } } }
/** * Resets all the route and cleaning the container * * @static * @return void */ public static function reset() { self::$routes = array(); self::$matched_route = null; }
/** * Generates routed URI from given URI. * * @param string URI to convert * @return string Routed uri */ public static function routed_uri($uri) { if (Router::$routes === NULL) { // Load routes Router::$routes = Kohana::config('routes'); } // Prepare variables $routed_uri = $uri = trim($uri, '/'); if (isset(Router::$routes[$uri])) { // Literal match, no need for regex $routed_uri = Router::$routes[$uri]; } else { // Loop through the routes and see if anything matches foreach (Router::$routes as $key => $val) { if ($key === '_default') { continue; } // Trim slashes $key = trim($key, '/'); $val = trim($val, '/'); if (preg_match('#^' . $key . '$#u', $uri)) { if (strpos($val, '$') !== FALSE) { // Use regex routing $routed_uri = preg_replace('#^' . $key . '$#u', $val, $uri); } else { // Standard routing $routed_uri = $val; } // A valid route has been found break; } } } if (isset(Router::$routes[$routed_uri])) { // Check for double routing (without regex) $routed_uri = Router::$routes[$routed_uri]; } return trim($routed_uri, '/'); }
<?php # The DOC_ROOT and APP_PATH constant have to happen in the actual app # Document root, ex: /path/to/home/app.com/../ (uses ./ on CLI) define('DOC_ROOT', empty($_SERVER['DOCUMENT_ROOT']) ? './' : realpath($_SERVER['DOCUMENT_ROOT']) . '/../'); # App path, ex: /path/to/home/app.com/ define('APP_PATH', realpath(dirname(__FILE__)) . '/'); # Environment require_once DOC_ROOT . 'environment.php'; # Where is core located? define('CORE_PATH', $_SERVER['DOCUMENT_ROOT'] . "/../core/"); # Load app configs require APP_PATH . "/config/config.php"; require APP_PATH . "/config/feature_flags.php"; # Bootstrap require CORE_PATH . "bootstrap.php"; # Routing Router::$routes = array('/' => '/index', '/calendar' => '/calendar/index'); # Match requested uri to any routes and instantiate controller Router::init(); # Display environment details require CORE_PATH . "environment-details.php";
<?php /*---------------------------- Forrst route mappings ----------------------------*/ Router::$routes = array('/' => 'Homepage::index');
<?php # The DOC_ROOT and APP_PATH constant have to happen in the actual app # Document root, ex: /path/to/home/app.com/../ (uses ./ on CLI) define('DOC_ROOT', empty($_SERVER['DOCUMENT_ROOT']) ? './' : realpath($_SERVER['DOCUMENT_ROOT']) . '/../'); # App path, ex: /path/to/home/app.com/ define('APP_PATH', realpath(dirname(__FILE__)) . '/'); # Environment require_once DOC_ROOT . 'environment.php'; # Where is core located? define('CORE_PATH', $_SERVER['DOCUMENT_ROOT'] . "/../core/"); # Load app configs require APP_PATH . "/config/config.php"; require APP_PATH . "/config/feature_flags.php"; # Bootstrap require CORE_PATH . "bootstrap.php"; # Routing Router::$routes = array('/' => '/index', '/users' => '/users/index', '/users/signup' => '/users/signup'); # Match requested uri to any routes and instantiate controller Router::init(); # Display environment details require CORE_PATH . "environment-details.php";
/** * testUrlParsing method * * @access public * @return void */ function testUrlParsing() { Router::connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value', 'somevalue', 'othervalue')); $result = Router::parse('/posts/2007/08/01/title-of-post-here'); $expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); Router::$routes = array(); Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'), array('year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY)); $result = Router::parse('/posts/2007/08/01/title-of-post-here'); $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); Router::$routes = array(); Router::connect('/posts/:day/:year/:month/*', array('controller' => 'posts', 'action' => 'view'), array('year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY)); $result = Router::parse('/posts/01/2007/08/title-of-post-here'); $expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); $this->router->routes = array(); Router::connect('/posts/:month/:day/:year/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); $result = Router::parse('/posts/08/01/2007/title-of-post-here'); $expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); Router::$routes = array(); Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view')); $result = Router::parse('/posts/2007/08/01/title-of-post-here'); $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); $this->assertEqual($result, $expected); Router::reload(); $result = Router::parse('/pages/display/home'); $expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array()); $this->assertEqual($result, $expected); $result = Router::parse('pages/display/home/'); $this->assertEqual($result, $expected); $result = Router::parse('pages/display/home'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/page/*', array('controller' => 'test')); $result = Router::parse('/page/my-page'); $expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index'); Router::reload(); Router::connect('/:language/contact', array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'), array('language' => '[a-z]{3}')); $result = Router::parse('/eng/contact'); $expected = array('pass' => array(), 'named' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/forestillinger/:month/:year/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'), array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')); $result = Router::parse('/forestillinger/10/2007/min-forestilling'); $expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array()); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/:controller/:action/*'); Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display')); $result = Router::parse('/'); $expected = array('pass' => array(), 'named' => array(), 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages'); $this->assertEqual($result, $expected); $result = Router::parse('/posts/edit/0'); $expected = array('pass' => array(0), 'named' => array(), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/:id::url_title', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\\d]+')); $result = Router::parse('/posts/5:sample-post-title'); $expected = array('pass' => array('5', 'sample-post-title'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/:id::url_title/*', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\\d]+')); $result = Router::parse('/posts/5:sample-post-title/other/params/4'); $expected = array('pass' => array('5', 'sample-post-title', 'other', 'params', '4'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/:url_title-(uuid::id)', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => Router::UUID)); $result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)'); $expected = array('pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'), 'named' => array(), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => false)); $result = Router::parse('/posts/view/foo:bar/routing:fun'); $expected = array('pass' => array('foo:bar', 'routing:fun'), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'))); $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42'); $expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); Router::reload(); Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedy' => true)); $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42'); $expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); $this->assertEqual($result, $expected); }
public static function dispatch() { $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); $replaces = array_values(static::$patterns); self::$routes = str_replace('//', '/', self::$routes); $found_route = false; // parse query parameters $query = ''; $q_arr = array(); if (strpos($uri, '&') > 0) { $query = substr($uri, strpos($uri, '&') + 1); $uri = substr($uri, 0, strpos($uri, '&')); $q_arr = explode('&', $query); foreach ($q_arr as $q) { $qobj = explode('=', $q); $q_arr[] = array($qobj[0] => $qobj[1]); if (!isset($_GET[$qobj[0]])) { $_GET[$qobj[0]] = $qobj[1]; } } } // check if route is defined without regex if (in_array($uri, self::$routes)) { $route_pos = array_keys(self::$routes, $uri); // foreach route position foreach ($route_pos as $route) { if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') { $found_route = true; //if route is not an object if (!is_object(self::$callbacks[$route])) { //call object controller and method self::invokeObject(self::$callbacks[$route]); if (self::$halts) { return; } } else { //call closure call_user_func(self::$callbacks[$route]); if (self::$halts) { return; } } } } // end foreach } else { // check if defined with regex $pos = 0; // foreach routes foreach (self::$routes as $route) { $route = str_replace('//', '/', $route); if (strpos($route, ':') !== false) { $route = str_replace($searches, $replaces, $route); } if (preg_match('#^' . $route . '$#', $uri, $matched)) { if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') { $found_route = true; //remove $matched[0] as [1] is the first parameter. array_shift($matched); if (!is_object(self::$callbacks[$pos])) { //call object controller and method self::invokeObject(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } else { //call closure call_user_func_array(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } } } $pos++; } // end foreach } // run the error callback if the route was not found if (!$found_route) { if (!is_object(self::$errorCallback)) { //call object controller and method self::invokeObject(self::$errorCallback, null, 'No routes found.'); if (self::$halts) { return; } } else { call_user_func(self::$errorCallback); if (self::$halts) { return; } } } }