123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * @file
- * Contains the controller/route/error bindings, along with middleware.
- *
- * Controller resolution:
- * @see \Silex\ControllerResolver
- * @see \Symfony\Component\HttpKernel\Controller\ControllerResolver
- *
- * Error handler resolution:
- * @see \Silex\ExceptionListenerWrapper
- *
- * View resolution:
- * @see \Silex\ViewListenerWrapper
- *
- * Other resolutions:
- * @see \Silex\Provider\HttpKernelServiceProvider::subscribe()
- */
- use demo\Controllers\BlogController;
- use demo\Controllers\DelegatingController;
- use demo\Controllers\ErrorController;
- use demo\Controllers\EscapeController;
- use demo\Controllers\FeedbackController;
- use demo\Controllers\HomeController;
- use demo\Controllers\StreamController;
- 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;
- //Request::setTrustedProxies(array('127.0.0.1'));
- // Middleware must be callables, or registered in the container.
- $app['mwAfterAll'] = function () { return new AfterAll(); };
- $app['mwBeforeAll'] = function () { return new BeforeAll(); };
- // This type of global configuration does not apply to mounted controllers,
- // which have their own "global" configuration.
- /* ---- Application middleware -------------------------------------------------
- Middleware is invoked as event subscribers on KernelEvents with a priority.
- Predefined priorities:
- - default == 0
- - Application::EARLY_EVENT == 512
- - Application::LATE_EVENT == -512.
- */
- // Usual service example with a method call.
- // "Before": triggered on KernelEvents::REQUEST.
- // Early before() middleware runs before routing and security, which can be
- // necessary to have it run even on 403/404 exceptions.
- $app->before('mwBeforeAll:next', $priority = 0);
- // Callable service example.
- // "After": triggered on KernelEvents::RESPONSE.
- $app->after('mwAfterAll', $priority = 0);
- // Plain callable example.
- // "Finish": triggered on KernelEvents::TERMINATE.
- $app->finish([Finish::class, 'handle']);
- /* ---- Route middleware -------------------------------------------------------
- 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. */
- // ---- Routing ----------------------------------------------------------------
- // Demo Twig.
- $app->get('/', HomeController::class . '::home')
- ->bind('homepage');
- // Demo redirects and forwards.
- $app->get('/home', DelegatingController::class . '::redirectPath');
- $app->get('/blogz', DelegatingController::class . '::forwardPath');
- $app->get('/all_blogs', DelegatingController::class . '::forwardName');
- // Demo typical entity listing routes with their modifiers.
- $app->get('/blogs', BlogController::class . '::index')
- ->bind('blog_list');
- $app->get('/blogs-json', BlogController::class . '::json');
- $app->get('/blogs-json-view', BlogController::class . '::jsonView');
- $app->get('/blog/{id}', BlogController::class . '::fifiAction')
- ->assert('id', '\d+')
- ->when("request.headers.get('User-Agent') matches '/firefox/i'");
- $app->get('/blog/{id}', BlogController::class . '::show')
- ->assert('id', '\d+')
- ->value('id', 1)
- ->bind('blog_post');
- // Demo POST request handling.
- $app->post('/feedback', FeedbackController::class . '::feedbackAction');
- // Demo escaping HTML and JSON
- $app->get('/hello/{name}', EscapeController::class . '::html');
- $app->get('/hello-json/{name}', EscapeController::class . '::json');
- // Demo streaming.
- $app->get('/noise', StreamController::class . '::customStream');
- $app->get('/pass', StreamController::class . '::fileStream');
- $app->get('/skip', function () {})
- ->after([AfterRoute::class, 'next'])
- ->before([BeforeRoute::class, 'next']);
- // Demo error handling.
- $app->get('err/http', ErrorController::class . '::errorHttp');
- $app->get('err/base', ErrorController::class . '::errorBase');
- // Demo param converters.
- $app->get('/user/{user}', UserController::class . '::itemAction')
- ->convert('user', 'converter.user:convert');
- /* ---- View handlers ----------------------------------------------------------
- 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.
- So one way to get the app is to use it as a global (!).
- They are triggered on KernelEvents::VIEW.
- */
- $app->view([JsonView::class, 'handle']);
- /* ---- Exception handlers -----------------------------------------------------
- Handlers are examined in order, and called in a chain with the result of the
- previous one. The last one must return a string or Response.
- They are triggered on KernelEvents::EXCEPTION.
- */
- $app->error([HttpErrorHandler::class, 'handle']);
- // Handlers do not receive $app, so they have to be closures or instantiated
- // here if they need it (or use the global $app) like view handlers.
- $app->error(new GenericErrorHandler($app));
|