getPageIdFromUrl

The getPageIdFromUrl hook is triggered when the URL fragments are evaluated. It passes the array of URL fragments as argument and expects an array of URL fragments as return value.

The first fragment returned will usually be the ID or alias of a Contao page.

Using the getPageIdFromUrl hook has been deprecated and will no longer work in Contao 5.0.

Parameters

  1. array $fragments

    The URL fragments (current url exploded by slash). Be aware that previous hook callbacks could have modified this data.

Return values

Return the (modified) array of URL fragments.

Example

// src/EventListener/GetPageIdFromUrlListener.php
namespace App\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;

#[AsHook('getPageIdFromUrl')]
class GetPageIdFromUrlListener
{
    public function __invoke(array $fragments): array
    {
        // Analyze the fragments
        if () {
            \Contao\Input::setGet('myFirstCustomGetParameter', $fragments[1]);
            \Contao\Input::setGet('mySecondCustomGetParameter', $fragments[2]);

            return [$fragments[0]];
        }

        return $fragments;
    }
}

References