controllers.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. //Request::setTrustedProxies(array('127.0.0.1'));
  9. // Fixed parameters for after() global middleware.
  10. $afterAllMiddleware = function (Request $request, Response $response, Application $app) {
  11. echo "<p>In aAM</p>\n";
  12. };
  13. $beforeAllMiddleware = function (Request $request, Application $app) {
  14. echo "<p>In bAM</p>\n";
  15. };
  16. // This type of global configuration does not apply to mounted controllers,
  17. // which have their own "global" configuration.
  18. $app->before($beforeAllMiddleware);
  19. $app->after($afterAllMiddleware);
  20. $app->get('/', function () use ($app) {
  21. return $app['twig']->render('index.html.twig', []);
  22. })->bind('homepage');
  23. $blogPosts = [
  24. 1 => [
  25. 'date' => '2011-03-29',
  26. 'author' => 'igorw',
  27. 'title' => 'Using Silex',
  28. 'body' => '...It takes time on version changes...',
  29. ],
  30. 2 => [
  31. 'date' => '2015-03-29',
  32. 'author' => 'igorw',
  33. 'title' => 'Using Silex 2',
  34. 'body' => '...Especialy S1 to S2...',
  35. ],
  36. ];
  37. // Available automatic arguments on controllers: Application, Request.
  38. $app->get('/blogs', function () use ($blogPosts) {
  39. $output = "<ul>\n";
  40. foreach ($blogPosts as $post) {
  41. $output .= "<li>" . $post ['title'] . "</li>\n";
  42. }
  43. $output .= "</ul>\n";
  44. return $output;
  45. });
  46. // Default: http://blog, not http://blog/
  47. $app->get('/blog/{id}', BlogController::class . '::fifiAction')
  48. ->assert('id', '\d+')
  49. ->when("request.headers.get('User-Agent') matches '/firefox/i'");
  50. $app->get('/blog/{id}', function (Application $app, $id) use ($blogPosts) {
  51. if (!isset ($blogPosts [$id])) {
  52. $app->abort(Response::HTTP_NOT_FOUND, "Post $id does not exist.");
  53. }
  54. $post = $blogPosts [$id];
  55. return "<h1> {$post['title']}</h1>" . "<p> {$post['body']} </p>";
  56. })->assert('id', '\d+')
  57. ->value('id', 1)
  58. ->bind('blog_post');
  59. $app->post('/feedback', FeedbackController::class . '::feedbackAction');
  60. $app->get('/user/{user}', UserController::class . '::itemAction')
  61. ->convert('user', 'converter.user:convert');
  62. /* Error handlers receive exactly these arguments, in that order. */
  63. $app->error(function (\Exception $e, Request $request, $code) use ($app) {
  64. if ($app['debug']) {
  65. return;
  66. }
  67. // 404.html, or 40x.html, or 4xx.html, or error.html
  68. $templates = [
  69. 'errors/' . $code . '.html.twig',
  70. 'errors/' . substr($code, 0, 2) . 'x.html.twig',
  71. 'errors/' . substr($code, 0, 1) . 'xx.html.twig',
  72. 'errors/default.html.twig',
  73. ];
  74. return new Response($app['twig']->resolveTemplate($templates)
  75. ->render(['code' => $code]), $code);
  76. });