Wrapper method which takes a Request object, parses it through all attached Route
objects, assigns the resulting parameters to the Request object, and returns it.
public static process ( |
||
$request | ||
return | Returns a copy of the request with parameters applied. |
/** * Returns the corresponding params for a given URL and an optional request * method. * * Examples: * ``` * 1: li3 route show /foo * 2: li3 route show post /foo/bar/1 * 3: li3 route show /test * 4: li3 route show /test --env=production * ``` * * Will return outputs similar to: * * ``` * 1: {"controller":"foo","action":"index" } * 2: {"controller":"foo","action":"bar","args":["1"]} * 3: {"controller":"lithium\\test\\Controller","action":"index"} * 4: {"controller":"test","action":"index"} * ``` * * @return void */ public function show() { $url = join(" ", $this->request->params['args']); $method = 'GET'; if (!$url) { $this->error('Please provide a valid URL'); } if (preg_match('/^(GET|POST|PUT|DELETE|HEAD|OPTIONS) (.+)/i', $url, $matches)) { $method = strtoupper($matches[1]); $url = $matches[2]; } $request = new Request(compact('url') + array('env' => array('REQUEST_METHOD' => $method))); $result = Router::process($request); $this->out($result->params ? json_encode($result->params) : "No route found."); }
/** * Tests that multiple continuation routes can be applied to the same URL. */ public function testStackedContinuationRoutes() { Router::connect('/admin/{:args}', array('admin' => true), array('continue' => true)); Router::connect('/{:locale:en|de|it|jp}/{:args}', array(), array('continue' => true)); Router::connect('/{:controller}/{:action}/{:id:[0-9]+}', array('id' => null)); $request = new Request(array('url' => '/en/foo/bar/5')); $expected = array('controller' => 'foo', 'action' => 'bar', 'id' => '5', 'locale' => 'en'); $this->assertEqual($expected, Router::process($request)->params); $request = new Request(array('url' => '/admin/foo/bar/5')); $expected = array('controller' => 'foo', 'action' => 'bar', 'id' => '5', 'admin' => true); $this->assertEqual($expected, Router::process($request)->params); $request = new Request(array('url' => '/admin/de/foo/bar/5')); $expected = array('controller' => 'foo', 'action' => 'bar', 'id' => '5', 'locale' => 'de', 'admin' => true); $this->assertEqual($expected, Router::process($request)->params); $request = new Request(array('url' => '/en/admin/foo/bar/5')); $this->assertFalse(Router::process($request)->params); $result = Router::match(array('Foo::bar', 'id' => 5)); $this->assertEqual('/foo/bar/5', $result); $result = Router::match(array('Foo::bar', 'id' => 5, 'admin' => true)); $this->assertEqual('/admin/foo/bar/5', $result); $result = Router::match(array('Foo::bar', 'id' => 5, 'admin' => true, 'locale' => 'jp')); $this->assertEqual('/admin/jp/foo/bar/5', $result); }
public function testMatchWithAbsoluteScope() { Router::attach('app', array('absolute' => true, 'host' => '{:domain}')); Router::scope('app', function () { Router::connect('/hello', 'Posts::index'); }); $request = new Request(array('url' => '/hello', 'base' => '')); $result = Router::process($request); $expected = 'http://' . $result->params['domain'] . '/hello'; $result = Router::match($result->params, $request); $this->assertEqual($expected, $result); }
public function testMatchOverRequestParamsWithScope() { Router::scope('app1', function () { Router::connect('/hello/world1', 'HelloApp1::index'); }); Router::scope('app2', function () { Router::connect('/hello/world2', 'HelloApp2::index'); }); $request = new Request(array('url' => '/hello/world1')); $result = Router::process($request); $expected = array('controller' => 'HelloApp1', 'action' => 'index', 'library' => 'app1'); $this->assertEqual($expected, $result->params); $result = Router::match($result->params); $this->assertEqual('/hello/world1', $result); $request = new Request(array('url' => '/hello/world2')); $result = Router::process($request); $expected = array('controller' => 'HelloApp2', 'action' => 'index', 'library' => 'app2'); $this->assertEqual($expected, $result->params); $result = Router::match($result->params); $this->assertEqual('/hello/world2', $result); }
/** * 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); }
public function testProcessWithAbsoluteAttachmentAndLibrary() { $request = new Request(array('url' => '/hello_world/index', 'env' => array('HTTP_HOST' => 'bob.amiga.com', 'HTTPS' => false))); Router::attach('app', array('absolute' => true, 'host' => '{:subdomain}.amiga.{:tld}', 'scheme' => 'http', 'library' => 'other')); Router::scope('app', function () { Router::connect('/hello_world/index', array('HelloWorld::index')); Router::connect('/{:controller}/{:action}'); }); Router::process($request); $expected = array('library' => 'other', 'controller' => 'HelloWorld', 'action' => 'index', 'subdomain' => 'bob', 'tld' => 'com'); $this->assertEqual($expected, $request->params); $result = Router::attached('app'); $this->assertEqual($expected, $result['values']); $request = new Request(array('url' => '/posts/index', 'env' => array('HTTP_HOST' => 'bob.amiga.com', 'HTTPS' => false))); Router::process($request); $expected = array('library' => 'other', 'controller' => 'Posts', 'action' => 'index', 'subdomain' => 'bob', 'tld' => 'com'); $this->assertEqual($expected, $request->params); $result = Router::attached('app'); $this->assertEqual($expected, $result['values']); }