Skip to content

Commit

Permalink
Proof of concept approach for Redirect exception.
Browse files Browse the repository at this point in the history
Tests, docs and other requirements are coming soon.
  • Loading branch information
markstory committed Jan 20, 2016
1 parent ad38687 commit 6f6be20
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/Routing/Exception/RedirectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Cake\Routing\Exception;

use RuntimeException;

class RedirectException extends RuntimeException
{
}
16 changes: 12 additions & 4 deletions src/Routing/Filter/RoutingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;
use Cake\Routing\Exception\RedirectException;
use Cake\Routing\Router;

/**
Expand All @@ -42,16 +43,23 @@ class RoutingFilter extends DispatcherFilter
* If Routes have not been loaded they will be loaded, and config/routes.php will be run.
*
* @param \Cake\Event\Event $event containing the request, response and additional params
* @return void
* @return void|Cake\Network\Response A response will be returned when a redirect route is encountered.
*/
public function beforeDispatch(Event $event)
{
$request = $event->data['request'];
Router::setRequestInfo($request);

if (empty($request->params['controller'])) {
$params = Router::parse($request->url);
$request->addParams($params);
try {
if (empty($request->params['controller'])) {
$params = Router::parse($request->url);
$request->addParams($params);
}
} catch (RedirectException $e) {
$response = $event->data['response'];
$response->statusCode($e->getCode());
$response->header('Location', $e->getMessage());
return $response;
}
}
}
8 changes: 2 additions & 6 deletions src/Routing/Route/RedirectRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Routing\Route;

use Cake\Network\Response;
use Cake\Routing\Exception\RedirectException;
use Cake\Routing\Router;

/**
Expand Down Expand Up @@ -91,12 +92,7 @@ public function parse($url)
if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
$status = $this->options['status'];
}
$this->response->header([
'Location' => Router::url($redirect, true)
]);
$this->response->statusCode($status);
$this->response->send();
$this->response->stop();
throw new RedirectException(Router::url($redirect, true), $status);
}

/**
Expand Down

0 comments on commit 6f6be20

Please sign in to comment.