Updating doc block for Router::redirect()

Updating RedirectRoute to not use defaults for where the route will redirect.
This commit is contained in:
mark_story 2010-12-18 15:36:22 -05:00
parent 3b0a3d4109
commit 5255b8fc9e
2 changed files with 31 additions and 7 deletions

View file

@ -2,7 +2,9 @@
App::import('Core', 'CakeResponse'); App::import('Core', 'CakeResponse');
App::import('Core', 'route/CakeRoute'); App::import('Core', 'route/CakeRoute');
/** /**
* Redirect route will perform an immediate redirect * Redirect route will perform an immediate redirect. Redirect routes
* are useful when you want to have Routing layer redirects occur in your
* application, for when URLs move.
* *
* PHP5 * PHP5
* *
@ -28,6 +30,25 @@ class RedirectRoute extends CakeRoute {
*/ */
public $response = null; public $response = null;
/**
* The location to redirect to. Either a string or a cake array url.
*
* @var mixed
*/
public $redirect;
/**
* Constructor
*
* @param string $template Template string with parameter placeholders
* @param array $defaults Array of defaults for the route.
* @param string $params Array of parameters and additional options for the Route
*/
public function __construct($template, $defaults = array(), $options = array()) {
parent::__construct($template, $defaults, $options);
$this->redirect = (array)$defaults;
}
/** /**
* Parses a string url into an array. Parsed urls will result in an automatic * Parses a string url into an array. Parsed urls will result in an automatic
* redirection * redirection
@ -43,9 +64,9 @@ class RedirectRoute extends CakeRoute {
if (!$this->response) { if (!$this->response) {
$this->response = new CakeResponse(); $this->response = new CakeResponse();
} }
$redirect = $this->defaults; $redirect = $this->redirect;
if (count($this->defaults) == 1 && !isset($this->defaults['controller'])) { if (count($this->redirect) == 1 && !isset($this->redirect['controller'])) {
$redirect = $this->defaults[0]; $redirect = $this->redirect[0];
} }
if (isset($this->options['persist']) && is_array($redirect)) { if (isset($this->options['persist']) && is_array($redirect)) {
$argOptions['context'] = array('action' => $redirect['action'], 'controller' => $redirect['controller']); $argOptions['context'] = array('action' => $redirect['action'], 'controller' => $redirect['controller']);

View file

@ -269,15 +269,18 @@ class Router {
* *
* `Router::redirect('/home/*', array('controller' => 'posts', 'action' => 'view', array('persist' => true));` * `Router::redirect('/home/*', array('controller' => 'posts', 'action' => 'view', array('persist' => true));`
* *
* Redirects /home/* to /posts/view and passes the parameters to /posts/view * Redirects /home/* to /posts/view and passes the parameters to /posts/view. Using an array as the
* redirect destination allows you to use other routes to define where a url string should be redirected ot.
* *
* `Router::redirect('/posts/*', 'http://google.com', array('status' => 302));` * `Router::redirect('/posts/*', 'http://google.com', array('status' => 302));`
* *
* Redirects /posts/* to http://google.com with a HTTP status of 302 * Redirects /posts/* to http://google.com with a HTTP status of 302
* *
* ### Options: * ### Options:
*
* - `status` Sets the HTTP status (default 301) * - `status` Sets the HTTP status (default 301)
* - `persist` Passes the params to the redirected route, if it can * - `persist` Passes the params to the redirected route, if it can. This is useful with greedy routes,
* routes that end in `*` are greedy. As you can remap urls and not loose any passed/named args.
* *
* @param string $route A string describing the template of the route * @param string $route A string describing the template of the route
* @param array $url A url to redirect to. Can be a string or a Cake array-based url * @param array $url A url to redirect to. Can be a string or a Cake array-based url