• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Canonical links

Robust

Well-known member
#1
So, say I have

example.com/route/1

I want to make this:
example.com/route/title-data.1

I think XenForo calls this canonical requests. How exactly is this feature used? I looked at the thread and forum controllers and tried to see how it works there, but it didn't explain much.

As always, any help is appreciated :)
 

Jake B.

Well-known member
#2
It's handled in the route in the buildLink method. If you use XenForo_Link::buildBasicLinkWithIntegerParam you'd just set whatever the title field is (In most default XF things such as conversations and threads it is 'title') as the 6th parameter. Here's what XenForo uses for conversations, and is the most basic example:

PHP:
    public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
    {
        $action = XenForo_Link::getPageNumberAsAction($action, $extraParams);
        return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'conversation_id', 'title');
    }
 

Jeremy

Well-known member
#3
If you are talking about how XenForo canonicalizes links such as this:

https://xenforo.com/community/threads/109285/

into

https://xenforo.com/community/threads/canonical-links.109285/

(or utilizing the proper title) when loading the links, you want to look at and utilize the following functions in your controller action:
XenForo_Controller::canonicalizeRequestUrl($linkUrl) (takes the response from XenForo_Link::buildPublicLink()) and
XenForo_Controller::canonicalizePageNumber($page, $perPage, $total, $linkType, $linkData = null)

Example from the threads controller:
PHP:
        $this->canonicalizePageNumber($page, $postsPerPage, $thread['reply_count'] + 1, 'threads', $thread);
        $this->canonicalizeRequestUrl(
            XenForo_Link::buildPublicLink('threads', $thread, array('page' => $page))
        );