diff --git a/src/Pluf/Dispatcher.php b/src/Pluf/Dispatcher.php index 2dbf6b3..54f9c49 100644 --- a/src/Pluf/Dispatcher.php +++ b/src/Pluf/Dispatcher.php @@ -121,7 +121,7 @@ class Pluf_Dispatcher foreach ($GLOBALS['_PX_views'] as $key => $ctl) { $match = array(); if (preg_match($ctl['regex'], $req->query, $match)) { - $req->view = $ctl; + $req->view = array($ctl, $match); $m = new $ctl['model'](); if (isset($m->{$ctl['method'].'_precond'})) { // Here we have preconditions to respects. If @@ -163,7 +163,15 @@ class Pluf_Dispatcher } if ($firstpass and substr($req->query, -1) != '/') { $req->query .= '/'; - return self::match($req, false); + $res = self::match($req, false); + if ($res->status_code != 404) { + Pluf::loadFunction('Pluf_HTTP_URL_urlForView'); + $name = (isset($req->view[0]['name'])) ? + $req->view[0]['name'] : + $req->view[0]['model'].'::'.$req->view[0]['method']; + $url = Pluf_HTTP_URL_urlForView($name, array_slice($req->view[1], 1)); + return new Pluf_HTTP_Response_Redirect($url, 301); + } } return new Pluf_HTTP_Response_NotFound($req); } diff --git a/src/Pluf/HTTP/Response/Redirect.php b/src/Pluf/HTTP/Response/Redirect.php index b892e77..495837c 100644 --- a/src/Pluf/HTTP/Response/Redirect.php +++ b/src/Pluf/HTTP/Response/Redirect.php @@ -23,11 +23,17 @@ class Pluf_HTTP_Response_Redirect extends Pluf_HTTP_Response { - function __construct($url) + /** + * Redirect response to a given URL. + * + * @param string URL + * @paran int Redirect code (302) or 301 for permanent + */ + function __construct($url, $code=302) { $content = sprintf(__('Please, click here to be redirected.'), $url); parent::__construct($content); $this->headers['Location'] = $url; - $this->status_code = 302; + $this->status_code = $code; } }