Skip to content

Instantly share code, notes, and snippets.

@ADmad
Last active May 16, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ADmad/606beddce65495d37d5e0bce1e2a7c04 to your computer and use it in GitHub Desktop.
Save ADmad/606beddce65495d37d5e0bce1e2a7c04 to your computer and use it in GitHub Desktop.
<?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