‪TYPO3CMS  9.5
AbstractController.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 
20 use ‪TYPO3\CMS\Extbase\Mvc\Web\Request as WebRequest;
21 
26 {
30  protected ‪$signalSlotDispatcher;
31 
35  protected ‪$objectManager;
36 
40  protected ‪$uriBuilder;
41 
45  protected ‪$extensionName;
46 
52  protected ‪$settings;
53 
59  protected ‪$request;
60 
66  protected ‪$response;
67 
71  protected ‪$validatorResolver;
72 
76  protected ‪$arguments;
77 
82  {
83  $this->signalSlotDispatcher = ‪$signalSlotDispatcher;
84  }
85 
89  public function ‪injectValidatorResolver(\‪TYPO3\CMS\‪Extbase\Validation\ValidatorResolver ‪$validatorResolver)
90  {
91  $this->validatorResolver = ‪$validatorResolver;
92  }
93 
101  protected ‪$supportedRequestTypes = [\TYPO3\CMS\Extbase\Mvc\Request::class];
102 
106  protected ‪$controllerContext;
107 
111  public function ‪getControllerContext()
112  {
114  }
115 
119  protected ‪$configurationManager;
120 
124  public function ‪__construct()
125  {
126  $className = static::class;
127  $classNameParts = explode('\\', $className, 4);
128  // Skip vendor and product name for core classes
129  if (strpos($className, 'TYPO3\\CMS\\') === 0) {
130  $this->extensionName = $classNameParts[2];
131  } else {
132  $this->extensionName = $classNameParts[1];
133  }
134  }
135 
139  public function ‪injectConfigurationManager(ConfigurationManagerInterface ‪$configurationManager)
140  {
141  $this->configurationManager = ‪$configurationManager;
142  $this->settings = $this->configurationManager->‪getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
143  }
144 
150  public function ‪injectObjectManager(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
151  {
152  $this->objectManager = ‪$objectManager;
153  $this->arguments = $this->objectManager->‪get(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class);
154  }
155 
166  public function ‪addFlashMessage($messageBody, $messageTitle = '', $severity = \‪TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession = true)
167  {
168  if (!is_string($messageBody)) {
169  throw new \InvalidArgumentException('The message body must be of type string, "' . gettype($messageBody) . '" given.', 1243258395);
170  }
171  /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
172  $flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
173  \‪TYPO3\CMS\Core\Messaging\FlashMessage::class,
174  (string)$messageBody,
175  (string)$messageTitle,
176  $severity,
177  $storeInSession
178  );
179  $this->controllerContext->getFlashMessageQueue()->enqueue($flashMessage);
180  }
181 
193  {
194  foreach ($this->supportedRequestTypes as $supportedRequestType) {
195  if (‪$request instanceof $supportedRequestType) {
196  return true;
197  }
198  }
199  return false;
200  }
201 
209  public function ‪processRequest(\‪TYPO3\CMS\‪Extbase\Mvc\RequestInterface ‪$request, \‪TYPO3\CMS\‪Extbase\Mvc\ResponseInterface ‪$response)
210  {
211  if (!$this->‪canProcessRequest($request)) {
212  throw new UnsupportedRequestTypeException(static::class . ' does not support requests of type "' . get_class(‪$request) . '". Supported types are: ' . implode(' ', $this->supportedRequestTypes), 1187701132);
213  }
214  if (‪$response instanceof \‪TYPO3\CMS\‪Extbase\Mvc\Web\Response && ‪$request instanceof WebRequest) {
215  ‪$response->setRequest(‪$request);
216  }
217  $this->request = ‪$request;
218  $this->request->‪setDispatched(true);
219  $this->response = ‪$response;
220  $this->uriBuilder = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Web\Routing\UriBuilder::class);
221  $this->uriBuilder->setRequest(‪$request);
224  $this->controllerContext = $this->‪buildControllerContext();
225  }
226 
232  protected function ‪buildControllerContext()
233  {
235  ‪$controllerContext = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\ControllerContext::class);
236  ‪$controllerContext->‪setRequest($this->request);
237  ‪$controllerContext->‪setResponse($this->response);
238  if ($this->arguments !== null) {
239  ‪$controllerContext->‪setArguments($this->arguments);
240  }
241  ‪$controllerContext->‪setUriBuilder($this->uriBuilder);
242 
243  return ‪$controllerContext;
244  }
245 
259  public function ‪forward($actionName, $controllerName = null, ‪$extensionName = null, array ‪$arguments = null)
260  {
261  $this->request->setDispatched(false);
262  if ($this->request instanceof WebRequest) {
263  $this->request->setControllerActionName($actionName);
264  if ($controllerName !== null) {
265  $this->request->setControllerName($controllerName);
266  }
267  if (‪$extensionName !== null) {
268  $this->request->setControllerExtensionName(‪$extensionName);
269  }
270  }
271  if (‪$arguments !== null) {
272  $this->request->setArguments(‪$arguments);
273  }
274  throw new StopActionException('forward', 1476045801);
275  }
276 
296  protected function ‪redirect($actionName, $controllerName = null, ‪$extensionName = null, array ‪$arguments = null, $pageUid = null, $delay = 0, $statusCode = 303)
297  {
298  if (!$this->request instanceof WebRequest) {
299  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539734);
300  }
301  if ($controllerName === null) {
302  $controllerName = $this->request->getControllerName();
303  }
304  $this->uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(true);
305  if (\‪TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL')) {
306  $this->uriBuilder->setAbsoluteUriScheme('https');
307  }
308  $uri = $this->uriBuilder->uriFor($actionName, ‪$arguments, $controllerName, ‪$extensionName);
309  $this->‪redirectToUri($uri, $delay, $statusCode);
310  }
311 
323  protected function ‪redirectToUri($uri, $delay = 0, $statusCode = 303)
324  {
325  if (!$this->request instanceof WebRequest) {
326  throw new ‪UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539735);
327  }
328 
329  $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
330 
331  $uri = $this->‪addBaseUriIfNecessary($uri);
332  $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
333  $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . (int)$delay . ';url=' . $escapedUri . '"/></head></html>');
334  if ($this->response instanceof \‪TYPO3\CMS\‪Extbase\Mvc\Web\‪Response) {
335  $this->response->setStatus($statusCode);
336  $this->response->setHeader('Location', (string)$uri);
337  }
338  // Avoid caching the plugin when we issue a redirect response
339  // This means that even when an action is configured as cachable
340  // we avoid the plugin to be cached, but keep the page cache untouched
341  $contentObject = $this->configurationManager->getContentObject();
342  if ($contentObject->getUserObjectType() === \‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::OBJECTTYPE_USER) {
343  $contentObject->convertToUserIntObject();
344  }
345 
346  throw new ‪StopActionException('redirectToUri', 1476045828);
347  }
348 
355  protected function ‪addBaseUriIfNecessary($uri)
356  {
357  return \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl((string)$uri);
358  }
359 
371  public function ‪throwStatus($statusCode, $statusMessage = null, $content = null)
372  {
373  if (!$this->request instanceof WebRequest) {
374  throw new ‪UnsupportedRequestTypeException('throwStatus() only supports web requests.', 1220539739);
375  }
376  if ($this->response instanceof \‪TYPO3\CMS\‪Extbase\Mvc\Web\‪Response) {
377  $this->response->setStatus($statusCode, $statusMessage);
378  if ($content === null) {
379  $content = $this->response->getStatus();
380  }
381  }
382  $this->response->setContent($content);
383  throw new StopActionException('throwStatus', 1476045871);
384  }
385 
391  {
393  foreach ($this->arguments as $argument) {
394  ‪$validator = $this->validatorResolver->getBaseValidatorConjunction($argument->getDataType());
395  if (‪$validator !== null) {
396  $argument->setValidator(‪$validator);
397  }
398  }
399  }
400 
407  {
409  foreach ($this->arguments as $argument) {
410  $argumentName = $argument->getName();
411  if ($this->request->hasArgument($argumentName)) {
412  $argument->setValue($this->request->getArgument($argumentName));
413  } elseif ($argument->isRequired()) {
414  throw new \TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException('Required argument "' . $argumentName . '" is not set for ' . $this->request->getControllerObjectName() . '->' . $this->request->getControllerActionName() . '.', 1298012500);
415  }
416  }
417  }
418 }
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\initializeControllerArgumentsBaseValidators
‪initializeControllerArgumentsBaseValidators()
Definition: AbstractController.php:378
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface\get
‪object &T get($objectName,... $constructorArguments)
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:26
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\processRequest
‪processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
Definition: AbstractController.php:197
‪TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
Definition: UnsupportedRequestTypeException.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\injectConfigurationManager
‪injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
Definition: AbstractController.php:127
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\injectValidatorResolver
‪injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
Definition: AbstractController.php:80
‪TYPO3\CMS\Extbase\Mvc\Dispatcher
Definition: Dispatcher.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$uriBuilder
‪TYPO3 CMS Extbase Mvc Web Routing UriBuilder $uriBuilder
Definition: AbstractController.php:37
‪TYPO3\CMS\Extbase\Mvc\ResponseInterface
Definition: ResponseInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\throwStatus
‪throwStatus($statusCode, $statusMessage=null, $content=null)
Definition: AbstractController.php:359
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\mapRequestArgumentsToControllerArguments
‪mapRequestArgumentsToControllerArguments()
Definition: AbstractController.php:394
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext\setUriBuilder
‪setUriBuilder(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder)
Definition: ControllerContext.php:129
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
Definition: ControllerContext.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext\setRequest
‪setRequest(\TYPO3\CMS\Extbase\Mvc\Request $request)
Definition: ControllerContext.php:71
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\buildControllerContext
‪TYPO3 CMS Extbase Mvc Controller ControllerContext buildControllerContext()
Definition: AbstractController.php:220
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\getConfiguration
‪array getConfiguration($configurationType, $extensionName=null, $pluginName=null)
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\getControllerContext
‪ControllerContext getControllerContext()
Definition: AbstractController.php:100
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: AbstractController.php:2
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$extensionName
‪string $extensionName
Definition: AbstractController.php:41
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$response
‪TYPO3 CMS Extbase Mvc ResponseInterface $response
Definition: AbstractController.php:59
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\injectSignalSlotDispatcher
‪injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
Definition: AbstractController.php:72
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$configurationManager
‪ConfigurationManagerInterface $configurationManager
Definition: AbstractController.php:107
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$validatorResolver
‪TYPO3 CMS Extbase Validation ValidatorResolver $validatorResolver
Definition: AbstractController.php:63
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\setDispatched
‪setDispatched($flag)
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext\setResponse
‪setResponse(\TYPO3\CMS\Extbase\Mvc\Response $response)
Definition: ControllerContext.php:91
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController
Definition: AbstractController.php:26
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\__construct
‪__construct()
Definition: AbstractController.php:112
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: AbstractController.php:284
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$signalSlotDispatcher
‪TYPO3 CMS Extbase SignalSlot Dispatcher $signalSlotDispatcher
Definition: AbstractController.php:29
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$controllerContext
‪TYPO3 CMS Extbase Mvc Controller ControllerContext $controllerContext
Definition: AbstractController.php:95
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:21
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:24
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: AbstractController.php:33
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\redirectToUri
‪redirectToUri($uri, $delay=0, $statusCode=303)
Definition: AbstractController.php:311
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\Cli\Response
Definition: Response.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$arguments
‪TYPO3 CMS Extbase Mvc Controller Arguments $arguments
Definition: AbstractController.php:67
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: AbstractController.php:138
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
Definition: ControllerInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$supportedRequestTypes
‪array $supportedRequestTypes
Definition: AbstractController.php:91
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\addBaseUriIfNecessary
‪string addBaseUriIfNecessary($uri)
Definition: AbstractController.php:343
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext\setArguments
‪setArguments(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments $arguments)
Definition: ControllerContext.php:111
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$settings
‪array $settings
Definition: AbstractController.php:47
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\canProcessRequest
‪bool canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
Definition: AbstractController.php:180
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\$request
‪TYPO3 CMS Extbase Mvc RequestInterface $request
Definition: AbstractController.php:53
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)
Definition: AbstractController.php:154
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\forward
‪forward($actionName, $controllerName=null, $extensionName=null, array $arguments=null)
Definition: AbstractController.php:247