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

PHP Help - XenForo Addon

Discussion in 'XenForo Development Discussions' started by Mofis, Sep 16, 2013.

  1. Mofis

    Mofis New Member

    I have been working on an addon, and recently ran into a wall; as I didn't know how to code this little bit.

    Here is my Library/SSLB/ControllerPublic/SSN/Index file.

    PHP:
    <?php

    class SSLB_ControllerPublic_SSN_Index extends XenForo_ControllerPublic_Abstract
    {
        public function 
    actionIndex()
        {
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Index');
        }

        public function 
    actionCreative()
        {
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Creative');
        }
    }
    With that code, I am able to go to domain.com/creative & domain.com/creative/creative and have the SSLB_Creative template be shown there. What I want, is to be able to go to domain.com/creative/creative/list and have another template be shown for that. In order to accomplish that, how would I change my PHP code in the Library/SSLB/ControllerPublic/SSN/Index file?

    Hope you guys can help me. In advance, thanks to all those that help me or even attempt to help me! :)
     
    Last edited: Sep 16, 2013
  2. Chris D

    Chris D XenForo Developer Staff Member

    Is Creative a route that you have created?

    Because if that's the case then, actionIndex() will be called when you go to domain.com/creative.

    actionCreative() will be called when you go to domain.com/creative/creative.

    Therefore in that scenario you want a new action called actionList() and that will be called when you go to domain.com/creative/list
     
    Mofis likes this.
  3. Mofis

    Mofis New Member

    I'm sorry, I accidentally screwed up on the main post. I've edited my mistakes. Everything that I updated, was changed to red.
    Yes it is.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    PHP:
        public function actionCreativeList()
        {
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Creative_List');
        }
    Should do it.
     
    Mofis likes this.
  5. Mofis

    Mofis New Member

    Oh wow, thanks! It worked flawlessly. I hope it's not too much to ask, but what would I change the PHP code to so that I can load a template by going to domain.com/creative/creative/?type=list?
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    Take your existing Creative action:
    PHP:
        public function actionCreative()
        {
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Creative');
        }
    Change it to:

    PHP:
        public function actionCreative()
        {
            
    $typeId $this->_input->filterSingle('type'XenForo_Input::STRING);
            if (
    $typeId)
            {
                if (
    method_exists(__CLASS__'actionCreative' ucfirst($typeId))
                {
                    return 
    $this->responseReroute(__CLASS__'creative' ucfirst($typeId));
                }
            }
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Creative');
        }
    So your URL contains the ?type= input.

    We take that input, and see if we have a function in the controller named actionCreative{$typeId}.

    If it exists, it reroutes the response to that action name.

    If it doesn't exist, the normal view for creative/creative will be rendered.

    Then, all you need to do is make sure this exists in the controller (same as my previous post):

    PHP:
        public function actionCreativeList()
        {
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Creative_List');
        }
    The great thing is, if you ever want to add any other actions just add another action to your controller...

    PHP:
        public function actionCreativeMore()
        {
            return 
    $this->responseView('SSLB_ViewPublic_Index''SSLB_Creative_More');
        }
    That example would be available at:

    domain.com/creative/creative/?type=more

    It's worth noting that I've not tested any of this :)
     
    Mofis likes this.

Share This Page