XF 2.1 [SOLVED] Route format to determine order by value

asprin

Active member
I have route format set up in ACP with values such as follows:
1582995394256.webp

This is building URLs in the format of
Code:
index.php?fc/stats/page-2
which is great.

However, I would also like to add one more parameter to the URL which will determine the order column (created or updated) to be used in the ORDER BY clause. So the use case is something like this:
Code:
index.php?fc/stats/created/desc/page-2
or
Code:
index.php?fc/stats/page-2/&order_by=created&mode=descending

which should tell the controller that ordering should be done on the created column with latest records first.

As such, what should the route format look like (if at all it's achievable) to accommodate the above?
 
The latter (query strings) should just work without you needing to do anything special. If you would like it as a route parameter, you could do something like stats/:str<order_by>/:page which would give you $params->order_by.
 
The latter (query strings) should just work without you needing to do anything special. If you would like it as a route parameter, you could do something like stats/:str<order_by>/:page which would give you $params->order_by.
So I modified the route format to
1583041039779.png

And then on using the URL
Code:
index.php?fc/stats/created
the error is as follows:
Code:
The requested page could not be found. (Code: invalid_action, controller: Fantasy:FC, action: Statscreated)

It's trying to look for an action Statscreated (when it should ideally be looking for Stats)

Also tried with the following URL but still no luck
Code:
index.php?fc/stats/order_by/created
Even in this case, it's trying to find an action named Statscreated

UPDATE:
If the URL contains the page argument, then the error goes away. For example, the following works:
Code:
index.php?fc/stats/created/page-1
And then I get the column name by doing $params->order_by. Is there way to let XF know that the page number is optional and may not always be present?
 
Last edited:
Okay. Solved it. I had to add a trailing / to the URL for it to work. So it would look like the following:

Code:
index.php?fc/stats/created/
 
Back
Top Bottom