diff --git a/Classes/MOC/Redirects/Controller/RedirectController.php b/Classes/MOC/Redirects/Controller/RedirectController.php index dfc7e26..10e7824 100644 --- a/Classes/MOC/Redirects/Controller/RedirectController.php +++ b/Classes/MOC/Redirects/Controller/RedirectController.php @@ -2,25 +2,29 @@ namespace MOC\Redirects\Controller; use TYPO3\Flow\Annotations as Flow; +use TYPO3\Flow\Mvc\Controller\ActionController; use TYPO3\Neos\Service\LinkingService; use TYPO3\TYPO3CR\Domain\Model\Node; -class RedirectController extends \TYPO3\Flow\Mvc\Controller\ActionController { +class RedirectController extends ActionController +{ - /** - * @Flow\Inject - * @var LinkingService - */ - protected $linkingService; + /** + * @Flow\Inject + * @var LinkingService + */ + protected $linkingService; - /** - * @param Node $node - * @return void - * - * @Flow\IgnoreValidation("node") - */ - public function redirectAction(Node $node) { - $this->redirectToUri($this->linkingService->createNodeUri($this->controllerContext, $node, NULL, NULL, TRUE, array(), '', TRUE), 0, 301); - } + /** + * @param Node $node + * @return void + * + * @Flow\IgnoreValidation("node") + */ + public function redirectAction(Node $node) + { + $this->redirectToUri($this->linkingService->createNodeUri($this->controllerContext, $node, null, null, true, + array(), '', true), 0, 301); + } } diff --git a/Classes/MOC/Redirects/Routing/RedirectFrontendNodeRoutePartHandler.php b/Classes/MOC/Redirects/Routing/RedirectFrontendNodeRoutePartHandler.php index 71e98fe..120ab17 100644 --- a/Classes/MOC/Redirects/Routing/RedirectFrontendNodeRoutePartHandler.php +++ b/Classes/MOC/Redirects/Routing/RedirectFrontendNodeRoutePartHandler.php @@ -11,84 +11,93 @@ /** * A route part handler for finding nodes specifically in the website's frontend. */ -class RedirectFrontendNodeRoutePartHandler extends DynamicRoutePart { - - /** - * @Flow\Inject - * @var \Doctrine\Common\Persistence\ObjectManager - */ - protected $entityManager; - - /** - * @Flow\Inject - * @var Bootstrap - */ - protected $bootstrap; - - /** - * @param string $requestPath The request path to be matched - * @return string value to match - */ - protected function findValueToMatch($requestPath) { - return $requestPath; - } - - /** - * @param string $requestPath - * @return boolean TRUE if the $requestPath could be matched, otherwise FALSE - */ - protected function matchValue($requestPath) { - /** @var Uri $uri */ - $uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri(); - $relativeUrl = rtrim($uri->getPath(), '/'); - $relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : ''); - $absoluteUrl = $uri->getHost() . $relativeUrl; - $absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString; - - /** @var QueryBuilder $queryBuilder */ - $queryBuilder = $this->entityManager->createQueryBuilder(); - - $queryBuilder->select('n') - ->distinct() - ->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n') - ->where('n.workspace = :workspace') - ->setParameter('workspace', 'live') - ->andWhere('n.properties LIKE :relativeUrl') - ->setParameter('relativeUrl', '%"redirectUrl"%' . rtrim($uri->getPath(), '/') . '%'); - - $query = $queryBuilder->getQuery(); - $nodes = $query->getResult(); - - if (empty($nodes)) { - return FALSE; - } - - foreach ($nodes as $node) { - /** @var NodeData $node */ - // Prevent partial matches - $redirectUrl = preg_replace('#^https?://#', '', $node->getProperty('redirectUrl')); - if (in_array($redirectUrl, array($relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString), TRUE)) { - $matchingNode = $node; - break; - } - } - - if (!isset($matchingNode)) { - return FALSE; - } - - $this->setName('node'); - $this->value = $matchingNode->getPath(); - - return TRUE; - } - - /** - * @param string $value value to resolve - * @return boolean TRUE if value could be resolved successfully, otherwise FALSE. - */ - protected function resolveValue($value) { - return FALSE; - } +class RedirectFrontendNodeRoutePartHandler extends DynamicRoutePart +{ + + /** + * @Flow\Inject + * @var \Doctrine\Common\Persistence\ObjectManager + */ + protected $entityManager; + + /** + * @Flow\Inject + * @var Bootstrap + */ + protected $bootstrap; + + /** + * @param string $requestPath The request path to be matched + * @return string value to match + */ + protected function findValueToMatch($requestPath) + { + return $requestPath; + } + + /** + * @param string $requestPath + * @return boolean TRUE if the $requestPath could be matched, otherwise FALSE + */ + protected function matchValue($requestPath) + { + /** @var Uri $uri */ + $uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri(); + $relativeUrl = rtrim($uri->getPath(), '/'); + $relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : ''); + $absoluteUrl = $uri->getHost() . $relativeUrl; + $absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString; + + if (empty($relativeUrl)) { + return false; + } + + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = $this->entityManager->createQueryBuilder(); + + $queryBuilder->select('n') + ->distinct() + ->from('TYPO3\TYPO3CR\Domain\Model\NodeData', 'n') + ->where('n.workspace = :workspace') + ->setParameter('workspace', 'live') + ->andWhere('n.properties LIKE :relativeUrl') + ->setParameter('relativeUrl', '%"redirectUrl"%' . $relativeUrl . '%'); + + $query = $queryBuilder->getQuery(); + $nodes = $query->getResult(); + + if (empty($nodes)) { + return false; + } + + foreach ($nodes as $node) { + /** @var NodeData $node */ + // Prevent partial matches + $redirectUrl = preg_replace('#^https?://#', '', $node->getProperty('redirectUrl')); + if (in_array($redirectUrl, + array($relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString), true)) { + $matchingNode = $node; + break; + } + } + + if (!isset($matchingNode)) { + return false; + } + + $this->setName('node'); + $this->value = $matchingNode->getPath(); + + return true; + } + + /** + * @param string $value value to resolve + * @return boolean TRUE if value could be resolved successfully, otherwise FALSE. + */ + protected function resolveValue($value) + { + return false; + } }