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

Get paramaters controller public

Discussion in 'XenForo Development Discussions' started by account8226, Jan 3, 2013.

  1. account8226

    account8226 Guest


    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.
  2. Onimua

    Onimua Well-Known Member

    You would use it the same way as to get any other parameter in your controller:
    $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?
  3. account8226

    account8226 Guest

    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...
  4. James

    James Well-Known Member

    Ah, it's not actually an input it's a URL?
  5. account8226

    account8226 Guest

    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.
  6. Chris D

    Chris D XenForo Developer Staff Member

    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
  7. account8226

    account8226 Guest

    Any guide or add-on I can use to understand a little bit more these routes ?
  8. account8226

    account8226 Guest

  9. Chris D

    Chris D XenForo Developer Staff Member

    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
  10. account8226

    account8226 Guest

    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 :

  11. Chris D

    Chris D XenForo Developer Staff Member

    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
    Sean Kendle likes this.
  12. account8226

    account8226 Guest

    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 :(
  13. account8226

    account8226 Guest

    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 :
    Here is my Route_Prefix_Index :
    class MyAddon_Route_Prefix_Index implements XenForo_Route_Interface
        public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
    $action $router->resolveActionWithStringParam($routePath$request'username');
        public function 
    buildLink($originalPrefix$outputPrefix$action$extension$data, array &$extraParams)
    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:
  14. account8226

    account8226 Guest

Share This Page