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

Is there a function to get a thread URL based on $thread_id input?

AndyB

Well-known member
#1
I'm creating an add-on in which I need to create a URL based on the $thread_id. Is there a function I can call that returns a link with the appropriate URL?

I would also need a similar xF function for posts. I input the $post_id and the function returns the URL.

Thank you.
 

AndyB

Well-known member
#3
Hi Jeremy,

Sorry I should have been more clear, I'm looking for a function to use in the PHP side of the add-on.
 

AndyB

Well-known member
#5
Looks like the following is used to redirect:

PHP:
      // regular redirect
       return $this->responseRedirect(
         XenForo_ControllerResponse_Redirect::SUCCESS,
         XenForo_Link::buildPublicLink('threads', $thread)
       );
How would I use XenForo_Link::buildPublicLink('threads', $thread) so that I get back a full URL in the form of:

http://www.mysite.com/forums/threads/1234/

The following does not work.

$thread = '1234';
$var = XenForo_Link::buildPublicLink('threads', $thread);
 

AndyB

Well-known member
#7
I've tried several different ways to express the array, but not having any luck.

For example the following does not work.

$thread = array('thread' => $thread_id);
 

Jeremy

Well-known member
#8
You cannot just arbitrarily name your data when dealing with pre-defined XenForo functions. When building a link, it uses an ID and expects data named appropriately. Look into XenForo_Route_Prefix_Threads for the actual building of the threads route.
 

KenSmith

Active member
#10
Andy, here's some more detail...

The magic sauce for link-building resides in the Route_Prefix class for any given route.
As Jeremy pointed out, for threads, that class is XenForo_Route_Prefix_Threads.
Look in library/XenForo/Route/Prefix/Threads.php and you'll find a function buildLink.

The most important line is the final one:
Code:
    return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'thread_id', 'title') . $postHash;
Notice the 'thread_id' and 'title' names. Whatever array you pass will need those two keys to create an appropriate link.

Most commonly, if you have an entire thread loaded into $thread already, you could get the link this way:
Code:
  XenForo_Link::buildPublicLink('threads', $thread)
If you don't already have an array loaded with those fields, you could do this:
Code:
  XenForo_Link::buildPublicLink('threads',array('thread_id' => $threadId, 'title' => $title));
Either way, you'll get back a link that looks like mysite.com/threads/thread-title.thread-id/

I mainly go into all this because you'll probably want to add a buildLink into your Route_Prefix_ShowDelete class as well.
The version in most of my route prefixes is a simple one-liner like this:
Code:
  public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
  {
     return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'tourn_id', 'event_name');
  }
In that example, I use a tournament Id and the event's name to build a pretty link into my 'tournaments' route.
The key is that if you want to add a single ID type parameter into your route's URLs, this tells it which field that represents,
and additionally, which field can be used to build the SEO-friendly version that includes more than just the numeric ID.
Make sense?
 

Chris D

XenForo developer
Staff member
#11
Last edited:

AndyB

Well-known member
#12
It's worth noting that you can just pass the thread_id parameter to a link and it will build a link without the title, e.g

PHP:
XenForo_Link::buildPublicLink('threads', array('thread_id' => 60386));
Produces:

http://xenforo.com/community/threads/60386

That will work fine.
Thank you, Marcus, Ken and Chris.

The quoted example only partially works, I get the following:

threads/60386

I wonder why the http://xenforo.com/community/ part on the link is not included?
 
#15
Sorry to bring this back.

I have a similar case here:

PHP:
// redirect after post
return $this->responseRedirect(
    XenForo_ControllerResponse_Redirect::SUCCESS,
    XenForo_Link::buildPublicLink('threads', array('thread_id' => $threadID)),
    new XenForo_Phrase('application_received')
);
With the following function in my route _prefix
PHP:
public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
{
    return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'thread_id');
}
Any idea what I'm doing wrong here? I don't get any redirect after a successful post, however the XenForo_Phrase does display correctly.