tagshookcontao

Get the current page url inside a hook for contao


I am using the parseArticle hook for news module for my contao instance. I need to get the current page url inside this parseArticle hook. I had checked with the insert tags "{{env::url}}". But it's not working. It is simply displaying this text. Is there any way to use insert tags inside our hook?

If no, what should be done to get the page url inside that hook for contao?


Solution

  • You can retrieve the current URL via \Environment::get('uri'). This also includes the query string.

    In general you can also "use insert tags" this way: \Controller::replaceInsertTags('{{…}}'), but this should never be necessary.

    If you want the url to the reader page (without the news item in the URL), you can use

    global $objPage
    $strRelativeUrl = $objPage->getFrontendUrl();
    $strAbsoluteUrl = $objPage->getAbsoluteUrl();
    

    Both of these function can take parameters to add to the URL. See PageModel.php#L1013 for example.