controllers.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. use demo\Controllers\BlogController;
  3. use demo\Controllers\FeedbackController;
  4. use demo\Controllers\UserController;
  5. use Silex\Application;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Exception\HttpException;
  9. //Request::setTrustedProxies(array('127.0.0.1'));
  10. // Fixed parameters for after() global middleware.
  11. $afterAllMiddleware = function (Request $request, Response $response, Application $app) {
  12. echo "<p>In aAM</p>\n";
  13. };
  14. $beforeAllMiddleware = function (Request $request, Application $app) {
  15. echo "<p>In bAM</p>\n";
  16. };
  17. // This type of global configuration does not apply to mounted controllers,
  18. // which have their own "global" configuration.
  19. $app->before($beforeAllMiddleware);
  20. $app->after($afterAllMiddleware);
  21. $app->get('/', function () use ($app) {
  22. return $app['twig']->render('index.html.twig', []);
  23. })->bind('homepage');
  24. $blogPosts = [
  25. 1 => [
  26. 'date' => '2011-03-29',
  27. 'author' => 'igorw',
  28. 'title' => 'Using Silex',
  29. 'body' => '...It takes time on version changes...',
  30. ],
  31. 2 => [
  32. 'date' => '2015-03-29',
  33. 'author' => 'igorw',
  34. 'title' => 'Using Silex 2',
  35. 'body' => '...Especialy S1 to S2...',
  36. ],
  37. ];
  38. // Available automatic arguments on controllers: Application, Request.
  39. $app->get('/blogs', function () use ($blogPosts) {
  40. $output = "<ul>\n";
  41. foreach ($blogPosts as $post) {
  42. $output .= "<li>" . $post ['title'] . "</li>\n";
  43. }
  44. $output .= "</ul>\n";
  45. return $output;
  46. });
  47. // Default: http://blog, not http://blog/
  48. $app->get('/blog/{id}', BlogController::class . '::fifiAction')
  49. ->assert('id', '\d+')
  50. ->when("request.headers.get('User-Agent') matches '/firefox/i'");
  51. $app->get('/blog/{id}', function (Application $app, $id) use ($blogPosts) {
  52. if (!isset ($blogPosts [$id])) {
  53. $app->abort(Response::HTTP_NOT_FOUND, "Post $id does not exist.");
  54. }
  55. $post = $blogPosts [$id];
  56. return "<h1> {$post['title']}</h1>" . "<p> {$post['body']} </p>";
  57. })->assert('id', '\d+')
  58. ->value('id', 1)
  59. ->bind('blog_post');
  60. $app->post('/feedback', FeedbackController::class . '::feedbackAction');
  61. $app->get('/user/{user}', UserController::class . '::itemAction')
  62. ->convert('user', 'converter.user:convert');
  63. /* Error handlers receive exactly these arguments, in that order. */
  64. $app->error(function (HttpException $e, Request $request, $code) {
  65. $response = new Response('HTTP Error caught: skipping generic error handler.',
  66. // This status is ignored by Silex, which tries to ensure status code
  67. // consistency with the exception.
  68. Response::HTTP_NOT_FOUND,
  69. // But we can force it with this fake header, which is removed before
  70. // sending the response to the user agent.
  71. ['X-Status-Code' => Response::HTTP_OK]);
  72. return $response;
  73. });
  74. $app->error(function (\Exception $e, Request $request, $code) use ($app) {
  75. if ($app['debug']) {
  76. return;
  77. }
  78. // 404.html, or 40x.html, or 4xx.html, or error.html
  79. $templates = [
  80. 'errors/' . $code . '.html.twig',
  81. 'errors/' . substr($code, 0, 2) . 'x.html.twig',
  82. 'errors/' . substr($code, 0, 1) . 'xx.html.twig',
  83. 'errors/default.html.twig',
  84. ];
  85. return new Response($app['twig']->resolveTemplate($templates)
  86. ->render(['code' => $code]), $code);
  87. });