UrlGeneratorTrait.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. /**
  5. * Wrapper methods for the Url Generator.
  6. *
  7. * This utility trait should only be used in application-level code, such as
  8. * classes that would implement ContainerInjectionInterface. Services registered
  9. * in the Container should not use this trait but inject the appropriate service
  10. * directly for easier testing.
  11. *
  12. * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
  13. * Use \Drupal\Core\Url instead.
  14. */
  15. trait UrlGeneratorTrait {
  16. /**
  17. * The url generator.
  18. *
  19. * @var \Drupal\Core\Routing\UrlGeneratorInterface
  20. */
  21. protected $urlGenerator;
  22. /**
  23. * Generates a URL or path for a specific route based on the given parameters.
  24. *
  25. * For details on the arguments, usage, and possible exceptions see
  26. * \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute().
  27. *
  28. * @return string
  29. * The generated URL for the given route.
  30. *
  31. * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
  32. * Use \Drupal\Core\Url instead.
  33. *
  34. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
  35. */
  36. protected function url($route_name, $route_parameters = [], $options = []) {
  37. return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  38. }
  39. /**
  40. * Returns a redirect response object for the specified route.
  41. *
  42. * @param string $route_name
  43. * The name of the route to which to redirect.
  44. * @param array $route_parameters
  45. * (optional) Parameters for the route.
  46. * @param array $options
  47. * (optional) An associative array of additional options.
  48. * @param int $status
  49. * (optional) The HTTP redirect status code for the redirect. The default is
  50. * 302 Found.
  51. *
  52. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  53. * A redirect response object that may be returned by the controller.
  54. */
  55. protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
  56. $options['absolute'] = TRUE;
  57. $url = $this->url($route_name, $route_parameters, $options);
  58. return new RedirectResponse($url, $status);
  59. }
  60. /**
  61. * Returns the URL generator service.
  62. *
  63. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  64. * The URL generator service.
  65. */
  66. protected function getUrlGenerator() {
  67. if (!$this->urlGenerator) {
  68. $this->urlGenerator = \Drupal::service('url_generator');
  69. }
  70. return $this->urlGenerator;
  71. }
  72. /**
  73. * Sets the URL generator service.
  74. *
  75. * @param \Drupal\Core\Routing\UrlGeneratorInterface $generator
  76. * The url generator service.
  77. *
  78. * @return $this
  79. */
  80. public function setUrlGenerator(UrlGeneratorInterface $generator) {
  81. $this->urlGenerator = $generator;
  82. return $this;
  83. }
  84. }