DelegatingController.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace demo\Controllers;
  3. use Silex\Application;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\HttpKernelInterface;
  7. /**
  8. * Class DelegatingController demonstrates delegation of response building.
  9. *
  10. * It shows redirection to a path, and forwarding to a subrequest using either
  11. * a path or a named route.
  12. *
  13. * @package demo\Controllers
  14. */
  15. class DelegatingController {
  16. // Redirect via response header
  17. public function redirectPath(Application $app) {
  18. return $app->redirect('/', Response::HTTP_TEMPORARY_REDIRECT); // Default 302.
  19. }
  20. // Forward to another controller to avoid redirection, using a path.
  21. public function forwardPath(Application $app) {
  22. $subRequest = Request::create('/blogs', 'GET');
  23. return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  24. }
  25. public function forwardName(Application $app) {
  26. /** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator */
  27. $generator = $app['url_generator'];
  28. $subRequest = Request::create($generator->generate('blog_list', 'GET'));
  29. return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  30. }
  31. }