UrlGeneratorTrait.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 is removed from 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 is removed from drupal:9.0.0.
  32. * Use \Drupal\Core\Url::fromUri() instead.
  33. *
  34. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
  35. */
  36. protected function url($route_name, $route_parameters = [], $options = []) {
  37. @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::url() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Url::fromUri() instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  38. return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  39. }
  40. /**
  41. * Returns a redirect response object for the specified route.
  42. *
  43. * @param string $route_name
  44. * The name of the route to which to redirect.
  45. * @param array $route_parameters
  46. * (optional) Parameters for the route.
  47. * @param array $options
  48. * (optional) An associative array of additional options.
  49. * @param int $status
  50. * (optional) The HTTP redirect status code for the redirect. The default is
  51. * 302 Found.
  52. *
  53. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  54. * A redirect response object that may be returned by the controller.
  55. *
  56. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  57. * Use new RedirectResponse(Url::fromRoute()) instead.
  58. */
  59. protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
  60. @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::redirect() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use new RedirectResponse(Url::fromRoute()) instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  61. $options['absolute'] = TRUE;
  62. $url = $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  63. return new RedirectResponse($url, $status);
  64. }
  65. /**
  66. * Returns the URL generator service.
  67. *
  68. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  69. * The URL generator service.
  70. *
  71. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  72. * Use the url_generator service instead.
  73. */
  74. protected function getUrlGenerator() {
  75. @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::getUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the url_generator service instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  76. if (!$this->urlGenerator) {
  77. $this->urlGenerator = \Drupal::service('url_generator');
  78. }
  79. return $this->urlGenerator;
  80. }
  81. /**
  82. * Sets the URL generator service.
  83. *
  84. * @param \Drupal\Core\Routing\UrlGeneratorInterface $generator
  85. * The url generator service.
  86. *
  87. * @return $this
  88. *
  89. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  90. */
  91. public function setUrlGenerator(UrlGeneratorInterface $generator) {
  92. @trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::setUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  93. $this->urlGenerator = $generator;
  94. return $this;
  95. }
  96. }