‪TYPO3CMS  9.5
UriBuilder.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
25 
35 {
39  const ‪ABSOLUTE_URL = 'url';
40 
44  const ‪ABSOLUTE_PATH = 'absolute';
45 
49  protected ‪$router;
50 
54  protected ‪$generated = [];
55 
60  public function ‪__construct(‪Router ‪$router = null)
61  {
62  $this->router = ‪$router ?? GeneralUtility::makeInstance(Router::class);
63  }
64 
77  public function ‪buildUriFromRoutePath($pathInfo, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
78  {
79  ‪$router = GeneralUtility::makeInstance(Router::class);
80  $route = ‪$router->‪match($pathInfo);
81  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
82  }
83 
96  public function ‪buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
97  {
98  $cacheIdentifier = 'route' . $name . serialize($parameters) . $referenceType;
99  if (isset($this->generated[$cacheIdentifier])) {
100  return $this->generated[$cacheIdentifier];
101  }
102  if (!isset($this->router->getRoutes()[$name])) {
103  throw new RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
104  }
105 
106  $route = $this->router->getRoutes()[$name];
107  $parameters = array_merge(
108  $route->getOptions()['parameters'] ?? [],
109  $parameters
110  );
111 
112  // If the route has the "public" option set, no token is generated.
113  if ($route->getOption('access') !== 'public') {
114  $parameters = [
115  'token' => ‪FormProtectionFactory::get('backend')->‪generateToken('route', $name)
116  ] + $parameters;
117  }
118 
119  // Add the Route path as &route=XYZ
120  $parameters = [
121  'route' => $route->getPath()
122  ] + $parameters;
123 
124  $this->generated[$cacheIdentifier] = $this->‪buildUri($parameters, $referenceType);
125  return $this->generated[$cacheIdentifier];
126  }
127 
138  public function ‪buildUriFromModule($moduleName, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
139  {
140  trigger_error('UriBuilder->buildUriFromModule() will be removed in TYPO3 v10.0, use buildUriFromRoute() instead.', E_USER_DEPRECATED);
141  $cacheIdentifier = 'module' . $moduleName . serialize($parameters) . $referenceType;
142  if (isset($this->generated[$cacheIdentifier])) {
143  return $this->generated[$cacheIdentifier];
144  }
145  $parameters = [
146  'route' => $moduleName,
147  'token' => ‪FormProtectionFactory::get('backend')->‪generateToken('route', $moduleName)
148  ] + $parameters;
149  $this->generated[$cacheIdentifier] = $this->‪buildUri($parameters, $referenceType);
150  return $this->generated[$cacheIdentifier];
151  }
152 
161  protected function ‪buildUri($parameters, $referenceType)
162  {
163  $uri = 'index.php' . ‪HttpUtility::buildQueryString($parameters, '?');
164  if ($referenceType === self::ABSOLUTE_PATH) {
166  } else {
167  $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $uri;
168  }
169  return GeneralUtility::makeInstance(Uri::class, $uri);
170  }
171 }
‪TYPO3\CMS\Backend\Routing\UriBuilder\__construct
‪__construct(Router $router=null)
Definition: UriBuilder.php:58
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\get
‪static TYPO3 CMS Core FormProtection AbstractFormProtection get($classNameOrType='default',... $constructorArguments)
Definition: FormProtectionFactory.php:72
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection\generateToken
‪string generateToken($formName, $action='', $formInstanceName='')
Definition: AbstractFormProtection.php:82
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromModule
‪Uri buildUriFromModule($moduleName, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:136
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪Uri buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:94
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:27
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\Backend\Routing\UriBuilder\$router
‪Router $router
Definition: UriBuilder.php:48
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_PATH
‪const ABSOLUTE_PATH
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\Routing\UriBuilder\$generated
‪array $generated
Definition: UriBuilder.php:52
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUri
‪Uri buildUri($parameters, $referenceType)
Definition: UriBuilder.php:159
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:160
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:223
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoutePath
‪Uri buildUriFromRoutePath($pathInfo, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:75
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:45
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Backend\Routing\Router\match
‪Route match($pathInfo)
Definition: Router.php:67
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:39
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:42
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:32
‪TYPO3\CMS\Backend\Routing