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

Canonical links

Discussion in 'XenForo Development Discussions' started by Robust, Dec 6, 2015.

  1. Robust

    Robust Well-Known Member

    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 :)
     
  2. Jake B.

    Jake B. Well-Known Member

    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');
        }
     
    Robust likes this.
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    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))
            );
     
    Robust likes this.
  4. Robust

    Robust Well-Known Member

Share This Page