‪TYPO3CMS  11.5
UriBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ServerRequestInterface;
30 
40 {
44  public const ‪ABSOLUTE_URL = 'url';
45 
49  public const ‪ABSOLUTE_PATH = 'absolute';
50 
54  public const ‪SHAREABLE_URL = 'share';
55 
59  protected ‪$router;
60 
64  protected ‪$generated = [];
65 
70  public function ‪__construct(‪Router ‪$router)
71  {
72  $this->router = ‪$router;
73  }
74 
87  public function ‪buildUriFromRoutePath($pathInfo, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
88  {
89  $route = $this->router->match($pathInfo);
90  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
91  }
92 
105  public function ‪buildUriWithRedirect(string $name, array $parameters = [], RouteRedirect $redirect = null, string $referenceType = self::ABSOLUTE_PATH): Uri
106  {
107  if ($redirect === null) {
108  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
109  }
110 
111  try {
112  $redirect->resolve($this->router);
113  } catch (RouteNotFoundException|RouteTypeNotAllowedException|MethodNotAllowedException $e) {
114  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
115  }
116  $parameters['redirect'] = $redirect->getName();
117  if ($redirect->hasParameters()) {
118  $parameters['redirectParams'] = $redirect->getFormattedParameters();
119  }
120  return $this->‪buildUriFromRoute($name, $parameters, $referenceType);
121  }
122 
135  public function ‪buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
136  {
137  $cacheIdentifier = 'route' . $name . serialize($parameters) . $referenceType;
138  if (isset($this->generated[$cacheIdentifier])) {
139  return $this->generated[$cacheIdentifier];
140  }
141 
142  $route = $this->router->getRouteCollection()->get((string)$name);
143  if ($route === null) {
144  throw new RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
145  }
146 
147  $parameters = array_merge(
148  $route->getOption('parameters') ?? [],
149  $parameters
150  );
151 
152  // If the route is not shareable and doesn't have the "public" option set, a token must be generated.
153  if ($referenceType !== self::SHAREABLE_URL && (!$route->hasOption('access') || $route->getOption('access') !== 'public')) {
154  $parameters = [
155  'token' => ‪FormProtectionFactory::get('backend')->‪generateToken('route', $name),
156  ] + $parameters;
157  }
158 
159  $this->generated[$cacheIdentifier] = $this->‪buildUri($route->getPath(), $parameters, (string)$referenceType);
160  return $this->generated[$cacheIdentifier];
161  }
162 
172  protected function ‪buildUri(string $route, array $parameters, string $referenceType): Uri
173  {
174  $path = ltrim($route . ‪HttpUtility::buildQueryString($parameters, '?'), '/');
175  if ($referenceType === self::ABSOLUTE_PATH) {
177  } elseif ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
178  && ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams') instanceof NormalizedParams
179  ) {
180  $uri = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getSiteUrl() . TYPO3_mainDir . $path;
181  } else {
182  $uri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . $path;
183  }
184  return GeneralUtility::makeInstance(Uri::class, $uri);
185  }
186 }
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\get
‪static TYPO3 CMS Core FormProtection AbstractFormProtection get($classNameOrType='default',... $constructorArguments)
Definition: FormProtectionFactory.php:73
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection\generateToken
‪string generateToken($formName, $action='', $formInstanceName='')
Definition: AbstractFormProtection.php:75
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUri
‪Uri buildUri(string $route, array $parameters, string $referenceType)
Definition: UriBuilder.php:170
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:23
‪TYPO3\CMS\Backend\Routing\UriBuilder\__construct
‪__construct(Router $router)
Definition: UriBuilder.php:68
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪Uri buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:133
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Backend\Routing\UriBuilder\SHAREABLE_URL
‪const SHAREABLE_URL
Definition: UriBuilder.php:54
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\Backend\Routing\UriBuilder\$router
‪Router $router
Definition: UriBuilder.php:58
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_PATH
‪const ABSOLUTE_PATH
Definition: UriBuilder.php:49
‪TYPO3\CMS\Backend\Routing\UriBuilder\$generated
‪array $generated
Definition: UriBuilder.php:62
‪TYPO3\CMS\Backend\Routing\RouteRedirect
Definition: RouteRedirect.php:30
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:171
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:276
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoutePath
‪Uri buildUriFromRoutePath($pathInfo, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:85
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:48
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:22
‪TYPO3\CMS\Backend\Routing\Exception\RouteTypeNotAllowedException
Definition: RouteTypeNotAllowedException.php:25
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriWithRedirect
‪Uri buildUriWithRedirect(string $name, array $parameters=[], RouteRedirect $redirect=null, string $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:103
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35