Remove index.php? when XenForo_Link::useFriendlyUrls(false)

LPH

Well-known member
The getBoardUrl function works fine when Use Friendly URLs is ticked in the ACP. However, when not checked then the /index.php? gets in the way of the URL.

PHP:
public static function getBoardUrl() {
   return XenForo_Application::get('options')->boardUrl;
}
In some places, to get around this problem then I'm using the following code.

PHP:
if ( XenForo_Link::useFriendlyUrls(false) == false ) {
   $spritesheet_url = XenForo_Application::get('options')->boardUrl . '/styles/dark/ratings/spritesheet.png';
} else {
   $spritesheet_url = XenForo_Link::buildPublicLink( 'canonical:index' . 'styles/dark/ratings/spritesheet.png' );
}
Is there an easier way (better way) to write this code? Is there a way to change 'canonical:index' so that the index.php? isn't present?
 

Mike

XenForo developer
Staff member
I'm unclear of what exactly you're trying to do.

The board URL always reflects the base URL to the install, essentially the directory containing the installation. Based on the code you've shown (getting a URL into the styles directory), you should always be using the board URL.

Building a link using XenForo_Link is only for internal page links within XF, not for linking to other assets.
 
Reactions: LPH

LPH

Well-known member
As always, thank you for replying.

I'm unclear of what exactly you're trying to do.
The main challenge is building a link to assets such as avatars or images from a styles directory from outside XenForo. The if/else conditional in the OP shows checking for Use Friendly URLs. The reason is the /index.php? causes the correct path not to load because the link is built wrong. I can get around it by using the boardUrl option in the XenForo_Application.

I just wasn't sure if the conditional was necessary. Is there a way to write the "canonical:index" such that if Use Friendly URLs is not enabled? I realize buildPublicLink wasn't meant for data or styles but I was just curious if there was a better approach.

Is that clearer?
 

Brogan

XenForo moderator
Staff member
Perhaps I'm misunderstanding the issue but if the directory is outside XF, the index.php is irrelevant, is it not?

So the boardURL is all you need - there's no need to do any code to check if index.php is in use or not, which is never going to be required.
 

Mike

XenForo developer
Staff member
Is there a way to write the "canonical:index" such that if Use Friendly URLs is not enabled?
That is almost literally what the boardUrl option is. The "canonical" link building option essentially just prefixes the boardURL to the generated link.
 

LPH

Well-known member
I'm sorry for not explaining.

If I simply use this code

PHP:
$spritesheet_url = XenForo_Application::get('options')->boardUrl . '/styles/dark/ratings/spritesheet.png';
then both Use Friendly URLs ticked on or off works fine and the png file loads.

If I simply use this code:

PHP:
$spritesheet_url = XenForo_Link::buildPublicLink( 'canonical:' . 'styles/dark/ratings/spritesheet.png' );
then ONLY Use Friendly URLs ticked on works

Links with Use Friendly URls ticked On

Code:
http://lph/community/styles/dark/ratings/spritesheet.png
Links with Use Friendly URls ticked Off

Code:
http://lph/community/index.php?styles/dark/ratings/spritesheet.png
See how the index.php? causes an issue for loading the png file if I use the buildPublicLink ?

Important

Of course, I now realize (because Mike said so .. LOL) I can skip trying to use the buildPublicLink (as well as the silly conditional) and just use the boardUrl options but it seemed odd to me that the buildPublicLink canonical wasn't working with the Use Friendly URLs off. I simply didn't understand why canonical didn't work.

I appreciate the help.
 
Last edited:
Top