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', '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
*
@ -28,6 +30,25 @@ class RedirectRoute extends CakeRoute {
*/
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
* redirection
@ -43,16 +64,16 @@ class RedirectRoute extends CakeRoute {
if (!$this->response) {
$this->response = new CakeResponse();
}
$redirect = $this->defaults;
if (count($this->defaults) == 1 && !isset($this->defaults['controller'])) {
$redirect = $this->defaults[0];
$redirect = $this->redirect;
if (count($this->redirect) == 1 && !isset($this->redirect['controller'])) {
$redirect = $this->redirect[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'];

View file

@ -269,15 +269,18 @@ class Router {
*
* `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));`
*
* Redirects /posts/* to http://google.com with a HTTP status of 302
*
* ### Options:
*
* - `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 array $url A url to redirect to. Can be a string or a Cake array-based url