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

XF 2.0 Route doesn't work without trailing slash

#1
Hi,

I am not sure if this is a bug or not. When I go to /myroute/slughere/ it works fine (displays the page), but when I remove the trailing slash, it doesn't work (e.g. /myroute/slughere), it says "The requested page could not be found. (Code: invalid_action, controller: My\Addon:MyPage, action: SlugHere)".

In addition, when I go to /myroute/ it works fine, as well as /myroute (even without trailing slash) - however, without the trailing slash, the breadcrumb would show (it doesn't show the MyPage breadcrumb link when there is a trailing slash, just Home):


I noticed that with /forums/, /threads/, ..etc they automatically add the trailing slash when it's not there.

Any help would be appreciated.

Pub\Controller\MyPage.php
Code:
<?php

namespace My\Addon\Pub\Controller;

use XF\Mvc\ParameterBag;

class MyPage extends \XF\Pub\Controller\AbstractController
{

public function actionIndex(ParameterBag $params)
{
   if ($params->slug)
   {
      return $this->rerouteController(__CLASS__, 'Category', $params);
   }
   $test = 'hello';
   $viewParams = ['test' => $test];
   return $this->view('My\Addon:MyPage\View', 'my_addon_index', $viewParams);
}

public function actionCategory(ParameterBag $params)
{
   $test ='bye';
   $viewParams = ['test' => $test];
   return $this->view('My\Addon:MyPage\Category', 'my_addon_category', $viewParams);
}
Public Route


I am running RC3.
 

Chris D

XenForo developer
Staff member
#2
Yep, pretty much expected. The same will happen in some cases in XF1 too.

The XF link builder will always build the correct link, though, so as long as your users aren't likely to go around manually typing URLs or removing trailing slashes from otherwise valid URLs you should be ok.
 
#3
Yep, pretty much expected. The same will happen in some cases in XF1 too.
Thanks Chris, although I'm still wondering, is there a way to add the trailing slash automatically, just like when you when try to remove it from /threads/1/ it will add it back (same for /forums/, /forums/nodename/ ...etc).
 

katsulynx

Well-known member
#4
You can add a redirect to the parent controller to lead the user to the correct URL. I've done that in all my add-ons and built an add-on for my site that does that for all base xf routes. I don't really consider it obvious for users that the Link doesn't work anymore as soon as you remove the slash.
 
#5
I don't really consider it obvious for users that the Link doesn't work anymore as soon as you remove the slash.
I agree with this, although that there are sometimes 'quirks' in code, this shouldn't really be an acceptable one imo.

I will have to see if there is a way around this through htaccess for any addons that use a route, but I am still curious as to how default XF2 routes add the trailing slash automatically when it's removed.
 

Chris D

XenForo developer
Staff member
#6
It's specific to routes that have a string parameter. e.g.

admin.php?options/groups/basicBoard/ will work

admin.php?options/groups/basicBoard will not work

It was the same in XF1 too.

Can you explain how in real usage this would actually pose a problem?
 

katsulynx

Well-known member
#7
@Chris_D as mentioned here, someone seems to have stumbled across this issue while developing XF2 as well.

The WhatsNew controller contains two explicit redirects to work around this issue. Although they are admitably not a large faction, there are some people that type URL for various reasons, for example copying them from a poster or something. No matter the reason for typing the URL, I personally never would leave a trailing slash there - which would then lead me to a 404 site. When I come to such a site, I don't want to figure out what could've gone wrong when I left that last trailing slash away. The rest of the URL appears to be right, so why should it be important. I'll probably just consider the website broken and leave. I consider it a bit sad that people could potentially leave over something like that which can be prevented by adding necessary redirects in all controllers.

On the other hand the question is also what the benefit of not "fixing" this is. Build two different pages, one ending with a trailing slash and the other one without one seems pretty pointless to me. This is even more confusing than leaving one leading to a 404 page.