XF 2.1 Custom threads url

StreetGT

Member
Hi folks, I have migrated an old forum that used ForumCo software, we are now running smoothly on XenForo but it's time to fix now the broken anchor links.

So, when I imported all the data, I created an Addon that adds a custom field (old_thread_id) in xf_thread.

Now I want to create a custom route (eg https://url.xpto/oldthread/ID) which is going to redirect to the new XenForo Thread URL based on the old_thread_id.

With this trick I should be able to run a mysql query to replace all the old forum links.

Any tips? I have read about extending Thread and rewritting canonical function.

Best regards
 
Assuming you're in a controller, you can fetch a thread by the old_thread_id like this:
PHP:
$thread = $this->em()->findOne('XF:Thread', ['old_thread_id' => $oldId]);

It's probably worth adding an index to the column.

You can redirect to the canonical URL by returning a redirect response:
PHP:
return $this->redirect($this->buildLink('threads', $thread));

These are just to get you started and not a robust solution of course. You'll want to join the necessary relations for permission/sanity checks and such.
 
Hi Jeremy, I have done it.

I have created a new route, used respository to find the thread by old_thread_id in a controller and then if it was a valid old_thread_id I redirected the user to a new link.

Best regards
 
Back
Top Bottom