A callable can be passed in place of $options. In this case the callable acts as a *route
handler*. Route handlers should return an instance of lithium\net\http\Response
and can be used to short-circuit the framework's lookup and invocation of controller
actions:
Router::connect('/photos/{:id:[0-9]+}.jpg', array(), function($request) {
return new Response(array(
'headers' => array('Content-type' => 'image/jpeg'),
'body' => Photos::first($request->id)->bytes()
));
});
public static connect ( string | object $template, array | string $params = [], array | callable $options = [] ) : array | ||
$template | string | object | An empty string, a route string `/` or an instance of `lithium\net\http\Route`. |
$params | array | string | An array describing the default or required elements of the route or alternatively a path string i.e. `Posts::index`. |
$options | array | callable | Either an array of options (`'handler'`, `'formatters'`, `'modifiers'`, `'unicode'` as well as any options for `Route`) or a callable that will be used as a route handler. |
return | array | Array of routes |
public function tearDown() { Router::reset(); foreach ($this->_routes as $route) { Router::connect($route); } }
/** * Clean up after the test. * * @return void */ public function tearDown() { Router::connect(null); $this->_routes->each(function ($route) { Router::connect($route); }); unset($this->html); }
function teardown() { Router::reset(); foreach ($this->_routes as $route) { Router::connect($route); } unset($this->analytics); }
public function setUp() { $this->html = new Html(); $this->mock = new MockHtml(); Router::connect('/test/{:args}', array('controller' => '\\lithium\\test\\Controller')); Router::connect('/test', array('controller' => '\\lithium\\test\\Controller')); $this->request = new Request(array('base' => null, 'env' => array('PHP_SELF' => '/', 'DOCUMENT_ROOT' => '/'))); }
/** * Initialize test by creating a new object instance with a default context. */ public function setUp() { Router::connect('/{:controller}/{:action}/{:args}'); $this->request = new Request(); $this->request->params = ['controller' => 'post', 'action' => 'index']; $this->request->persist = ['controller']; $this->context = new MockRenderer(['request' => $this->request]); }
public function testRun() { Router::connect('/', array('controller' => 'test', 'action' => 'test')); $request = new Request(); $request->url = '/'; MockDispatcher::run($request); $result = end(MockDispatcher::$dispatched); $expected = array('controller' => 'test', 'action' => 'test'); $this->assertEqual($expected, $result->params); }
/** * Initialize test by creating a new object instance with a default context. */ public function setUp() { Router::connect('/{:controller}/{:action}/{:id}.{:type}', array('id' => null)); Router::connect('/{:controller}/{:action}/{:args}'); $request = new Request(); $request->params = array('controller' => 'posts', 'action' => 'index'); $request->persist = array('controller'); $this->context = new MockFormRenderer(compact('request')); $this->form = new Form(array('context' => $this->context)); $this->base = $this->context->request()->env('base') . '/'; }
public function setUp() { $this->_routes = Router::get(); Router::reset(); Router::connect('/{:controller}/{:action}/page/{:page:[0-9]+}'); $request = new Request(); $request->params = array('controller' => 'posts', 'action' => 'index'); $request->persist = array('controller'); $this->context = new MockRenderer(compact('request')); $this->pagination = new Pagination(array('context' => $this->context)); }
public function testConfigManipulation() { $config = MockDispatcher::config(); $expected = array('rules' => array()); $this->assertEqual($expected, $config); MockDispatcher::config(array('rules' => array('admin' => array('action' => 'admin_{:action}')))); Router::connect('/', array('controller' => 'test', 'action' => 'test', 'admin' => true)); MockDispatcher::run(new Request(array('url' => '/'))); $result = end(MockDispatcher::$dispatched); $expected = array('action' => 'admin_test', 'controller' => 'Test', 'admin' => true); $this->assertEqual($expected, $result->params); }
/** * Clean up after the test. */ public function tearDown() { Router::reset(); foreach ($this->_routes as $scope => $routes) { Router::scope($scope, function () use($routes) { foreach ($routes as $route) { Router::connect($route); } }); } unset($this->html); }
/** * Tests that URLs are properly escaped by the URL handler. */ public function testUrlAutoEscaping() { Router::connect('/{:controller}/{:action}/{:id}'); $this->assertEqual('/<foo>/<bar>', $this->subject->url('/<foo>/<bar>')); $result = $this->subject->url(array('Controller::action', 'id' => '<script />')); $this->assertEqual('/controller/action/<script />', $result); $this->subject = new Simple(array('response' => new Response(), 'view' => new View(), 'request' => new Request(array('base' => '', 'env' => array('HTTP_HOST' => 'foo.local'))))); $this->assertEqual('/<foo>/<bar>', $this->subject->url('/<foo>/<bar>')); $result = $this->subject->url(array('Controller::action', 'id' => '<script />')); $this->assertEqual('/controller/action/<script />', $result); $result = $this->subject->url(array('Posts::index', '?' => array('foo' => 'bar', 'baz' => 'dib'))); $this->assertEqual('/posts?foo=bar&baz=dib', $result); }
protected function _restoreCtrlContext() { Router::reset(); foreach ($this->_context['routes'] as $scope => $routes) { Router::scope($scope, function () use($routes) { foreach ($routes as $route) { Router::connect($route); } }); } foreach ($this->_context['scopes'] as $scope => $attachment) { Router::attach($scope, $attachment); } Router::scope($this->_context['scope']); }
// TODO: See why this isn't catching in the Minerva library routes... // This should already be set... Router::connect('/minerva_gallery/admin/{:controller}/{:action}/{:args}', array('admin' => 'admin', 'library' => 'minerva_gallery', 'controller' => 'items', 'action' => 'index')); // JSON Routes Router::connect('/minerva_gallery/{:controller}/{:action}.json', array('library' => 'minerva_gallery', 'controller' => 'items', 'type' => 'json')); Router::connect('/minerva_gallery/{:controller}/{:action}/{:args}.json', array('library' => 'minerva_gallery', 'controller' => 'items', 'type' => 'json')); // Route for reading a gallery (note the "document_type" parameter) Router::connect('/gallery/read/{:url}', array('library' => 'minerva', 'plugin' => 'minerva_gallery', 'controller' => 'pages', 'action' => 'read')); // Route for listing all galleries Router::connect('/gallery/index', array('library' => 'minerva', 'plugin' => 'minerva_gallery', 'controller' => 'pages', 'action' => 'index')); // Pagination for galleries (default limit is 10) Router::connect('/gallery/index/page:{:page:[0-9]+}', array('library' => 'minerva', 'plugin' => 'minerva_gallery', 'controller' => 'pages', 'action' => 'index')); Router::connect('/gallery/index/page:{:page:[0-9]+}/limit:{:limit:[0-9]+}', array('library' => 'minerva', 'plugin' => 'minerva_gallery', 'controller' => 'pages', 'action' => 'index')); // Yes, you can render "static" pages from the library as well by using the "view" action, // Templates from: /libraries/minerva_gallery/views/pages/static/template-name.html.php Router::connect('/gallery', array('library' => 'minerva', 'plugin' => 'minerva_gallery', 'controller' => 'pages', 'action' => 'view', 'home')); Router::connect('/gallery/view/{:args}', array('library' => 'minerva', 'plugin' => 'minerva_gallery', 'controller' => 'pages', 'action' => 'view', 'home')); // NOTE: /gallery route could also be reached via the default Minerva route: /minerva/plugin/minerva_gallery // Also: /minerva/plugin/minerva_gallery/pages/read/document == /gallery/read/{:url} // TODO? Do galleries also need comments? Should there be a unified comments library now? // Router::connect('/gallery/comments/{:action}.json', array('library' => 'minerva_gallery', 'controller' => 'comments', 'type' => 'json')); // Router::connect('/gallery/comments/{:action}/{:args}.json', array('library' => 'minerva_gallery', 'controller' => 'comments', 'type' => 'json')); // SWEEPSTAKES LIBRARY SPECIFIC ROUTES (Routed to look like it belongs to Minerva. Library added as with 'minerva_plugin' => true, so it will follow the render paths.) /* Router::connect("/minerva/gallery/admin/{:controller}/{:action}/{:url}", array('admin' => 'admin', 'library' => 'sweeps', 'controller' => 'sweepstakes', 'action' => 'index')); Router::connect("/minerva/gallery/{:controller}/{:action}", array('library' => 'sweeps', 'controller' => 'sweepstakes', 'action' => 'index')); Router::connect("/minerva/gallery/{:controller}/{:action}/{:args}", array('library' => 'sweeps', 'controller' => 'sweepstakes', 'action' => 'index')); Router::connect("/minerva/gallery/{:controller}/{:action}/{:url}", array('library' => 'sweeps', 'controller' => 'sweepstakes', 'action' => 'index')); * */
public function testConfigManipulation() { $config = MockDispatcher::config(); $expected = array('rules' => array()); $this->assertEqual($expected, $config); MockDispatcher::config(array('rules' => array('admin' => array('action' => 'admin_{:action}')))); Router::connect('/', array('controller' => 'test', 'action' => 'test', 'admin' => true)); MockDispatcher::run(new Request(array('url' => '/'))); $result = end(MockDispatcher::$dispatched); $expected = array('action' => 'admin_test', 'controller' => 'Test', 'admin' => true); $this->assertEqual($expected, $result->params); MockDispatcher::config(array('rules' => array('action' => array('action' => function ($params) { return Inflector::camelize(strtolower($params['action']), false); })))); MockDispatcher::$dispatched = array(); Router::reset(); Router::connect('/', array('controller' => 'test', 'action' => 'TeST-camelize')); MockDispatcher::run(new Request(array('url' => '/'))); $result = end(MockDispatcher::$dispatched); $expected = array('action' => 'testCamelize', 'controller' => 'Test'); $this->assertEqual($expected, $result->params); MockDispatcher::config(array('rules' => function ($params) { if (isset($params['admin'])) { return array('special' => array('action' => 'special_{:action}')); } return array(); })); MockDispatcher::$dispatched = array(); Router::reset(); Router::connect('/', array('controller' => 'test', 'action' => 'test', 'admin' => true)); Router::connect('/special', array('controller' => 'test', 'action' => 'test', 'admin' => true, 'special' => true)); MockDispatcher::run(new Request(array('url' => '/'))); $result = end(MockDispatcher::$dispatched); $expected = array('action' => 'test', 'controller' => 'Test', 'admin' => true); $this->assertEqual($expected, $result->params); MockDispatcher::run(new Request(array('url' => '/special'))); $result = end(MockDispatcher::$dispatched); $expected = array('action' => 'special_test', 'controller' => 'Test', 'admin' => true, 'special' => true); $this->assertEqual($expected, $result->params); }
Router::connect('/{:library}/profile/trace/{:file}', array('file' => 'latest'), function ($request) { // If for some reason a profile session is active, do NOT run the code in this route. // It would cause some pretty big issues =) if (isset($_GET['XDEBUG_PROFILE']) || isset($_COOKIE['XDEBUG_PROFILE'])) { echo 'You can\'t attempt to get the latest profile information while the profiler is active.'; return false; } // webgrind bootstrap/config process require LITHIUM_APP_PATH . '/libraries/li3_perf/extensions/webgrind/library/bootstrap.php'; $trace_files = FileHandler::getInstance()->getTraceList(1); if (is_array($trace_files)) { $dataFile = $trace_files[0]['filename']; // I've seen this work before and then sometimes not...Sometimes it needs the slash. Weird. if (!file_exists(Webgrind::$config->xdebugOutputDir . $dataFile)) { $dataFile = '/' . $dataFile; } $costFormat = Webgrind::$config->defaultCostformat; $reader = FileHandler::getInstance()->getTraceReader($dataFile, $costFormat); $result = array(); $functions = array(); $shownTotal = 0; $breakdown = array('internal' => 0, 'procedural' => 0, 'class' => 0, 'include' => 0); $functionCount = $reader->getFunctionCount(); $result['moodpik'] = array('function_calls' => 0, 'total_cost' => 0); for ($i = 0; $i < $functionCount; $i++) { $functionInfo = $reader->getFunctionInfo($i); //var_dump($functionInfo['functionName']); if (strstr($functionInfo['functionName'], 'moodpik\\')) { $result['moodpik']['function_calls']++; $result['moodpik']['total_cost'] += $functionInfo['summedSelfCost']; } $isInternal = strpos($functionInfo['functionName'], 'php::') !== false; if ($isInternal) { if (get('hideInternals', false)) { continue; } $breakdown['internal'] += $functionInfo['summedSelfCost']; $humanKind = 'internal'; } elseif (false !== strpos($functionInfo['functionName'], 'require_once::') || false !== strpos($functionInfo['functionName'], 'require::') || false !== strpos($functionInfo['functionName'], 'include_once::') || false !== strpos($functionInfo['functionName'], 'include::')) { $breakdown['include'] += $functionInfo['summedSelfCost']; $humanKind = 'include'; } elseif (false !== strpos($functionInfo['functionName'], '->') || false !== strpos($functionInfo['functionName'], '::')) { $breakdown['class'] += $functionInfo['summedSelfCost']; $humanKind = 'class'; } else { $breakdown['procedural'] += $functionInfo['summedSelfCost']; $humanKind = 'procedural'; } $shownTotal += $functionInfo['summedSelfCost']; $functions[$i] = $functionInfo; $functions[$i]['nr'] = $i; $functions[$i]['humanKind'] = $humanKind; } usort($functions, 'costCmp'); $remainingCost = $shownTotal * get('showFraction'); $result['functions'] = array(); foreach ($functions as $function) { $remainingCost -= $function['summedSelfCost']; $function['file'] = urlencode($function['file']); $result['functions'][] = $function; if ($remainingCost < 0) { break; } } $result['summedInvocationCount'] = $reader->getFunctionCount(); $result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec'); $result['dataFile'] = $dataFile; $result['invokeUrl'] = $reader->getHeader('cmd'); $result['runs'] = $reader->getHeader('runs'); $result['breakdown'] = $breakdown; $result['mtime'] = date(Webgrind::$config->dateFormat, filemtime(Webgrind::$config->xdebugOutputDir . '/' . $dataFile)); $creator = preg_replace('/[^0-9\\.]/', '', $reader->getHeader('creator')); $result['linkToFunctionLine'] = version_compare($creator, '2.1') > 0; var_dump($result); exit; } });
public function testMatchWithScopeAndWithoutController() { Router::scope('app', function () { Router::connect('/{:id}', 'Posts::index'); }); $request = new Request(array('url' => '/1', 'base' => '')); MockDispatcher::run($request); $result = Router::match(array('id' => 2), $request); $this->assertEqual('/2', $result); }
<?php use lithium\core\Libraries; use lithium\action\Response; use lithium\net\http\Router; use lithium\net\http\Media; $config = Libraries::get('li3_docs'); $base = $config['url'] == '/' ? '' : $config['url']; /** * Handles broken URL parsers by matching method URLs with no closing ) and redirecting. */ Router::connect("{$base}/{:args}\\(", array(), function ($request) { return new Response(array('location' => "{$request->url})")); }); Router::connect($base ?: '/', array('controller' => 'li3_docs.ApiBrowser', 'action' => 'index')); Router::connect("{$base}/{:path:js|css}/{:file}.{:type}", array(), function ($request) { $req = $request->params; $file = dirname(__DIR__) . "/webroot/{$req['path']}/{$req['file']}.{$req['type']}"; if (!file_exists($file)) { return; } return new Response(array('body' => file_get_contents($file), 'headers' => array('Content-type' => str_replace(array('css', 'js'), array('text/css', 'text/javascript'), $req['type'])))); }); Router::connect("{$base}/{:lib}/{:args}", array('controller' => 'li3_docs.ApiBrowser', 'action' => 'view'));
<?php use lithium\net\http\Router; Router::connect('/oauth/client/{:action}/{:args}', array('library' => 'li3_pecl_oauth', 'controller' => 'li3_pecl_oauth.client', 'action' => 'index'));
use lithium\net\http\Router; /** * Url prefix */ $umPrefix = '/'; $user = '******'; $users = $user . 's'; /** * SessionController routes */ Router::connect($umPrefix . 'login', 'li3_usermanager.Session::create'); Router::connect($umPrefix . 'logout', 'li3_usermanager.Session::destroy'); /** * UsersController routes */ Router::connect($umPrefix . $user, 'li3_usermanager.Users::index'); if (LI3_UM_EnableUserRegistration) { Router::connect("{$umPrefix}{$user}/register", 'li3_usermanager.Users::add'); } Router::connect("{$umPrefix}{$user}/activate/{:token}/{:id}", 'li3_usermanager.Users::activate'); Router::connect("{$umPrefix}{$user}/edit-details", 'li3_usermanager.Users::editDetails'); Router::connect("{$umPrefix}{$user}/change-email", 'li3_usermanager.Users::changeEmail'); Router::connect("{$umPrefix}{$user}/change-password", 'li3_usermanager.Users::changePassword'); Router::connect("{$umPrefix}{$user}/reset-password", 'li3_usermanager.Users::requestResetPassword'); Router::connect("{$umPrefix}{$user}/reset-password/{:token}/{:id}", 'li3_usermanager.Users::resetPassword'); /** * ManageUsersController routes */ Router::connect("{$umPrefix}manage/{$users}/{:action}/{:id}", array('controller' => 'li3_usermanager.ManageUsers', 'id' => null));
<?php use li3b_users\models\Asset; use lithium\net\http\Router; use lithium\core\Environment; use lithium\action\Dispatcher; use lithium\action\Response; Router::connect("/login", array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'login', 'admin' => null)); Router::connect("/logout", array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'logout', 'admin' => null)); Router::connect("/register", array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'register', 'admin' => null)); Router::connect("/settings", array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'update', 'admin' => null)); Router::connect("/profile/{:args}", array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'read', 'args' => array(), 'admin' => null)); // Route for images stored in GridFS. Router::connect('/profilepic/{:args}.(jpe?g|png|gif)', array(), function ($request) { $possibleErrors = array('TOO_LARGE', 'INVALID_FILE_TYPE'); if (!in_array($request->params['args'][0], $possibleErrors)) { $image = Asset::find('first', array('conditions' => array('_id' => $request->params['args'][0]))); if (!$image || !$image->file) { header("Status: 404 Not Found"); header("HTTP/1.0 404 Not Found"); die; } return new Response(array('headers' => array('Content-type' => $image->contentType), 'body' => $image->file->getBytes())); } return new Response(array('location' => '/li3b_users/img/default-profile-picture.png')); });
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use \lithium\net\http\Router; // Route for reading a blog post (note the "url" named parameter) Router::connect('/blog/read/{:url}', array('controller' => 'pages', 'action' => 'read', 'page_type' => 'blog')); // Yes, you can render "static" pages from the library as well by using the "view" action, // just ensure "page_type" is set. Templates from: /libraries/blog/views/pages/static/template-name.html.php Router::connect('/blog/view/{:url}', array('controller' => 'pages', 'action' => 'view', 'page_type' => 'blog')); // would use update from blog library (using core templates if not present) //Router::connect('/blogs/update/{:url}', array('controller' => 'pages', 'action' => 'update')); // would use core layout because admin is set to true and would then look for update.html.php in the library and default back to core if not found ///Router::connect('/blogs/update/{:url}', array('admin' => true, 'controller' => 'pages', 'action' => 'update')); Router::connect('/blog/create', array('admin' => true, 'controller' => 'pages', 'action' => 'create', 'page_type' => 'blog')); // Route for listing all blog entries Router::connect('/blog', array('controller' => 'pages', 'action' => 'index', 'page_type' => 'blog')); ?>
public function testRunWithContinuingRules() { MockDispatcher::config(array('rules' => array('api' => array('action' => 'api_{:action}'), 'admin' => array('action' => 'admin_{:action}')))); Router::connect('/', array('controller' => 'test', 'action' => 'test', 'admin' => true, 'api' => true)); MockDispatcher::run(new Request(array('url' => '/'))); $result = end(MockDispatcher::$dispatched); $expected = array('action' => 'admin_api_test', 'controller' => 'Test', 'admin' => true, 'api' => true); $this->assertEqual($expected, $result->params); }
* or even duplicate routes to be written in each library's "routes.php" file. * * NOTE: This of course does not mean a plugin needs to have a public index action. * It also does not cover controllers in the base application. Note the "plugin" prefix. */ Router::connect('/plugin/{:library}/{:controller}/{:action}/page-{:page:[0-9]+}/limit-{:limit:[0-9]+}/sort-{:sort}/{:args}', array('action' => 'index'), array('persist' => array('controller', 'library'))); Router::connect('/plugin/{:library}/{:controller}/{:action}/page-{:page:[0-9]+}/limit-{:limit:[0-9]+}', array('action' => 'index'), array('persist' => array('controller', 'library'))); Router::connect('/plugin/{:library}/{:controller}/{:action}/page-{:page:[0-9]+}/sort-{:sort}', array('action' => 'index'), array('persist' => array('controller', 'library'))); Router::connect('/plugin/{:library}/{:controller}/{:action}/page-{:page:[0-9]+}/{:args}', array('action' => 'index'), array('persist' => array('controller', 'admin', 'library'))); Router::connect('/plugin/{:library}/{:controller}/{:action}/page-{:page:[0-9]+}', array('action' => 'index'), array('persist' => array('controller', 'admin', 'library'))); Router::connect("/plugin/{:library}/{:controller}/{:action}/{:args}", array('action' => 'index', 'args' => array()), array('persist' => array('controller', 'library'))); /** * Connect the "public" static pages. * NOTE: This is something that might very well be overwritten by the main app's routes. * * Remember, li3b_core static pages can always be used with: /plugin/li3b_core/pages/view/home * So even if the main application wants to repurpose the "/" URL, it can still use core static pages * which can have template overrides in the main app's views directory at: /views/_libraries/li3b_core/pages/... */ Router::connect("/", array('library' => 'li3b_core', 'controller' => 'pages', 'action' => 'view', 'args' => array('home'), 'persist' => false)); Router::connect("/page/{:args}", array('library' => 'li3b_core', 'controller' => 'pages', 'action' => 'view', 'args' => array('home'), 'persist' => false)); /** * Add the testing routes. These routes are only connected in non-production environments, and allow * browser-based access to the test suite for running unit and integration tests for the Lithium * core, as well as your own application and any other loaded plugins or frameworks. Browse to * [http://path/to/app/test](/test) to run tests. */ if (!Environment::is('production')) { Router::connect('/test/{:args}', array('controller' => 'lithium\\test\\Controller')); Router::connect('/test', array('controller' => 'lithium\\test\\Controller')); }
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\net\http\Router; use lithium\core\Libraries; $defaultConfig = array('login' => array('/login' => array('OpenIdSessions' => 'add')), 'logout' => array('/logout' => array('OpenIdSessions' => 'delete'))); // Override config if ($actionConfig = Libraries::get('li3_openid_auth', 'ActionConfig')) { foreach (array_keys($actionConfig) as $action) { $defaultConfig[$action] = $actionConfig[$action]; } } foreach (array_keys($defaultConfig) as $action) { $url = key($defaultConfig[$action]); $ctrl = key($defaultConfig[$action][$url]); $act = $defaultConfig[$action][$url][$ctrl]; Router::connect($url, array('library' => 'li3_openid_auth', 'controller' => $ctrl, 'action' => $act)); } //Router::connect('/dprlogin', 'Sessions::add'); //Router::connect('/logout', 'Sessions::delete');
<?php use lithium\net\http\Router; Router::connect('/bot', array('library' => 'li3_bot', 'controller' => 'pages', 'action' => 'home')); Router::connect('/bot/logs', array('library' => 'li3_bot', 'controller' => 'logs', 'action' => 'channels')); Router::connect('/bot/logs/{:channel}/{:date:[0-9]{4}-[0-9]{2}-[0-9]{2}}', array('library' => 'li3_bot', 'controller' => 'logs', 'action' => 'view')); Router::connect('/bot/logs/{:channel}/{:year}', array('library' => 'li3_bot', 'controller' => 'logs', 'action' => 'index')); Router::connect('/bot/logs/{:channel}', array('library' => 'li3_bot', 'controller' => 'logs', 'action' => 'index')); Router::connect('/bot/tells', array('library' => 'li3_bot', 'controller' => 'tells', 'action' => 'index'));
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\net\http\Router; // View non-published drafts: Router::connect('/testLeaderboard', array('controller' => 'Leaderboard', 'action' => 'index', 'library' => 'li3_leaderboard'));
* the primary key of the database object, and can be accessed in the controller as * `$this->request->id`. * * If you're using a relational database, such as MySQL, SQLite or Postgres, where the primary key * is an integer, uncomment the routes below to enable URLs like `/posts/edit/1138`, * `/posts/view/1138.json`, etc. */ // Router::connect('/{:controller}/{:action}/{:id:\d+}.{:type}', array('id' => null)); // Router::connect('/{:controller}/{:action}/{:id:\d+}'); /** * If you're using a document-oriented database, such as CouchDB or MongoDB, or another type of * database which uses 24-character hexidecimal values as primary keys, uncomment the routes below. */ // Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}.{:type}', array('id' => null)); // Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}'); /** * ### Default controller/action routes * * Finally, connect the default route. This route acts as a catch-all, intercepting requests in the * following forms: * * - `/foo/bar`: Routes to `FooController::bar()` with no parameters passed. * - `/foo/bar/param1/param2`: Routes to `FooController::bar('param1, 'param2')`. * - `/foo`: Routes to `FooController::index()`, since `'index'` is assumed to be the action if none * is otherwise specified. * * In almost all cases, custom routes should be added above this one, since route-matching works in * a top-down fashion. */ Router::connect('/{:controller}/{:action}/{:args}');
/** * Tests that routes can be connected and correctly match based on HTTP headers or method verbs. * * @return void */ public function testHttpMethodBasedRouting() { Router::connect('/{:controller}/{:id:[0-9]+}', array('http:method' => 'GET', 'action' => 'view')); Router::connect('/{:controller}/{:id:[0-9]+}', array('http:method' => 'PUT', 'action' => 'edit')); $request = new Request(array('url' => '/posts/13', 'env' => array('REQUEST_METHOD' => 'GET'))); $params = Router::process($request)->params; $expected = array('controller' => 'posts', 'action' => 'view', 'id' => '13'); $this->assertEqual($expected, $params); $this->assertEqual('/posts/13', Router::match($params)); $request = new Request(array('url' => '/posts/13', 'env' => array('REQUEST_METHOD' => 'PUT'))); $params = Router::process($request)->params; $expected = array('controller' => 'posts', 'action' => 'edit', 'id' => '13'); $this->assertEqual($expected, $params); $request = new Request(array('url' => '/posts/13', 'env' => array('REQUEST_METHOD' => 'POST'))); $params = Router::process($request)->params; $this->assertFalse($params); }
/** * Tests that continuations can be used for route suffixes. */ public function testSuffixContinuation() { Router::connect("/{:args}.{:type}", array(), array('continue' => true)); Router::connect('/{:controller}/{:id:[0-9]+}', array('action' => 'view')); $result = Router::match(array('controller' => 'versions', 'action' => 'view', 'id' => 13, 'type' => 'jsonp')); $this->assertEqual('/versions/13.jsonp', $result); $result = Router::match(array('controller' => 'versions', 'action' => 'view', 'id' => 13)); $this->assertEqual('/versions/13', $result); }
public function testLibraryBasedRoute() { $route = Router::connect('/{:library}/{:controller}/{:action}', array('library' => 'app'), array('persist' => array('library'))); $expected = '/app/hello/world'; $result = Router::match(array('controller' => 'hello', 'action' => 'world')); $this->assertEqual($expected, $result); $expected = '/myapp/hello/world'; $result = Router::match(array('library' => 'myapp', 'controller' => 'hello', 'action' => 'world')); $this->assertEqual($expected, $result); }