/** * Returns the target (eg. URL) for the controller and action * * @param string $controller The name of the controller * @param string $action The name of the action * @param array $params Associative array with the route params, if they are required. * * @return string The target. * * @throws RouterException On errors. (Including if the route is not found) */ public function getTargetForControllerAction($controller, $action, $params = array()) { // We only put the language in the URI if it is not the default $languagePrefix = $this->currentLanguage == $this->defaultLanguage ? '' : '/' . $this->currentLanguage; $uri = parent::getTargetForControllerAction($controller, $action, $params); if (empty($languagePrefix)) { return $uri; } else { return $languagePrefix . rtrim($uri, '/'); } }
/** * Tests if the reverse routing works correctly with multiple routes for the same controller and action. * * @return void */ public function testMultiRoute() { // Make sure the routes are arranged in a way, where the ordering does not matter $rules = array('Multiroute/Multiparam' => array('/multiroute/multiparam/{param1:alnum}', '/multiroute/multiparam/{param1:alnum}/{param2:alnum}/{param3:alnum}', '/multiroute/multiparam', '/multiroute/multiparam/{param1:alnum}/{param2:alnum}')); $reverseRouter = new ArrayReverseRouter($rules); // Check if the not parameterised routing works $this->assertEquals('/multiroute/multiparam', $reverseRouter->getTargetForControllerAction('Multiroute', 'Multiparam'), 'Invalid result for non-parameterised route'); // Check if the route with 1 param works $this->assertEquals('/multiroute/multiparam/test1', $reverseRouter->getTargetForControllerAction('Multiroute', 'Multiparam', array('param1' => 'test1')), 'Invalid result for 1 param route'); // Check if the route with 2 param works $this->assertEquals('/multiroute/multiparam/test1/test2', $reverseRouter->getTargetForControllerAction('Multiroute', 'Multiparam', array('param1' => 'test1', 'param2' => 'test2')), 'Invalid result for 2 param route'); // Check if the route with 3 param works $this->assertEquals('/multiroute/multiparam/test1/test2/test3', $reverseRouter->getTargetForControllerAction('Multiroute', 'Multiparam', array('param1' => 'test1', 'param2' => 'test2', 'param3' => 'test3')), 'Invalid result for 3 param route'); // Check if a param with an invalid name correctly throws an error try { $reverseRouter->getTargetForControllerAction('Multiroute', 'Multiparam', array('param' => 'test1')); $this->fail('No exception is thrown for a route with an invalid param'); } catch (RouterException $e) { $this->assertContains('No exact route match for controller and action', $e->getMessage(), 'Invalid exception message'); } }