controllers.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\BeforeAll;
  31. use demo\Middleware\Finish;
  32. use demo\Views\JsonView;
  33. //Request::setTrustedProxies(array('127.0.0.1'));
  34. // Middleware must be callables, or registered in the container.
  35. $app['mwAfterAll'] = function () { return new AfterAll(); };
  36. $app['mwBeforeAll'] = function () { return new BeforeAll(); };
  37. // This type of global configuration does not apply to mounted controllers,
  38. // which have their own "global" configuration.
  39. /* ---- Application middleware -------------------------------------------------
  40. Middleware is invoked as event subscribers on KernelEvents with a priority.
  41. Predefined priorities:
  42. - default == 0
  43. - Application::EARLY_EVENT == 512
  44. - Application::LATE_EVENT == -512.
  45. */
  46. // Usual service example with a method call.
  47. // "Before": triggered on KernelEvents::REQUEST.
  48. // Early before() middleware runs before routing and security, which can be
  49. // necessary to have it run even on 403/404 exceptions.
  50. $app->before('mwBeforeAll:next', $priority = 0);
  51. // Callable service example.
  52. // "After": triggered on KernelEvents::RESPONSE.
  53. $app->after('mwAfterAll');
  54. // Plain callable example.
  55. // "Finish": triggered on KernelEvents::TERMINATE.
  56. $app->finish([Finish::class, 'handle']);
  57. // ---- Routing ----------------------------------------------------------------
  58. // Demo Twig.
  59. $app->get('/', HomeController::class . '::home')
  60. ->bind('homepage');
  61. // Demo redirects and forwards.
  62. $app->get('/home', DelegatingController::class . '::redirectPath');
  63. $app->get('/blogz', DelegatingController::class . '::forwardPath');
  64. $app->get('/all_blogs', DelegatingController::class . '::forwardName');
  65. // Demo typical entity listing routes with their modifiers.
  66. $app->get('/blogs', BlogController::class . '::index')
  67. ->bind('blog_list');
  68. $app->get('/blogs-json', BlogController::class . '::json');
  69. $app->get('/blogs-json-view', BlogController::class . '::jsonView');
  70. $app->get('/blog/{id}', BlogController::class . '::fifiAction')
  71. ->assert('id', '\d+')
  72. ->when("request.headers.get('User-Agent') matches '/firefox/i'");
  73. $app->get('/blog/{id}', BlogController::class . '::show')
  74. ->assert('id', '\d+')
  75. ->value('id', 1)
  76. ->bind('blog_post');
  77. // Demo POST request handling.
  78. $app->post('/feedback', FeedbackController::class . '::feedbackAction');
  79. // Demo escaping HTML and JSON
  80. $app->get('/hello/{name}', EscapeController::class . '::html');
  81. $app->get('/hello-json/{name}', EscapeController::class . '::json');
  82. // Demo streaming.
  83. $app->get('/noise', StreamController::class . '::customStream');
  84. $app->get('/pass', StreamController::class . '::fileStream');
  85. // Demo error handling.
  86. $app->get('err/http', ErrorController::class . '::errorHttp');
  87. $app->get('err/base', ErrorController::class . '::errorBase');
  88. // Demo param converters.
  89. $app->get('/user/{user}', UserController::class . '::itemAction')
  90. ->convert('user', 'converter.user:convert');
  91. /* ---- View handlers ----------------------------------------------------------
  92. View handlers can also receive Request $request as 2nd arg e.g. for basic
  93. content negotiation. But the callback_resolver service:
  94. - can not receive $app
  95. - unlike the controller_resolver, can only use standard callables/services
  96. - unlike the exception listener, can not use _invoke-able class names.
  97. So one way to get the app is to use it as a global (!).
  98. They are triggered on KernelEvents::VIEW.
  99. */
  100. $app->view([JsonView::class, 'handle']);
  101. /* ---- Exception handlers -----------------------------------------------------
  102. Handlers are examined in order, and called in a chain with the result of the
  103. previous one. The last one must return a string or Response.
  104. They are triggered on KernelEvents::EXCEPTION.
  105. */
  106. $app->error([HttpErrorHandler::class, 'handle']);
  107. // Handlers do not receive $app, so they have to be closures or instantiated
  108. // here if they need it (or use the global $app like view handlers.
  109. $app->error(new GenericErrorHandler($app));