controllers.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. use demo\Controllers\BlogController;
  3. use demo\Controllers\DelegatingController;
  4. use demo\Controllers\ErrorController;
  5. use demo\Controllers\EscapeController;
  6. use demo\Controllers\FeedbackController;
  7. use demo\Controllers\HomeController;
  8. use demo\Controllers\StreamController;
  9. use demo\Controllers\UserController;
  10. use demo\Errors\GenericErrorHandler;
  11. use demo\Errors\HttpErrorHandler;
  12. use demo\Views\JsonView;
  13. use Silex\Application;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\HttpException;
  18. //Request::setTrustedProxies(array('127.0.0.1'));
  19. // Fixed parameters for after() global middleware.
  20. $afterAllMiddleware = function (Request $request, Response $response, Application $app) {
  21. $MESSAGE = "<p>In aAM</p>\n";
  22. if ($response instanceof JsonResponse) {
  23. $content = $response->getContent();
  24. $raw = json_decode($content, TRUE);
  25. $raw[] = $MESSAGE;
  26. $newContent = json_encode($raw);
  27. $response->setContent($newContent);
  28. return $response;
  29. }
  30. // Only echo info on text responses.
  31. $ct = $response->headers->get('Content-Type');
  32. if (isset($ct) && strpos($ct, 'text') !== 0) {
  33. return;
  34. }
  35. echo $MESSAGE;
  36. };
  37. $beforeAllMiddleware = function (Request $request, Application $app) {
  38. // Only add info on requests URIs containing "json".
  39. if (!preg_match('/json/', $request->getRequestUri())) {
  40. echo "<p>In bAM</p>\n";
  41. }
  42. };
  43. // This type of global configuration does not apply to mounted controllers,
  44. // which have their own "global" configuration.
  45. $app->before($beforeAllMiddleware);
  46. $app->after($afterAllMiddleware);
  47. // Demo Twig.
  48. $app->get('/', HomeController::class . '::home')
  49. ->bind('homepage');
  50. // Demo redirects and forwards.
  51. $app->get('/home', DelegatingController::class . '::redirectPath');
  52. $app->get('/blogz', DelegatingController::class . '::forwardPath');
  53. $app->get('/all_blogs', DelegatingController::class . '::forwardName');
  54. // Demo typical entity listing routes with their modifiers.
  55. $app->get('/blogs', BlogController::class . '::index')
  56. ->bind('blog_list');
  57. $app->get('/blogs-json', BlogController::class . '::json');
  58. $app->get('/blogs-json-view', BlogController::class . '::jsonView');
  59. $app->get('/blog/{id}', BlogController::class . '::fifiAction')
  60. ->assert('id', '\d+')
  61. ->when("request.headers.get('User-Agent') matches '/firefox/i'");
  62. $app->get('/blog/{id}', BlogController::class . '::show')
  63. ->assert('id', '\d+')
  64. ->value('id', 1)
  65. ->bind('blog_post');
  66. // Demo POST request handling.
  67. $app->post('/feedback', FeedbackController::class . '::feedbackAction');
  68. // Demo escaping HTML and JSON
  69. $app->get('/hello/{name}', EscapeController::class . '::html');
  70. $app->get('/hello-json/{name}', EscapeController::class . '::json');
  71. // Demo streaming.
  72. $app->get('/noise', StreamController::class . '::customStream');
  73. $app->get('/pass', StreamController::class . '::fileStream');
  74. // Demo error handling.
  75. $app->get('err/http', ErrorController::class . '::errorHttp');
  76. $app->get('err/base', ErrorController::class . '::errorBase');
  77. // Demo param converters.
  78. $app->get('/user/{user}', UserController::class . '::itemAction')
  79. ->convert('user', 'converter.user:convert');
  80. // Register a view handler. They can also receive Request $request as 2nd arg,
  81. // e.g. for basic content negotiation. But the callback_resolver service:
  82. // - can not receive $app
  83. // - unlike the controller_resolver, can only use standard callables/services
  84. // - unlike the exception listener, can not use _invoke-able class names.
  85. // So one way to get the app is to use it as a global (!).
  86. $app->view([JsonView::class, 'handle']);
  87. // Handlers are examined in order, and called in a chain with the result of the
  88. // previous one. The last one must return a string or Response.
  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));