‪TYPO3CMS  10.4
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;
28 
38 {
42  const ‪ABSOLUTE_URL = 'url';
43 
47  const ‪ABSOLUTE_PATH = 'absolute';
48 
52  protected ‪$router;
53 
57  protected ‪$generated = [];
58 
63  public function ‪__construct(‪Router ‪$router = null)
64  {
65  $this->router = ‪$router ?? GeneralUtility::makeInstance(Router::class);
66  }
67 
80  public function ‪buildUriFromRoutePath($pathInfo, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
81  {
82  ‪$router = GeneralUtility::makeInstance(Router::class);
83  $route = ‪$router->‪match($pathInfo);
84  return $this->‪buildUriFromRoute($route->getOption('_identifier'), $parameters, $referenceType);
85  }
86 
99  public function ‪buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
100  {
101  $cacheIdentifier = 'route' . $name . serialize($parameters) . $referenceType;
102  if (isset($this->generated[$cacheIdentifier])) {
103  return $this->generated[$cacheIdentifier];
104  }
105  if (!isset($this->router->getRoutes()[$name])) {
106  throw new RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
107  }
108 
109  $route = $this->router->getRoutes()[$name];
110  $parameters = array_merge(
111  $route->getOptions()['parameters'] ?? [],
112  $parameters
113  );
114 
115  // If the route has the "public" option set, no token is generated.
116  if ($route->getOption('access') !== 'public') {
117  $parameters = [
118  'token' => ‪FormProtectionFactory::get('backend')->‪generateToken('route', $name)
119  ] + $parameters;
120  }
121 
122  // Add the Route path as &route=XYZ
123  $parameters = [
124  'route' => $route->getPath()
125  ] + $parameters;
126 
127  $this->generated[$cacheIdentifier] = $this->‪buildUri($parameters, $referenceType);
128  return $this->generated[$cacheIdentifier];
129  }
130 
139  protected function ‪buildUri($parameters, $referenceType)
140  {
141  $uri = 'index.php' . ‪HttpUtility::buildQueryString($parameters, '?');
142  if ($referenceType === self::ABSOLUTE_PATH) {
144  } else {
145  if (isset(‪$GLOBALS['TYPO3_REQUEST'])
146  && ‪$GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface
147  && ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams') instanceof NormalizedParams) {
148  $uri = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getSiteUrl() . TYPO3_mainDir . $uri;
149  } else {
150  $uri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . $uri;
151  }
152  }
153  return GeneralUtility::makeInstance(Uri::class, $uri);
154  }
155 }
‪TYPO3\CMS\Backend\Routing\UriBuilder\__construct
‪__construct(Router $router=null)
Definition: UriBuilder.php:61
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\get
‪static TYPO3 CMS Core FormProtection AbstractFormProtection get($classNameOrType='default',... $constructorArguments)
Definition: FormProtectionFactory.php:74
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection\generateToken
‪string generateToken($formName, $action='', $formInstanceName='')
Definition: AbstractFormProtection.php:83
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪Uri buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:97
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:22
‪TYPO3\CMS\Backend\Routing\UriBuilder\$router
‪Router $router
Definition: UriBuilder.php:51
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_PATH
‪const ABSOLUTE_PATH
Definition: UriBuilder.php:47
‪TYPO3\CMS\Backend\Routing\UriBuilder\$generated
‪array $generated
Definition: UriBuilder.php:55
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUri
‪Uri buildUri($parameters, $referenceType)
Definition: UriBuilder.php:137
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:163
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:250
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoutePath
‪Uri buildUriFromRoutePath($pathInfo, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:78
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:47
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Backend\Routing\Router\match
‪Route match($pathInfo)
Definition: Router.php:69
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:42
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:34
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35