InternalController.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\Response;
  13. /**
  14. * InternalController.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class InternalController extends ContainerAware
  19. {
  20. /**
  21. * Forwards to the given controller with the given path.
  22. *
  23. * @param string $path The path
  24. * @param string $controller The controller name
  25. *
  26. * @return Response A Response instance
  27. */
  28. public function indexAction($path, $controller)
  29. {
  30. // safeguard
  31. if (!is_string($controller)) {
  32. throw new \RuntimeException('A Controller must be a string.');
  33. }
  34. // check that the controller looks like a controller
  35. if (false === strpos($controller, '::')) {
  36. $count = substr_count($controller, ':');
  37. if (2 == $count) {
  38. // the convention already enforces the Controller suffix
  39. } elseif (1 == $count) {
  40. // controller in the service:method notation
  41. list($service, $method) = explode(':', $controller, 2);
  42. $class = get_class($this->container->get($service));
  43. if (!preg_match('/Controller$/', $class)) {
  44. throw new \RuntimeException('A Controller class name must end with Controller.');
  45. }
  46. } else {
  47. throw new \LogicException('Unable to parse the Controller name.');
  48. }
  49. } else {
  50. list($class, $method) = explode('::', $controller, 2);
  51. if (!preg_match('/Controller$/', $class)) {
  52. throw new \RuntimeException('A Controller class name must end with Controller.');
  53. }
  54. }
  55. $request = $this->container->get('request');
  56. $attributes = $request->attributes;
  57. $attributes->remove('path');
  58. $attributes->remove('controller');
  59. if ('none' !== $path) {
  60. parse_str($path, $tmp);
  61. $attributes->add($tmp);
  62. }
  63. return $this->container->get('http_kernel')->forward($controller, $attributes->all(), $request->query->all());
  64. }
  65. }