|
@@ -29,9 +29,14 @@ use demo\Controllers\UserController;
|
|
|
use demo\Errors\GenericErrorHandler;
|
|
|
use demo\Errors\HttpErrorHandler;
|
|
|
use demo\Middleware\AfterAll;
|
|
|
+use demo\Middleware\AfterRoute;
|
|
|
use demo\Middleware\BeforeAll;
|
|
|
+use demo\Middleware\BeforeRoute;
|
|
|
use demo\Middleware\Finish;
|
|
|
use demo\Views\JsonView;
|
|
|
+use Silex\Application;
|
|
|
+use Symfony\Component\HttpFoundation\Request;
|
|
|
+use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
|
@@ -59,12 +64,36 @@ $app->before('mwBeforeAll:next', $priority = 0);
|
|
|
|
|
|
|
|
|
|
|
|
-$app->after('mwAfterAll');
|
|
|
+$app->after('mwAfterAll', $priority = 0);
|
|
|
|
|
|
|
|
|
|
|
|
$app->finish([Finish::class, 'handle']);
|
|
|
|
|
|
+
|
|
|
+Middleware is invoked as event subscribers on KernelEvents with fixed priority.
|
|
|
+
|
|
|
+- After() middleware is called on KernelEvents::Response.
|
|
|
+ - Fixed priority 128 means before the Application after() default 0.
|
|
|
+ - App middleware EARLY_EVENT == 512 > 0 => app MW before route MW
|
|
|
+ - App middleware 0 or LATE_EVENT == 512 > 0 => app MW after route MW
|
|
|
+ - It receives Request, Response, Application.
|
|
|
+- Before() middleware is called on KernelEvents::Request.
|
|
|
+ - App middleware EARLY_EVENT / 0 / LATE_EVENT are all > -1024 means global
|
|
|
+ before() middleware is /always/ invoked before any before() route
|
|
|
+ middleware.
|
|
|
+ - Using manual values < -1024 allow running route middleware earlier, but with
|
|
|
+ undefined consequences since Silex expectations are no longer abided by.
|
|
|
+ - It receives Request, Application.
|
|
|
+
|
|
|
+All route middlewares for a route are called in order. Since route middleware
|
|
|
+have no priority of they own, making them come outside the normal order means
|
|
|
+changing the priority of the global middleware.
|
|
|
+
|
|
|
+Route middleware is resolved by the callback resolved, so suffers the same
|
|
|
+limitations as view or error handlers, but since they receive $app, this does
|
|
|
+not matter much. */
|
|
|
+
|
|
|
|
|
|
|
|
|
$app->get('/', HomeController::class . '::home')
|
|
@@ -98,6 +127,9 @@ $app->get('/hello-json/{name}', EscapeController::class . '::json');
|
|
|
|
|
|
$app->get('/noise', StreamController::class . '::customStream');
|
|
|
$app->get('/pass', StreamController::class . '::fileStream');
|
|
|
+$app->get('/skip', function () {})
|
|
|
+ ->after([AfterRoute::class, 'next'])
|
|
|
+ ->before([BeforeRoute::class, 'next']);
|
|
|
|
|
|
|
|
|
$app->get('err/http', ErrorController::class . '::errorHttp');
|
|
@@ -108,8 +140,9 @@ $app->get('/user/{user}', UserController::class . '::itemAction')
|
|
|
->convert('user', 'converter.user:convert');
|
|
|
|
|
|
|
|
|
-View handlers can also receive Request $request as 2nd arg e.g. for basic
|
|
|
-content negotiation. But the callback_resolver service:
|
|
|
+View handlers receive the controller result, and can also receive Request
|
|
|
+$request as 2nd arg e.g. for basic content negotiation. But the
|
|
|
+callback_resolver service:
|
|
|
- can not receive $app
|
|
|
- unlike the controller_resolver, can only use standard callables/services
|
|
|
- unlike the exception listener, can not use _invoke-able class names.
|
|
@@ -127,5 +160,5 @@ They are triggered on KernelEvents::EXCEPTION.
|
|
|
*/
|
|
|
$app->error([HttpErrorHandler::class, 'handle']);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
$app->error(new GenericErrorHandler($app));
|