controllers.php 6.5 KB

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