controllers.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @file
  4. * Contains the controller/route/error bindings, along with middleware.
  5. *
  6. * Controller resolution:
  7. * @see \Silex\ControllerResolver
  8. * @see \Symfony\Component\HttpKernel\Controller\ControllerResolver
  9. *
  10. * Error handler resolution:
  11. * @see \Silex\ExceptionListenerWrapper
  12. *
  13. * View resolution:
  14. * @see \Silex\ViewListenerWrapper
  15. *
  16. * Other resolutions:
  17. * @see \Silex\Provider\HttpKernelServiceProvider::subscribe()
  18. */
  19. use demo\Controllers\DelegatingController;
  20. use demo\Controllers\ErrorController;
  21. use demo\Controllers\EscapeController;
  22. use demo\Controllers\FeedbackController;
  23. use demo\Controllers\HomeController;
  24. use demo\Controllers\StreamController;
  25. use demo\Controllers\UserController;
  26. use demo\Errors\GenericErrorHandler;
  27. use demo\Errors\HttpErrorHandler;
  28. use demo\Middleware\AfterAll;
  29. use demo\Middleware\AfterRoute;
  30. use demo\Middleware\BeforeAll;
  31. use demo\Middleware\BeforeRoute;
  32. use demo\Middleware\Finish;
  33. use demo\Views\JsonView;
  34. use Silex\Application;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\HttpFoundation\Response;
  37. //Request::setTrustedProxies(array('127.0.0.1'));
  38. // Middleware must be callables, or registered in the container.
  39. $app['mwAfterAll'] = function () { return new AfterAll(); };
  40. $app['mwBeforeAll'] = function () { return new BeforeAll(); };
  41. // This type of global configuration does not apply to mounted controllers,
  42. // which have their own "global" configuration.
  43. /* ---- Application middleware -------------------------------------------------
  44. Middleware is invoked as event subscribers on KernelEvents with a priority.
  45. Predefined priorities:
  46. - default == 0
  47. - Application::EARLY_EVENT == 512
  48. - Application::LATE_EVENT == -512.
  49. */
  50. // Usual service example with a method call.
  51. // "Before": triggered on KernelEvents::REQUEST.
  52. // Early before() middleware runs before routing and security, which can be
  53. // necessary to have it run even on 403/404 exceptions.
  54. $app->before('mwBeforeAll:next', $priority = 0);
  55. // Callable service example.
  56. // "After": triggered on KernelEvents::RESPONSE.
  57. $app->after('mwAfterAll', $priority = 0);
  58. // Plain callable example.
  59. // "Finish": triggered on KernelEvents::TERMINATE.
  60. $app->finish([Finish::class, 'handle']);
  61. /* ---- Route middleware -------------------------------------------------------
  62. Middleware is invoked as event subscribers on KernelEvents with fixed priority.
  63. - After() middleware is called on KernelEvents::Response.
  64. - Fixed priority 128 means before the Application after() default 0.
  65. - App middleware EARLY_EVENT == 512 > 0 => app MW before route MW
  66. - App middleware 0 or LATE_EVENT == 512 > 0 => app MW after route MW
  67. - It receives Request, Response, Application.
  68. - Before() middleware is called on KernelEvents::Request.
  69. - App middleware EARLY_EVENT / 0 / LATE_EVENT are all > -1024 means global
  70. before() middleware is /always/ invoked before any before() route
  71. middleware.
  72. - Using manual values < -1024 allow running route middleware earlier, but with
  73. undefined consequences since Silex expectations are no longer abided by.
  74. - It receives Request, Application.
  75. All route middlewares for a route are called in order. Since route middleware
  76. have no priority of they own, making them come outside the normal order means
  77. changing the priority of the global middleware.
  78. Route middleware is resolved by the callback resolved, so suffers the same
  79. limitations as view or error handlers, but since they receive $app, this does
  80. not matter much. */
  81. // ---- Routing ----------------------------------------------------------------
  82. // Demo Twig.
  83. $app->get('/', HomeController::class . '::home')
  84. ->bind('homepage');
  85. // Demo redirects and forwards.
  86. $app->get('/home', DelegatingController::class . '::redirectPath');
  87. $app->get('/blogz', DelegatingController::class . '::forwardPath');
  88. $app->get('/all_blogs', DelegatingController::class . '::forwardName');
  89. // Using a mounted route collection
  90. $blog = require_once __DIR__ . '/controllers-blog.php';
  91. // This will handle /blogs/ and below, AND /blogs : see the 'blog_mount' route.
  92. $app->mount('/blogs', $blog);
  93. // Demo POST request handling.
  94. $app->post('/feedback', FeedbackController::class . '::feedbackAction');
  95. // Demo escaping HTML and JSON
  96. $app->get('/hello/{name}', EscapeController::class . '::html');
  97. $app->get('/hello-json/{name}', EscapeController::class . '::json');
  98. // Demo streaming.
  99. $app->get('/noise', StreamController::class . '::customStream');
  100. $app->get('/pass', StreamController::class . '::fileStream');
  101. $app->get('/skip', function () {})
  102. ->after([AfterRoute::class, 'next'])
  103. ->before([BeforeRoute::class, 'next']);
  104. // Demo error handling.
  105. $app->get('err/http', ErrorController::class . '::errorHttp');
  106. $app->get('err/base', ErrorController::class . '::errorBase');
  107. // Demo param converters.
  108. $app->get('/user/{user}', UserController::class . '::itemAction')
  109. ->convert('user', 'converter.user:convert');
  110. /* ---- View handlers ----------------------------------------------------------
  111. View handlers receive the controller result, and can also receive Request
  112. $request as 2nd arg e.g. for basic content negotiation. But the
  113. callback_resolver service:
  114. - can not receive $app
  115. - unlike the controller_resolver, can only use standard callables/services
  116. - unlike the exception listener, can not use _invoke-able class names.
  117. So one way to get the app is to use it as a global (!).
  118. They are triggered on KernelEvents::VIEW.
  119. */
  120. $app->view([JsonView::class, 'handle']);
  121. /* ---- Exception handlers -----------------------------------------------------
  122. Handlers are examined in order, and called in a chain with the result of the
  123. previous one. The last one must return a string or Response.
  124. They are triggered on KernelEvents::EXCEPTION.
  125. */
  126. $app->error([HttpErrorHandler::class, 'handle']);
  127. // Handlers do not receive $app, so they have to be closures or instantiated
  128. // here if they need it (or use the global $app) like view handlers.
  129. $app->error(new GenericErrorHandler($app));