Last active
May 16, 2018 14:33
-
-
Save ADmad/606beddce65495d37d5e0bce1e2a7c04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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