RouterHelper.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\Templating\Helper;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Templating\Helper\Helper;
  13. /**
  14. * RouterHelper manages links between pages in a template context.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class RouterHelper extends Helper
  19. {
  20. protected $generator;
  21. public function __construct(UrlGeneratorInterface $router)
  22. {
  23. $this->generator = $router;
  24. }
  25. /**
  26. * Generates a URL from the given parameters.
  27. *
  28. * @param string $name The name of the route
  29. * @param mixed $parameters An array of parameters
  30. * @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
  31. *
  32. * @return string The generated URL
  33. *
  34. * @see UrlGeneratorInterface
  35. */
  36. public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
  37. {
  38. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0. Use the "path" or "url" method instead.', E_USER_DEPRECATED);
  39. return $this->generator->generate($name, $parameters, $referenceType);
  40. }
  41. /**
  42. * Generates a URL reference (as an absolute or relative path) to the route with the given parameters.
  43. *
  44. * @param string $name The name of the route
  45. * @param mixed $parameters An array of parameters
  46. * @param bool $relative Whether to generate a relative or absolute path
  47. *
  48. * @return string The generated URL reference
  49. *
  50. * @see UrlGeneratorInterface
  51. */
  52. public function path($name, $parameters = array(), $relative = false)
  53. {
  54. return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
  55. }
  56. /**
  57. * Generates a URL reference (as an absolute URL or network path) to the route with the given parameters.
  58. *
  59. * @param string $name The name of the route
  60. * @param mixed $parameters An array of parameters
  61. * @param bool $schemeRelative Whether to omit the scheme in the generated URL reference
  62. *
  63. * @return string The generated URL reference
  64. *
  65. * @see UrlGeneratorInterface
  66. */
  67. public function url($name, $parameters = array(), $schemeRelative = false)
  68. {
  69. return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getName()
  75. {
  76. return 'router';
  77. }
  78. }