controllers.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // Normal service with a method call. "Before": Triggered on KE::REQUEST.
  40. $app->before('mwBeforeAll:next');
  41. // Callable service. "After": triggered on KE::RESPONSE.
  42. $app->after('mwAfterAll');
  43. // Plain callable. "Finish": triggered on KE::TERMINATE.
  44. $app->finish([Finish::class, 'handle']);
  45. // Demo Twig.
  46. $app->get('/', HomeController::class . '::home')
  47. ->bind('homepage');
  48. // Demo redirects and forwards.
  49. $app->get('/home', DelegatingController::class . '::redirectPath');
  50. $app->get('/blogz', DelegatingController::class . '::forwardPath');
  51. $app->get('/all_blogs', DelegatingController::class . '::forwardName');
  52. // Demo typical entity listing routes with their modifiers.
  53. $app->get('/blogs', BlogController::class . '::index')
  54. ->bind('blog_list');
  55. $app->get('/blogs-json', BlogController::class . '::json');
  56. $app->get('/blogs-json-view', BlogController::class . '::jsonView');
  57. $app->get('/blog/{id}', BlogController::class . '::fifiAction')
  58. ->assert('id', '\d+')
  59. ->when("request.headers.get('User-Agent') matches '/firefox/i'");
  60. $app->get('/blog/{id}', BlogController::class . '::show')
  61. ->assert('id', '\d+')
  62. ->value('id', 1)
  63. ->bind('blog_post');
  64. // Demo POST request handling.
  65. $app->post('/feedback', FeedbackController::class . '::feedbackAction');
  66. // Demo escaping HTML and JSON
  67. $app->get('/hello/{name}', EscapeController::class . '::html');
  68. $app->get('/hello-json/{name}', EscapeController::class . '::json');
  69. // Demo streaming.
  70. $app->get('/noise', StreamController::class . '::customStream');
  71. $app->get('/pass', StreamController::class . '::fileStream');
  72. // Demo error handling.
  73. $app->get('err/http', ErrorController::class . '::errorHttp');
  74. $app->get('err/base', ErrorController::class . '::errorBase');
  75. // Demo param converters.
  76. $app->get('/user/{user}', UserController::class . '::itemAction')
  77. ->convert('user', 'converter.user:convert');
  78. // Register a view handler. They can also receive Request $request as 2nd arg,
  79. // e.g. for basic content negotiation. But the callback_resolver service:
  80. // - can not receive $app
  81. // - unlike the controller_resolver, can only use standard callables/services
  82. // - unlike the exception listener, can not use _invoke-able class names.
  83. // So one way to get the app is to use it as a global (!).
  84. // They are triggered on KE::VIEW.
  85. $app->view([JsonView::class, 'handle']);
  86. // Handlers are examined in order, and called in a chain with the result of the
  87. // previous one. The last one must return a string or Response.
  88. // They are triggered on KE::EXCEPTION.
  89. $app->error([HttpErrorHandler::class, 'handle']);
  90. // Handlers do not receive $app, so they have to be closures or instantiated
  91. // here if they need it (or use the global $app like view handlers.
  92. $app->error(new GenericErrorHandler($app));