Get paramaters controller public

  • Thread starter Thread starter account8226
  • Start date Start date
A

account8226

Guest
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.
 
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?
 
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...
 
Ah, it's not actually an input it's a URL?

Yes, or like for the default XenForo core and threads, for example here it's threads/42845 ^^

I would like to do this with my own add-on.
 
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
 
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

Any guide or add-on I can use to understand a little bit more these routes ?
 
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
 
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)
 
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
 
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 :(
 
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:
 
Top Bottom