Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace App\Error\Middleware;
use Cake\Error\Middleware\ErrorHandlerMiddleware as CoreErrorHandlerMiddleware;
use Zend\Diactoros\Response\RedirectResponse;
/**
* Error handling middleware.
*
* Checks for old urls and 301 redirects to corresonding new ones.
*/
class ErrorHandlerMiddleware extends CoreErrorHandlerMiddleware
{
public function __construct($renderer = null, array $config = [])
{
$this->_defaultConfig['exceptions'] = [
'Cake\Routing\Exception\MissingControllerException',
'Cake\Controller\Exception\MissingActionException',
];
parent::__construct($renderer, $config);
}
public function handleException($exception, $request, $response)
{
if (!in_array(get_class($exception), $this->config('exceptions'), true)) {
return parent::handleException($exception, $request, $response);
}
$newUrl = $this->getNewUrl();
if ($newUrl) {
reuturn new RedirectResponse(
$newUrl,
301
);
}
return parent::handleException($exception, $request, $response);
}
protected function getNewUrl()
{
// login here to check if new url exists for existing url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.