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

How do I create an add-on with a route that calls a template

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 26, 2013.

  1. AndyB

    AndyB Well-Known Member

    I would like to create an add-on called Show Deleted. This add-on will display links of all soft deleted posts and threads.

    The link to call this add-on will be:

    http://www.mysite.com/forums/showdeleted/

    This is what I have done so far.
     
  2. AndyB

    AndyB Well-Known Member

    Created the add-on

    pic001.jpg
     
  3. AndyB

    AndyB Well-Known Member

    Created the directories and files.

    library
    --Andy
    ----ShowDeleted
    ------Index.php
     
  4. AndyB

    AndyB Well-Known Member

    Create the Route Prefix

    pic002.jpg
     
  5. AndyB

    AndyB Well-Known Member

    Created Index.php

    PHP:
    <?php

    echo 'test';

    ?>
     
  6. AndyB

    AndyB Well-Known Member

    Created the andy_showdeleted template

    pic003.jpg
     
  7. AndyB

    AndyB Well-Known Member

    I would like to know how to edit the Index.php file so the andy_showdeleted template is displayed under the breadcrumb. Currently I get echoed "test" above the header and the following under the breadcrumb:

    Route showdeleted/ could not be found.
     
  8. KenSmith

    KenSmith Active Member

    I'm debating whether to respond because I'm still learning the nuances of this myself.
    But hopefully I can provide some direction for you.

    Returning a template from a controller looks like this:
    Code:
    class Andy_ControllerPublic_ShowDeleted extends XenForo_ControllerPublic_Abstract
    {
       public function actionIndex()
       {
         $viewParams = array( );  //  this would include any variables that you want to send to your template
         return $this->responseView('Andy_ViewPublic_ShowDeleted', 'your_template_name', $viewParams);
       }
    
    That code would be in Andy/ControllerPublic/ShowDeleted.php

    However, your Route Prefix needs another piece to connect it all together.

    Something like this in Andy/Route/Prefix/ShowDeleted.php

    Code:
    <?php
    
    class Andy_Route_Prefix_ShowDeleted implements XenForo_Route_Interface
    {
      /**
      * Match a specific route for an already matched prefix.
      *
      * @see XenForo_Route_Interface::match()
      */
      public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
      {
      return $router->getRouteMatch('Andy_ControllerPublic_ShowDeleted', $routePath);
      }
    }
    
    And the Route Prefix setting in the ACP should point to Andy_Route_Prefix_ShowDeleted instead of your current value of Andy_ShowDeleted_Index
     
    Sidane and AndyB like this.
  9. AndyB

    AndyB Well-Known Member

    Hi Ken,

    That works perfect. Thank you so much for helping me. I'll post the changes I made now.
     
    Valhalla and KenSmith like this.
  10. AndyB

    AndyB Well-Known Member

    The directories and files:

    library
    --Andy
    ----ShowDeleted
    ------ControllerPublic
    --------ShowDeleted.php
    ------Route
    --------Prefix
    ----------ShowDeleted.php
     
  11. AndyB

    AndyB Well-Known Member

    library/Andy/ShowDeleted/ControllerPublic/ShowDeleted.php

    PHP:
    <?php

    class Andy_ShowDeleted_ControllerPublic_ShowDeleted extends XenForo_ControllerPublic_Abstract
    {
       public function 
    actionIndex()
       {
         
    $viewParams = array( );  //  this would include any variables that you want to send to your template
         
    return $this->responseView('Andy_ShowDeleted_ViewPublic_ShowDeleted''andy_showdeleted'$viewParams);
       }
    }

    ?>
     
  12. AndyB

    AndyB Well-Known Member

    library/Andy/ShowDeleted/Route/Prefix/ShowDeleted.php

    PHP:
    <?php

    class Andy_ShowDeleted_Route_Prefix_ShowDeleted 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)
       {
         return 
    $router->getRouteMatch('Andy_ShowDeleted_ControllerPublic_ShowDeleted'$routePath);
       }
    }
    ?>
     
  13. AndyB

    AndyB Well-Known Member

    The Route Prefix

    pic001.jpg
     
  14. AndyB

    AndyB Well-Known Member

    Next I will create some queries and edit the template.
     
  15. LPH

    LPH Well-Known Member

    @AndyB
    Drats. Tripped over this thread and was hoping to see your work on templates. What templates did you end up creating?
     
  16. AndyB

    AndyB Well-Known Member

    LPH likes this.

Share This Page