mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 18:12:15 +00:00
Merge pull request #552 from skipperbent/v4-exception-callback-fix
SimpleRouter::error not working within group
This commit is contained in:
@@ -425,16 +425,11 @@ class SimpleRouter
|
|||||||
*/
|
*/
|
||||||
public static function error(Closure $callback): CallbackExceptionHandler
|
public static function error(Closure $callback): CallbackExceptionHandler
|
||||||
{
|
{
|
||||||
$routes = static::router()->getRoutes();
|
|
||||||
|
|
||||||
$callbackHandler = new CallbackExceptionHandler($callback);
|
$callbackHandler = new CallbackExceptionHandler($callback);
|
||||||
|
|
||||||
$group = new RouteGroup();
|
static::router()->addRoute(
|
||||||
$group->addExceptionHandler($callbackHandler);
|
(new RouteGroup())->addExceptionHandler($callbackHandler)
|
||||||
|
);
|
||||||
array_unshift($routes, $group);
|
|
||||||
|
|
||||||
static::router()->setRoutes($routes);
|
|
||||||
|
|
||||||
return $callbackHandler;
|
return $callbackHandler;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,29 @@ class RouterCallbackExceptionHandlerTest extends \PHPUnit\Framework\TestCase
|
|||||||
throw new ExceptionHandlerException();
|
throw new ExceptionHandlerException();
|
||||||
});
|
});
|
||||||
|
|
||||||
TestRouter::debugNoReset('/404-url', 'get');
|
TestRouter::debug('/404-url');
|
||||||
TestRouter::router()->reset();
|
}
|
||||||
|
|
||||||
$this->assertTrue(true);
|
public function testExceptionHandlerCallback() {
|
||||||
|
|
||||||
|
TestRouter::group(['prefix' => null], function() {
|
||||||
|
TestRouter::get('/', function() {
|
||||||
|
return 'Hello world';
|
||||||
|
});
|
||||||
|
|
||||||
|
TestRouter::get('/not-found', 'DummyController@method1');
|
||||||
|
TestRouter::error(function(\Pecee\Http\Request $request, \Exception $exception) {
|
||||||
|
|
||||||
|
if($exception instanceof \Pecee\SimpleRouter\Exceptions\NotFoundHttpException && $exception->getCode() === 404) {
|
||||||
|
return $request->setRewriteCallback(static function() {
|
||||||
|
return 'success';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$result = TestRouter::debugOutput('/thisdoes-not/existssss', 'get');
|
||||||
|
$this->assertEquals('success', $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user