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

Creating a new XF controller

Discussion in 'XenForo Development Discussions' started by Retricide, Aug 6, 2013.

  1. Retricide

    Retricide Member

    There are a few tutorials on how to create an add-on that extends an already existing controller, but I cant find any information on how to create a new controller for an add-on.

    I'm try to create a simple add-on that basically mimics the xenforo help page layout, but uses custom templates for the pages.

    Can anyone point me in the right direction to create a new controller with its own route?

    Thanks.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    It's quite simple:
    1. Create an add-on in the Admin CP, let's say the ID is YourAddOn
    2. Create a directory in library called YourAddOn
    3. In that directory create a couple of extra directories (I like to mimic the layout of the XenForo directory structure as much as possible for my add-ons) -- the end result should be you have a library/YourAddOn/Route/Prefix directory.
    4. In that directory create a file called YourPages.php (or similar).
    PHP:
    <?php

    class YourAddOn_Route_Prefix_YourPages implements XenForo_Route_Interface
    {
        
    /**
        * Match a specific route for an already matched prefix.
        *
        * @see XenForo_Route_Interface::match()
        */
        
    public function match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
        {
            
    $action $router->resolveActionWithStringParam($routePath$request'page_name');
            return 
    $router->getRouteMatch('YourAddOn_ControllerPublic_Page'$action'yourpages');
        }

        
    /**
        * Method to build a link to the specified page/action with the provided
        * data and params.
        *
        * @see XenForo_Route_BuilderInterface
        */
        
    public function buildLink($originalPrefix$outputPrefix$action$extension$data, array &$extraParams)
        {
            return 
    XenForo_Link::buildBasicLinkWithStringParam($outputPrefix$action$extension$data'page_name');
        }
    }
    That's pretty much set up how you'd want it for your add-on, I think. Some of the stuff would need tweaking depending on what field names you use in the database etc.

    1. Then go to Admin CP > Development > Route Prefixes
    2. That should look like:
    upload_2013-8-6_12-33-54.png
    Did you notice this line in your Route Prefix?

    PHP:
    return $router->getRouteMatch('YourAddOn_ControllerPublic_Page'$action'yourpages');
    So you need to create your controller:

    1. Create the ControllerPublic directory in your add-on directory
    2. Create a file called Page.php:
    PHP:
    <?php

    class YourAddOn_ControllerPublic_Page extends XenForo_ControllerPublic_Abstract
    {   
        public function 
    actionIndex()
        {
            
    // Your controller code here
        
    }
    }
    If you now navigate to:

    http://yoursite.com/index.php?your-pages

    You will most likely get an error about there not being a controller response from YourAddOn_ControllerPublic_Page::actionIndex().

    That shows you, though, that your route prefix is working and routing to the relevant Controller. It's now your job to do the hard bit and add all of the wonderful code you need to write :)
     

Share This Page