core-bundle/src/Resources/contao/classes/Frontend.php
public static function getPageIdFromUrl()
	{
		@trigger_error('Using Frontend::getPageIdFromUrl() has been deprecated and will no longer work in Contao 5.0. Use the Symfony routing instead.', E_USER_DEPRECATED);

		$strRequest = Environment::get('relativeRequest');

		if ($strRequest == '')
		{
			return null;
		}

		// Get the request without the query string
		list($strRequest) = explode('?', $strRequest, 2);

		// URL decode here (see #6232)
		$strRequest = rawurldecode($strRequest);

		// The request string must not contain "auto_item" (see #4012)
		if (strpos($strRequest, '/auto_item/') !== false)
		{
			return false;
		}

		// Extract the language
		if (Config::get('addLanguageToUrl'))
		{
			$arrMatches = array();

			// Use the matches instead of substr() (thanks to Mario Müller)
			if (preg_match('@^([a-z]{2}(-[A-Z]{2})?)/(.*)$@', $strRequest, $arrMatches))
			{
				Input::setGet('language', $arrMatches[1]);

				// Trigger the root page if only the language was given
				if ($arrMatches[3] == '')
				{
					return null;
				}

				$strRequest = $arrMatches[3];
			}
			else
			{
				return false; // Language not provided
			}
		}

		// Remove the URL suffix if not just a language root (e.g. en/) is requested
		if ($strRequest != '' && (!Config::get('addLanguageToUrl') || !preg_match('@^[a-z]{2}(-[A-Z]{2})?/$@', $strRequest)))
		{
			$intSuffixLength = \strlen(Config::get('urlSuffix'));

			// Return false if the URL suffix does not match (see #2864)
			if ($intSuffixLength > 0)
			{
				if (substr($strRequest, -$intSuffixLength) != Config::get('urlSuffix'))
				{
					return false;
				}

				$strRequest = substr($strRequest, 0, -$intSuffixLength);
			}
		}

		$arrFragments = null;

		// Use folder-style URLs
		if (Config::get('folderUrl') && strpos($strRequest, '/') !== false)
		{
			$strAlias = $strRequest;
			$arrOptions = array($strAlias);

			// Compile all possible aliases by applying dirname() to the request (e.g. news/archive/item, news/archive, news)
			while ($strAlias != '/' && strpos($strAlias, '/') !== false)
			{
				$strAlias = \dirname($strAlias);
				$arrOptions[] = $strAlias;
			}

			/** @var PageModel $objPageModel */
			$objPageModel = System::getContainer()->get('contao.framework')->getAdapter(PageModel::class);

			// Check if there are pages with a matching alias
			$objPages = $objPageModel->findByAliases($arrOptions);

			if ($objPages !== null)
			{
				$arrPages = array();

				// Order by domain and language
				while ($objPages->next())
				{
					/** @var PageModel $objModel */
					$objModel = $objPages->current();
					$objPage  = $objModel->loadDetails();

					$domain = $objPage->domain ?: '*';
					$arrPages[$domain][$objPage->rootLanguage][] = $objPage;

					// Also store the fallback language
					if ($objPage->rootIsFallback)
					{
						$arrPages[$domain]['*'][] = $objPage;
					}
				}

				$strHost = Environment::get('host');

				// Look for a root page whose domain name matches the host name
				if (isset($arrPages[$strHost]))
				{
					$arrLangs = $arrPages[$strHost];
				}
				else
				{
					$arrLangs = $arrPages['*'] ?? array(); // empty domain
				}

				$arrAliases = array();

				// Use the first result (see #4872)
				if (!Config::get('addLanguageToUrl'))
				{
					$arrAliases = current($arrLangs);
				}
				// Try to find a page matching the language parameter
				elseif (($lang = Input::get('language')) && isset($arrLangs[$lang]))
				{
					$arrAliases = $arrLangs[$lang];
				}

				// Return if there are no matches
				if (empty($arrAliases))
				{
					return false;
				}

				$objPage = $arrAliases[0];

				// The request consists of the alias only
				if ($strRequest == $objPage->alias)
				{
					$arrFragments = array($strRequest);
				}
				// Remove the alias from the request string, explode it and then re-insert the alias at the beginning
				else
				{
					$arrFragments = explode('/', substr($strRequest, \strlen($objPage->alias) + 1));
					array_unshift($arrFragments, $objPage->alias);
				}
			}
		}

		// If folderUrl is deactivated or did not find a matching page
		if ($arrFragments === null)
		{
			if ($strRequest == '/')
			{
				return false;
			}
			else
			{
				$arrFragments = explode('/', $strRequest);
			}
		}

		// Add the second fragment as auto_item if the number of fragments is even
		if (\count($arrFragments) % 2 == 0)
		{
			if (!Config::get('useAutoItem'))
			{
				return false; // see #264
			}

			array_insert($arrFragments, 1, array('auto_item'));
		}

		// HOOK: add custom logic
		if (isset($GLOBALS['TL_HOOKS']['getPageIdFromUrl']) && \is_array($GLOBALS['TL_HOOKS']['getPageIdFromUrl']))
		{
			foreach ($GLOBALS['TL_HOOKS']['getPageIdFromUrl'] as $callback)
			{
				$arrFragments = static::importStatic($callback[0])->{$callback[1]}($arrFragments);
			}
		}

		// Return if the alias is empty (see #4702 and #4972)
		if ($arrFragments[0] == '' && \count($arrFragments) > 1)
		{
			return false;
		}

		// Add the fragments to the $_GET array
		for ($i=1, $c=\count($arrFragments); $i<$c; $i+=2)
		{
			// Return false if the key is empty (see #4702 and #263)
			if ($arrFragments[$i] == '')
			{
				return false;
			}

			// Return false if there is a duplicate parameter (duplicate content) (see #4277)
			if (isset($_GET[$arrFragments[$i]]))
			{
				return false;
			}

			// Return false if the request contains an auto_item keyword (duplicate content) (see #4012)
			if (Config::get('useAutoItem') && \in_array($arrFragments[$i], $GLOBALS['TL_AUTO_ITEM']))
			{
				return false;
			}

			Input::setGet($arrFragments[$i], $arrFragments[$i+1], true);
		}

		return $arrFragments[0] ?: null;
	}
public static function getPageIdFromUrl()
	{
		@trigger_error('Using Frontend::getPageIdFromUrl() has been deprecated and will no longer work in Contao 5.0. Use the Symfony routing instead.', E_USER_DEPRECATED);

		$strRequest = Environment::get('relativeRequest');

		if ($strRequest == '')
		{
			return null;
		}

		// Get the request without the query string
		list($strRequest) = explode('?', $strRequest, 2);

		// URL decode here (see #6232)
		$strRequest = rawurldecode($strRequest);

		// The request string must not contain "auto_item" (see #4012)
		if (strpos($strRequest, '/auto_item/') !== false)
		{
			return false;
		}

		// Extract the language
		if (Config::get('addLanguageToUrl'))
		{
			$arrMatches = array();

			// Use the matches instead of substr() (thanks to Mario Müller)
			if (preg_match('@^([a-z]{2}(-[A-Z]{2})?)/(.*)$@', $strRequest, $arrMatches))
			{
				Input::setGet('language', $arrMatches[1]);

				// Trigger the root page if only the language was given
				if ($arrMatches[3] == '')
				{
					return null;
				}

				$strRequest = $arrMatches[3];
			}
			else
			{
				return false; // Language not provided
			}
		}

		// Remove the URL suffix if not just a language root (e.g. en/) is requested
		if ($strRequest != '' && (!Config::get('addLanguageToUrl') || !preg_match('@^[a-z]{2}(-[A-Z]{2})?/$@', $strRequest)))
		{
			$intSuffixLength = \strlen(Config::get('urlSuffix'));

			// Return false if the URL suffix does not match (see #2864)
			if ($intSuffixLength > 0)
			{
				if (substr($strRequest, -$intSuffixLength) != Config::get('urlSuffix'))
				{
					return false;
				}

				$strRequest = substr($strRequest, 0, -$intSuffixLength);
			}
		}

		$arrFragments = null;

		// Use folder-style URLs
		if (Config::get('folderUrl') && strpos($strRequest, '/') !== false)
		{
			$strAlias = $strRequest;
			$arrOptions = array($strAlias);

			// Compile all possible aliases by applying dirname() to the request (e.g. news/archive/item, news/archive, news)
			while ($strAlias != '/' && strpos($strAlias, '/') !== false)
			{
				$strAlias = \dirname($strAlias);
				$arrOptions[] = $strAlias;
			}

			/** @var PageModel $objPageModel */
			$objPageModel = System::getContainer()->get('contao.framework')->getAdapter(PageModel::class);

			// Check if there are pages with a matching alias
			$objPages = $objPageModel->findByAliases($arrOptions);

			if ($objPages !== null)
			{
				$arrPages = array();

				// Order by domain and language
				while ($objPages->next())
				{
					/** @var PageModel $objModel */
					$objModel = $objPages->current();
					$objPage  = $objModel->loadDetails();

					$domain = $objPage->domain ?: '*';
					$arrPages[$domain][$objPage->rootLanguage][] = $objPage;

					// Also store the fallback language
					if ($objPage->rootIsFallback)
					{
						$arrPages[$domain]['*'][] = $objPage;
					}
				}

				$strHost = Environment::get('host');

				// Look for a root page whose domain name matches the host name
				if (isset($arrPages[$strHost]))
				{
					$arrLangs = $arrPages[$strHost];
				}
				else
				{
					$arrLangs = $arrPages['*'] ?? array(); // empty domain
				}

				$arrAliases = array();

				// Use the first result (see #4872)
				if (!Config::get('addLanguageToUrl'))
				{
					$arrAliases = current($arrLangs);
				}
				// Try to find a page matching the language parameter
				elseif (($lang = Input::get('language')) && isset($arrLangs[$lang]))
				{
					$arrAliases = $arrLangs[$lang];
				}

				// Return if there are no matches
				if (empty($arrAliases))
				{
					return false;
				}

				$objPage = $arrAliases[0];

				// The request consists of the alias only
				if ($strRequest == $objPage->alias)
				{
					$arrFragments = array($strRequest);
				}
				// Remove the alias from the request string, explode it and then re-insert the alias at the beginning
				else
				{
					$arrFragments = explode('/', substr($strRequest, \strlen($objPage->alias) + 1));
					array_unshift($arrFragments, $objPage->alias);
				}
			}
		}

		// If folderUrl is deactivated or did not find a matching page
		if ($arrFragments === null)
		{
			if ($strRequest == '/')
			{
				return false;
			}
			else
			{
				$arrFragments = explode('/', $strRequest);
			}
		}

		// Add the second fragment as auto_item if the number of fragments is even
		if (\count($arrFragments) % 2 == 0)
		{
			if (!Config::get('useAutoItem'))
			{
				return false; // see #264
			}

			array_insert($arrFragments, 1, array('auto_item'));
		}

		// HOOK: add custom logic
		if (isset($GLOBALS['TL_HOOKS']['getPageIdFromUrl']) && \is_array($GLOBALS['TL_HOOKS']['getPageIdFromUrl']))
		{
			foreach ($GLOBALS['TL_HOOKS']['getPageIdFromUrl'] as $callback)
			{
				$arrFragments = static::importStatic($callback[0])->{$callback[1]}($arrFragments);
			}
		}

		// Return if the alias is empty (see #4702 and #4972)
		if ($arrFragments[0] == '' && \count($arrFragments) > 1)
		{
			return false;
		}

		// Add the fragments to the $_GET array
		for ($i=1, $c=\count($arrFragments); $i<$c; $i+=2)
		{
			// Return false if the key is empty (see #4702 and #263)
			if ($arrFragments[$i] == '')
			{
				return false;
			}

			// Return false if there is a duplicate parameter (duplicate content) (see #4277)
			if (isset($_GET[$arrFragments[$i]]))
			{
				return false;
			}

			// Return false if the request contains an auto_item keyword (duplicate content) (see #4012)
			if (Config::get('useAutoItem') && \in_array($arrFragments[$i], $GLOBALS['TL_AUTO_ITEM']))
			{
				return false;
			}

			Input::setGet($arrFragments[$i], $arrFragments[$i+1], true);
		}

		return $arrFragments[0] ?: null;
	}
public static function getPageIdFromUrl ( ) { @ trigger_error ( 'Using Frontend::getPageIdFromUrl() has been deprecated and will no longer work in Contao 5.0. Use the Symfony routing instead.' , E_USER_DEPRECATED ) ; $ strRequest = Environment :: get ( 'relativeRequest' ) ; if ( $ strRequest == '' ) { return null ; } // Get the request without the query string list ( $ strRequest ) = explode ( '?' , $ strRequest , 2 ) ; // URL decode here (see #6232) $ strRequest = rawurldecode ( $ strRequest ) ; // The request string must not contain "auto_item" (see #4012) if ( strpos ( $ strRequest , '/auto_item/' ) !== false ) { return false ; } // Extract the language if ( Config :: get ( 'addLanguageToUrl' ) ) { $ arrMatches = array ( ) ; // Use the matches instead of substr() (thanks to Mario Müller) if ( preg_match ( '@^([a-z]{2}(-[A-Z]{2})?)/(.*)$@' , $ strRequest , $ arrMatches ) ) { Input :: setGet ( 'language' , $ arrMatches [ 1 ] ) ; // Trigger the root page if only the language was given if ( $ arrMatches [ 3 ] == '' ) { return null ; } $ strRequest = $ arrMatches [ 3 ] ; } else { return false ; // Language not provided } } // Remove the URL suffix if not just a language root (e.g. en/) is requested if ( $ strRequest != '' && ( ! Config :: get ( 'addLanguageToUrl' ) || ! preg_match ( '@^[a-z]{2}(-[A-Z]{2})?/$@' , $ strRequest ) ) ) { $ intSuffixLength = \ strlen ( Config :: get ( 'urlSuffix' ) ) ; // Return false if the URL suffix does not match (see #2864) if ( $ intSuffixLength > 0 ) { if ( substr ( $ strRequest , - $ intSuffixLength ) != Config :: get ( 'urlSuffix' ) ) { return false ; } $ strRequest = substr ( $ strRequest , 0 , - $ intSuffixLength ) ; } } $ arrFragments = null ; // Use folder-style URLs if ( Config :: get ( 'folderUrl' ) && strpos ( $ strRequest , '/' ) !== false ) { $ strAlias = $ strRequest ; $ arrOptions = array ( $ strAlias ) ; // Compile all possible aliases by applying dirname() to the request (e.g. news/archive/item, news/archive, news) while ( $ strAlias != '/' && strpos ( $ strAlias , '/' ) !== false ) { $ strAlias = \ dirname ( $ strAlias ) ; $ arrOptions [ ] = $ strAlias ; } /** @var PageModel $objPageModel */ $ objPageModel = System :: getContainer ( ) -> get ( 'contao.framework' ) -> getAdapter ( PageModel :: class ) ; // Check if there are pages with a matching alias $ objPages = $ objPageModel -> findByAliases ( $ arrOptions ) ; if ( $ objPages !== null ) { $ arrPages = array ( ) ; // Order by domain and language while ( $ objPages -> next ( ) ) { /** @var PageModel $objModel */ $ objModel = $ objPages -> current ( ) ; $ objPage = $ objModel -> loadDetails ( ) ; $ domain = $ objPage -> domain ? : '*' ; $ arrPages [ $ domain ] [ $ objPage -> rootLanguage ] [ ] = $ objPage ; // Also store the fallback language if ( $ objPage -> rootIsFallback ) { $ arrPages [ $ domain ] [ '*' ] [ ] = $ objPage ; } } $ strHost = Environment :: get ( 'host' ) ; // Look for a root page whose domain name matches the host name if ( isset ( $ arrPages [ $ strHost ] ) ) { $ arrLangs = $ arrPages [ $ strHost ] ; } else { $ arrLangs = $ arrPages [ '*' ] ?? array ( ) ; // empty domain } $ arrAliases = array ( ) ; // Use the first result (see #4872) if ( ! Config :: get ( 'addLanguageToUrl' ) ) { $ arrAliases = current ( $ arrLangs ) ; } // Try to find a page matching the language parameter elseif ( ( $ lang = Input :: get ( 'language' ) ) && isset ( $ arrLangs [ $ lang ] ) ) { $ arrAliases = $ arrLangs [ $ lang ] ; } // Return if there are no matches if ( empty ( $ arrAliases ) ) { return false ; } $ objPage = $ arrAliases [ 0 ] ; // The request consists of the alias only if ( $ strRequest == $ objPage -> alias ) { $ arrFragments = array ( $ strRequest ) ; } // Remove the alias from the request string, explode it and then re-insert the alias at the beginning else { $ arrFragments = explode ( '/' , substr ( $ strRequest , \ strlen ( $ objPage -> alias ) + 1 ) ) ; array_unshift ( $ arrFragments , $ objPage -> alias ) ; } } } // If folderUrl is deactivated or did not find a matching page if ( $ arrFragments === null ) { if ( $ strRequest == '/' ) { return false ; } else { $ arrFragments = explode ( '/' , $ strRequest ) ; } } // Add the second fragment as auto_item if the number of fragments is even if ( \ count ( $ arrFragments ) % 2 == 0 ) { if ( ! Config :: get ( 'useAutoItem' ) ) { return false ; // see #264 } array_insert ( $ arrFragments , 1 , array ( 'auto_item' ) ) ; } // HOOK: add custom logic if ( isset ( $ GLOBALS [ 'TL_HOOKS' ] [ 'getPageIdFromUrl' ] ) && \ is_array ( $ GLOBALS [ 'TL_HOOKS' ] [ 'getPageIdFromUrl' ] ) ) { foreach ( $ GLOBALS [ 'TL_HOOKS' ] [ 'getPageIdFromUrl' ] as $ callback ) { $ arrFragments = static :: importStatic ( $ callback [ 0 ] ) -> { $ callback [ 1 ] } ( $ arrFragments ) ; } } // Return if the alias is empty (see #4702 and #4972) if ( $ arrFragments [ 0 ] == '' && \ count ( $ arrFragments ) > 1 ) { return false ; } // Add the fragments to the $_GET array for ( $ i = 1 , $ c = \ count ( $ arrFragments ) ; $ i < $ c ; $ i += 2 ) { // Return false if the key is empty (see #4702 and #263) if ( $ arrFragments [ $ i ] == '' ) { return false ; } // Return false if there is a duplicate parameter (duplicate content) (see #4277) if ( isset ( $ _GET [ $ arrFragments [ $ i ] ] ) ) { return false ; } // Return false if the request contains an auto_item keyword (duplicate content) (see #4012) if ( Config :: get ( 'useAutoItem' ) && \ in_array ( $ arrFragments [ $ i ] , $ GLOBALS [ 'TL_AUTO_ITEM' ] ) ) { return false ; } Input :: setGet ( $ arrFragments [ $ i ] , $ arrFragments [ $ i + 1 ] , true ) ; } return $ arrFragments [ 0 ] ? : null ; }
Split the current request into fragments, strip the URL suffix, recreate the $_GET array and return the page ID

@return mixed

@deprecated Deprecated since Contao 4.7, to be removed in Contao 5.0.
Use the Symfony routing instead.
Split the current request into fragments strip the URL suffix recreate the $_GET array and return the page ID