• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

Notes on XenForo's Library Structure

This entry is part of a series of entries "Reading XenForo Code, A Place to Start"
cobblestones.jpg


In the first two parts to this beginner's series I started with a way to read and interact with XenForo code then did a quick breakdown of the index file. Part 3 looks at the directory structure.

In my view, reading XenForo code is an important first step to building a simple or complex XenForo addon. It's ridiculous to think one can open up a text editor, type out code, and slap together an addon without first looking at how XenForo built their forum software. It's equally important as to why reading the XenForo Resource Manager addon is an important step.

After debating how to best present the XenForo directory structure in this article, I decided to spend the last two days making notes on the structure of XenForo. Based on the notes, I decided the best presentation was to put ideas into the context of building a simple flow in Laravel.

MVC Pattern

XenForo uses the MVC pattern. These three letters stand for Model-View-Controller. This is a means to separate code so that everything is not jammed into one file. Generally, models are the data layer between the controllers (logic and responses) and views (presentation to the browser). Not all developers use the same approaches for each of the three areas but it helps to understand that XenForo uses a "Skinny Controller, Fat Model" approach. Additionally, instead of views containing HTML, XenForo utilizes templates available online via the AdminCP. This allows easy styling of the forums and addons.

Class Name == Directory Structure
Besides understanding MVC, XenForo developers have added a nice touch. The class names match the directory structure. If the class name is XenForo_Model_Thread then the PHP file Thread.php can be found /library/XenForo/Model directory.

Laravel Framework

There are numerous PHP frameworks available, with each having strengths and weaknesses. I've chosen Laravel to help compare XenForo's approach to Laravel's approach.

A simple flow in Laravel to display something in the web browser is a route >> controller >> model >> view.

In the web.php, a developer would write something like:
PHP:
Route::get('/', 'PageController@home' );

This line (or facade for those of you with a programming background) loads a home method located in the PageController file when someone browses to the root of the domain. The method home can be as simple as returning a view, or lines of html. In Laravel, this would look like:

PHP:
public function home() {
     return view ('welcome');
}

The welcome view file contains the html.

PHP:
<html>
     <head>
          <title>
               Welcome to Learning XenForo.
          </title>
     </head>
     <body>
          <p>This is HTML in a view file.</p>
          <p>It has two paragraphs.</p>
     </body>
</html>

To summarize, the browser lands on /, the router points to the method home located in the PageController. The home method returns the welcome view, which contains all the HTML.

XenForo Approach
XenForo also uses the MVC pattern. A bonus with XenForo is that the directory structure leads to the class name.

XenForo and all addons are located in the library directory. XenForo has four controller directories.

ControllerAdmin
ControllerHelper
ControllerPublic
ControllerResponse

The base controller class has the name XenForo_Controller and is located in the XenForo library directory.

In your favorite text editor, open the files and you will find the Controller.php file located in /library/XenForo.

library/
XenForo/
Controller.php

Go ahead and open Controller.php. Locate the responseView method starting at line 1041. The comment at the top of the method describes that calling this method will load a view, and in the case of XenForo this means a template (located in the AdminCP) will be loaded.

PHP:
   /**
    * Controller response for when you want to output using a view class.
    *
    * @param string Name of the view class to be rendered
    * @param string Name of the template that should be displayed (may be ignored by view)
    * @param array  Key-value pairs of parameters to pass to the view
    * @param array  Key-value pairs of parameters to pass to the container view
    *
    * @return XenForo_ControllerResponse_View
    */
    public function responseView($viewName = '', $templateName = '', array $params = array(), array $containerParams = array())
    {
        $controllerResponse = new XenForo_ControllerResponse_View();
        $controllerResponse->viewName = $viewName;
        $controllerResponse->templateName = $templateName;
        $controllerResponse->params = $params;
        $controllerResponse->containerParams = $containerParams;
        return $controllerResponse;
    }

A simple example using responseView would be:

PHP:
return $this->responseView('XenForo_ViewPublic_Account_Signature', 'account_signature', $viewParams)

The view Signature.php is located in XenForo/ViewPublic/Account and the template loaded is account_signature.

Just as there are four controllers, there are three main views directories.

ViewAdmin
ViewPublic
ViewRenderer

In terms of Models, there is only the one directory named Models. In a future post I'll dive into the requirement for a Listener and models.

Summary

Model-View-Controller is a pattern to help developers separate the logic of the program (controllers) from the presentation layer (views) and the data layer (models). XenForo uses the MVC pattern, with a bonus of naming their classes the same as the directory structure. The base controller for XenForo is located in the library/XenForo directory. This Controller.php file has a responseView that will load the view and the template.
Previous entry in series The Index File

Blog entry information

Author
LPH
Views
2,062
Last update

More entries in General

More entries from LPH

Top