[Guide] Explanation of a Hello World script.

Status
Not open for further replies.

Heretic121

OMG Member
Joined
Sep 24, 2010
Messages
446
This is an explanation of a Hello World script I found on XenForo's website, link here. This is post is an attempt to explain some of the different parts of XenForo when it comes to mod development. I am not saying that I know what I'm talking about, because I don't. I'm merely giving you what I've come to understand about XenForo. If anyone would like to correct me then please do so.

The user that created the article was, unfortunately, not too familiar with OOP and MVC. Thankfully I've done a bit of OOP programming so I found it easier to understand what he was doing than he did.

First Step:
This is pretty simple. You're setting up an add-on so that XenForo knows what it's looking for basically.

Second Step:
Now we really should look at what we're doing from here onwards because everything is pretty much linked into everything you'll do after this.
He talks about create a "Route Prefix". Now as far as I can tell, a Route Prefix is what goes after your URL. When he names it "hello-world" he is setting up XenForo to accept "hello-world" as a route to your add-on. He later states that to access your add-on you have to visit "http://yoursite.com/xenforo/index.php?hello-world" to view the add-on. Do you notice the connection between the part after the question in the link and the name you give your route prefix? I'm assuming that whatever you name your prefix is how you would load it when you create your add-on. Note: The Route Class is basically the location of the Route file which stores the class, which you'll create later.

Third Step:
Now he creates a template for his add-on. He names it "helloWorld_view" (Remember the name I'll come back to it in step 5). He adds some simple HTML which will ultimately display "Hello World". He then attaches it to the add-on he made in the first step.

Fourth Step:
He explains a bit about the way the add-ons directory on the server should be structured. Now what I've noticed is that it is essential to get this right because a lot of the code used in the add-on relies on the add-ons directory tree being correct. I'll explain more when I show the code. Also make a note of the folder, and file, names I'll refer to this in the next step.

Fifth Step:
Now here is where I started to work things out myself, and personally I find it quite self-explanatory.
He shows us the code we need to put in the Index.php (Note the capital i), which is below:
PHP:
<?php
    class HelloWorld_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
    {
        public function actionIndex()
        {
            return $this->responseView('HelloWorld_ViewPublic_Index', 'helloWorld_view');
        }
    }
?>
Right, I'll start by explaining that "HelloWorld_ControllerPublic_Index" is actually the location of... well itself really lol. So "<add-on id>_ControllerPublic_<filename>". Pretty simple really. Now for those who aren't familiar with OOP: "extends" means that it "attaches" this class to the end of the class named after it, in this case "XenForo_ControllerPublic_Abstract" and once again this is the file location of the class we want to add our class to. Moving on...

The function name, as far as I can tell, stands for "Do this action when Index is loaded" or words to that effect. What is interesting is the code inside the function.
Now then in OOP $this is the "variable" to indicate that we're looking for something inside the current class. So the function $this->responseView() is a function already inside the current class. The first variable given to the function is "HelloWorld_ViewPublic_Index" which isn't a listing of a directory, because we have no directory called "ViewPublic" apart from the one in the XenForo folder. So I guess the way to explain this bit is by saying it stands for "View the index for HelloWorld". The second variable is the name of the template he made in the third step. Which is exactly what it will load.

Sixth Step:
Now he shows you what to put in your second PHP file. The code he wants you to put in is:
PHP:
<?php

    class HelloWorld_Route_Prefix_Index implements XenForo_Route_Interface
    {
        public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
        {
            return $router->getRouteMatch('HelloWorld_ControllerPublic_Index', $routePath, 'hello-world');
        }
    }
?>
Right you know about the naming of classes so I won't explain that again. I'll just jump straight to the function inside it.
I'm not sure what getRouteMatch does but then I can't know everything :) I do, however, have some understanding of the variables given to it. The first variable is the location of the class you created earlier. The second variable I believe it the web address you're currently at. The third, and most important value, is 'hello-world'. Now this variable did turn up once, during the creation of the Route Prefix. So whatever you named your Route Prefix, you better make sure you use the same tag here otherwise none of it will work.

I think that's about everything I've learned so far, or at least as far as I can remember it is. I hope this has been useful for you and I'd like it if people would let me know if any of my assumptions are wrong. I'm willing to accept questions but as to whether I can answer them or not is a different question entirely.

Thanks for reading.
 

Mikey

:mikey:
Staff member
Joined
Jan 26, 2008
Messages
17,836
Nice! Thanks for posting, I only skimmed it so far, but I have no doubt that I'll comeback to it later and it will be hella useful :D
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
Excellent write up. Thanks for posting :D *reading again so I learn something*
 

Heretic121

OMG Member
Joined
Sep 24, 2010
Messages
446
Thanks guys :D Much appreciated.
Floris, you're most welcome. I needed somewhere to post it and XenForo won't allow me to because I haven't brought it, so Mikey suggested putting it here ;)
 

Weiyan

OMG Member
Joined
Nov 22, 2010
Messages
60
Thanks for this great tutor.

How to pass variable to template?
 

Cezz

OMG Member
Joined
Sep 24, 2010
Messages
581
Great tuturoial Ben, the only thing I would say is that with
Code:
HelloWorld_ViewPublic_Index

Though in your file it doesn't point to a file, it can... if you did create HelloWorld/ViewPublic/Index.php then you could control the View Class and get it to output custom details overwriting the standard view class of Xenforo..

Thanks for this great tutor.

How to pass variable to template?

You need to pass ViewParams, as an array... in the example

Code:
<?php
    class HelloWorld_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
    {
        public function actionIndex()
        {
            return $this->responseView('HelloWorld_ViewPublic_Index', 'helloWorld_view');
        }
    }
?>

you would change

Code:
            return $this->responseView('HelloWorld_ViewPublic_Index', 'helloWorld_view');

to

Code:
            return $this->responseView('HelloWorld_ViewPublic_Index', 'helloWorld_view',array('var1' => 'value1','var2' => 'value2'));

You can then access {$var1} and {xen:raw $var2} etc in the template then.
 

Stormraven

Trusted Member
Joined
Oct 24, 2010
Messages
1,836
Nice article Ben. I've only skimmed it so far, but I will read it later on my iPad :) It'll make a very interesting read
 

Weiyan

OMG Member
Joined
Nov 22, 2010
Messages
60
Now can pass param to view template. The next is to learn how to receive param from request query or form data?
 
Status
Not open for further replies.
Top