Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for redirection routes. Fixes #1151
- Loading branch information
1 parent
692aafb
commit 7630580
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
App::import('Core', 'CakeResponse'); | ||
App::import('Core', 'route/CakeRoute'); | ||
/** | ||
* Redirect route will perform an immediate redirect | ||
* | ||
* PHP5 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @package cake | ||
* @subpackage cake.cake.libs.route | ||
* @since CakePHP(tm) v 2.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
class RedirectRoute extends CakeRoute { | ||
|
||
/** | ||
* A CakeResponse object | ||
* | ||
* @var CakeResponse | ||
*/ | ||
public $response = null; | ||
|
||
/** | ||
* Parses a string url into an array. Parsed urls will result in an automatic | ||
* redirection | ||
* | ||
* @param string $url The url to parse | ||
* @return boolean False on failure | ||
*/ | ||
public function parse($url) { | ||
$params = parent::parse($url); | ||
if (!$params) { | ||
return false; | ||
} | ||
if (!$this->response) { | ||
$this->response = new CakeResponse(); | ||
} | ||
$redirect = $this->defaults; | ||
if (count($this->defaults) == 1 && !isset($this->defaults['controller'])) { | ||
$redirect = $this->defaults[0]; | ||
} | ||
if (isset($this->options['persist']) && is_array($redirect)) { | ||
$argOptions['context'] = array('action' => $redirect['action'], 'controller' => $redirect['controller']); | ||
$args = Router::getArgs($params['_args_'], $argOptions); | ||
$redirect += $args['pass']; | ||
$redirect += $args['named']; | ||
} | ||
$status = 301; | ||
if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) { | ||
$status = $this->options['status']; | ||
} | ||
$this->response->header(array('Location' => Router::url($redirect, true))); | ||
$this->response->statusCode($status); | ||
$this->response->send(); | ||
} | ||
|
||
/** | ||
* There is no reverse routing redirection routes | ||
* | ||
* @param array $url Array of parameters to convert to a string. | ||
* @return mixed either false or a string url. | ||
*/ | ||
public function match($url) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
App::import('Core', 'route/RedirectRoute'); | ||
App::import('Core', 'CakeResponse'); | ||
App::import('Core', 'Router'); | ||
/** | ||
* test case for RedirectRoute | ||
* | ||
* @package cake.tests.libs.route | ||
*/ | ||
class RedirectRouteTestCase extends CakeTestCase { | ||
/** | ||
* setUp method | ||
* | ||
* @return void | ||
*/ | ||
function setUp() { | ||
parent::setUp(); | ||
Configure::write('Routing', array('admin' => null, 'prefixes' => array())); | ||
Router::reload(); | ||
} | ||
|
||
/** | ||
* test the parsing of routes. | ||
* | ||
* @return void | ||
*/ | ||
function testParsing() { | ||
$route = new RedirectRoute('/home', array('controller' => 'posts')); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/home'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true))); | ||
|
||
$route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index')); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/home'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true))); | ||
$this->assertEqual($route->response->statusCode(), 301); | ||
|
||
$route = new RedirectRoute('/google', 'http://google.com'); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/google'); | ||
$this->assertEqual($route->response->header(), array('Location' => 'http://google.com')); | ||
|
||
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302)); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/posts/2'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view', true))); | ||
$this->assertEqual($route->response->statusCode(), 302); | ||
|
||
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true)); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/posts/2'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view/2', true))); | ||
|
||
$route = new RedirectRoute('/posts/*', '/test', array('persist' => true)); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/posts/2'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/test', true))); | ||
|
||
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true)); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/my_controllers/do_something/passme/named:param'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add/passme/named:param', true))); | ||
|
||
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add')); | ||
$route->response = $this->getMock('CakeResponse', array('_sendHeader')); | ||
$result = $route->parse('/my_controllers/do_something/passme/named:param'); | ||
$this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add', true))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters