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

Get paramaters controller public

A

account8226

Guest
#1
Hello,

Let's say I have a add-on where one of the URLs is index.php?myaddon/library/CUSTOM_PARAM

How can I get the CUSTOM_PARAM variable (which would be an unknown string), and use it as a param of my model ?

Do I have to use routes for this ? How ?

Best regards.
 

Onimua

Well-known member
#2
You would use it the same way as to get any other parameter in your controller:
Code:
$param = $this->_input->filterSingle('paramName', XenForo_Input::STRING);
Just replace XenForo_Input::STRING with whatever input type you're wanting to match (like UINT). Then pass on $param to your model.

Unless you're saying the parameter you're sending is random?
 
A

account8226

Guest
#3
You would use it the same way as to get any other parameter in your controller:
Code:
$param = $this->_input->filterSingle('paramName', XenForo_Input::STRING);
Just replace XenForo_Input::STRING with whatever input type you're wanting to match (like UINT). Then pass on $param to your model.

Unless you're saying the parameter you're sending is random?
The problem is that I don't know the param name, it can be any string, it's like for the members controller, the param can be /1, /2, etc...
 

Chris D

XenForo developer
Staff member
#6
You need a Route Prefix controller.

This will allow you to pass a parameter to the URL. The threads route passes the thread ID, e.g 42845
 

Chris D

XenForo developer
Staff member
#9
Looks good. Maybe a bit advanced though in the sense you might not need sub-prefixes.

You might be better looking at existing routes e.g XenForo_Route_Prefix_ProfilePosts
 
A

account8226

Guest
#10
Looks good. Maybe a bit advanced though in the sense you might not need sub-prefixes.

You might be better looking at existing routes e.g XenForo_Route_Prefix_ProfilePosts
I am correctly getting the param in my router (using $action = $router->resolveActionWithStringParam($routePath, $request, 'username'); )

But how do I get the param in my Controller ? I'm returning the action in my getRouteMatch method (3rd parameters), but how do I use it in my controller ? The following isn't working :

PHP:
$this->_input->filterSingle('username', XenForo_Input::STRING)
 

Chris D

XenForo developer
Staff member
#11
I'm not sure if that would work - certainly not well with some usernames, e.g. those with spaces etc.

You might want to use: $action = $router->resolveActionWithIntegerParam($routePath, $request, 'user_id');

Check out XenForo_Route_Prefix_Members

There's a buildLink function which returns:

XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'user_id', 'username');

This is what outputs the member URLs with a user ID and username
 
A

account8226

Guest
#12
I'm not sure if that would work - certainly not well with some usernames, e.g. those with spaces etc.

You might want to use: $action = $router->resolveActionWithIntegerParam($routePath, $request, 'user_id');

Check out XenForo_Route_Prefix_Members

There's a buildLink function which returns:

XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'user_id', 'username');

This is what outputs the member URLs with a user ID and username
That thread is exactly what am I trying to do : http://xenforo.com/community/threads/creating-sub-pages-within-a-mod.5253/

But no solutions :(
 
A

account8226

Guest
#13
When I try to access :

index.php?myaddon/videos/username/ (to list any video by "username" : username is not a XenForo username ;))

I have the following error :
The controller MyAddon_ControllerPublic_Index does not define an action called Username.
Here is my Route_Prefix_Index :
PHP:
<?php
class MyAddon_Route_Prefix_Index implements XenForo_Route_Interface
{
 
    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
    {
        $action = $router->resolveActionWithStringParam($routePath, $request, 'username');
        return $router->getRouteMatch('MyAddon_ControllerPublic_Index', $action, 'myaddon');
    }
 
    public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
    {
        return XenForo_Link::buildBasicLinkWithStringParam($outputPrefix, $action, $extension, $data, 'username');
    }
}
?>
Username can be anything, any custom string I add to the URL. I would like to run my Controller action : videos with the param username, to run a different query depending on the username.

Why is that not working :cry: