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

Something I'm missing with buildPublicLink?

Onimua

Well-known member
#1
I've created a form that submits to the database, and everything works fine as you'd expect. The problem occurs in the redirect portion of the code.

PHP:
		$newMessage = $writer->getMergedData();
		$return = XenForo_Link::buildPublicLink('whiteboard/view-message', $newMessage);

		return $this->responseRedirect(
			XenForo_ControllerResponse_Redirect::SUCCESS,
			$return,
			new XenForo_Phrase('your_message_has_been_posted')
		);
The redirect takes me to whiteboard/view-message as I'd expect, and the template loads fine but it doesn't have what I'm expecting to see, which is whiteboard/view-message/title-here.25. Is there something missing that I can't find? I tried searching but didn't find anything that really made it clear.

I am submitting the data to a separate table within the XF database if that makes a difference.
 

Shadab

Well-known member
#2
Have you implemented the "buildLink()" method for the route-prefix class associated with the whiteboard route-prefix?
 

Onimua

Well-known member
#4
Have you implemented the "buildLink()" method for the route-prefix class associated with the whiteboard route-prefix?
Nope, wasn't aware of my needing to do that. This is the first time I've used it so I'm unsure of what I need to do with it. I'm assuming it's stupidly simple?
 

Kier

XenForo Developer
Staff member
#5
Nope, wasn't aware of my needing to do that. This is the first time I've used it so I'm unsure of what I need to do with it. I'm assuming it's stupidly simple?
Yes, it's very simple. You just have to tell the system what to do with the data you provide when building a link. It's done in the Route_Prefix classes.

For example, posts:
PHP:
public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
{
	return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'post_id');
}
and pages:
PHP:
public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
{
	return XenForo_Link::buildBasicLinkWithStringParam($outputPrefix, $action, $extension, $data, 'node_name');
}
 

Onimua

Well-known member
#6
Yes, it's very simple. You just have to tell the system what to do with the data you provide when building a link. It's done in the Route_Prefix classes.

For example, posts:
PHP:
public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
{
	return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'post_id');
}
and pages:
PHP:
public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
{
	return XenForo_Link::buildBasicLinkWithStringParam($outputPrefix, $action, $extension, $data, 'node_name');
}
Thanks Kier. I ended up one step closer, but apparently I'm missing another step now.


I now have this: /whiteboard/this-is-a-test-message.27/view-message
 

Onimua

Well-known member
#8
Well I do want to keep view-message but switch it to whiteboard/view-message/title.id. I had thought buildPublicLink('whiteboard/view-message') would make it do so but it looks like I'm missing one final piece of the puzzle to get it working 100%. Is that bit simple to do?

Otherwise I think I might take a different approach with how I'm doing this as I'm probably making it more complex than I should.
 

Kier

XenForo Developer
Staff member
#9
The principle of /prefix/item.id/action is fairly ingrained in XenForo. Trying to make /prefix/action/item.id as you suggest is very much swimming against the tide.

It can be done - you just need to write a custom version of buildLink() and resolveActionWithIntegerParam(), but is it really worth it?

Consider the URL structure to be like a directory tree. You have a collection of items (prefix) then you pick a single item (item.id) and then you do something with it (action), so you have /forums/myforum.2/create-thread or /posts/1234/edit or /threads/my-thread.234 and so on.