1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

[Guide] Hello World plugin.

Discussion in 'Development Tutorials [Archive]' started by RandZhe, Oct 7, 2010.

  1. RandZhe

    RandZhe Member

    I'm not going to explain every step, I'm just going to show the steps to create a simple plugin that will print a "Hello World" message.

    First of all, I'm not a programmer. I have little knoledge of PHP OOP and MVC is tottaly new to me.

    If I say something stupid please let me know xD...

    Step 1 - The first step, I believe, is to create a new add-o. So let's go to Development -> Create Add-on. Use the following configuration.
    ID: HelloWorld
    Title: Hello world
    Version String: 1.0.0
    Version ID: 1

    Leave the other options, though I don't know what are they for hehe.

    Step 2 - While we are in the admin cp, let's create a Router Prefix by going to Development -> Route Prefixes and clicking the "Create New Route Prefix" button.
    We're going to name it "hello-world" and select the "Public" option from Route Type. From the drop down menu select our add-on "Hello World"
    In the Route Class field, enter the following: "HelloWorld_Route_Prefix_Index"
    Now hit the button Save Prefix.

    What we just did, was to create the url prefix that we will be using to access our hello world page form index.php?hello-world.
    We are using a class name that we haven't created yet.

    Step 3 - Now we are going to create a new template to use with this addon. We are doing it now that we are still in the admin cp, we could leave it for later but... just do it :D
    Let's go to: Appearance -> Templates -> +Create New Template. We must give it a unique name, we're going to name it "helloWorld_view" I believe we can name it as we want, as log as the name is not being used by another template.

    Use the following HTML for you template content:
    <div style="text-align: center;">
    	<h2 style="font-size: 20px;">Hello World</h2>
    From the "Add-on:" drop down options, select our Add-on "Hello World".

    Click Save and Exit.

    Step 4 - Here is where the magic begins...
    To make this work, we need to create a folder inside the "library" directory under the name of our add-on. So let's create a folder called "HelloWorld" in there. Also we need to create two more directories inside our HelloWorld folder, these are: ControllerPublic and Route. Also under Route, we need to create a folder called Prefix.
    We should have the following in our add-on folder:

    -| library
    ------| HelloWorld
    -----------| ControllerPublic
    -----------| Route
    ----------------| Prefix

    We will be creating two files needed for our add-on to display our Hello World phrase.

    Step 5 - Let's create the first php file under ControllerPublic with the name of Index.php, note the upper case.

    Inside this file were going to add a class to extends the XenForo_ControllerPublic_Abstract. Which let us define an action to render our template. Not sure what is what I just said, as I mentioned earlier, I'm not yet familliar with MVC nor OOP programming hehe.. this is helping me a lot to, though.

    Ok, ad the following PHP code to our index.php file:

    class HelloWorld_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
            public function 
    I'm not explaining this, since I just figured out by myself how to do this, but have no clue about the code itself, still learning this stuff...

    Step 5 - Go ahead and create the second php file under the Route/Prefix folder with the name of Index.php again.
    This is where we define our class that will let us access our hello world page.
    Use this code for your index.php file:


    class HelloWorld_Route_Prefix_Index implements XenForo_Route_Interface
            public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)

    After this, you should see the message "Hello World" by goint to http://yoursite.com/xenforo/index.php?hello-world

    I thinks it's all, if there's any question I can answer, I'll do it.

    editing: The structure we should have in our folder should look like this:

    -| library
    ------| HelloWorld
    -----------| ControllerPublic
    --------------------| Index.php
    -----------| Route
    ----------------| Prefix
    --------------------| Index.php
    megabosx, DarkSign, Bobage24 and 8 others like this.
  2. borbole

    borbole Well-Known Member

    Nice guide. However there is one tiny thing that I would like to add if you don''t mind. Can you please replace this part:

    <div align="center">

    <div style="text-align: center">
    Because the align=" center" for the div tag has been deprecated.
    Umit, dmnkhhn and karrott like this.
  3. RandZhe

    RandZhe Member

    It rendered just fine on my browser and the code provably won't be used in an actual add-on, but there it is ;)
    borbole likes this.
  4. Andy Huang

    Andy Huang Well-Known Member

    Darn, you stole my hello world add-on and made it better!!!

    Kidding aside, this is a great article. Thank you very much for putting it together and sharing it with us :)
  5. Ben Davis

    Ben Davis Active Member

    Vodkaholic likes this.
  6. gldtn

    gldtn Well-Known Member

    What's the best way to create a style for the template ?
  7. sifuhall

    sifuhall Active Member

    Thanks for this!
  8. Weiyan

    Weiyan Well-Known Member

    Thanks this well explained tutor.

    Some additional question:

    How to pass variables to the template?
  9. ragtek

    ragtek Guest

    It's the 3rd parameter in responseView.
    You need to create an array with "key" => value
    For example:
     $params = array(
    'nodeBreadCrumbs' => $this->getModelFromCache('XenForo_Model_Node')->getNodeBreadCrumbs($categorytrue),
    'article' => $article,
    'otherArticles' => $otherArticles,
    'category' => $category,
    'canDelete' => $canDelete,
    'canLike' => $this->getArticleModel()->canLikeArticle($article$category),
    'author' => $this->getArticleModel()->getAuthor($article),
    'attachments' => $article['attachments'],


    And then you can use {$canLike} {$otherArticles} ,.... in your template
    Weiyan likes this.
  10. Weiyan

    Weiyan Well-Known Member

    Just follow this tutor, its very easy to create a page, with ragtek's enhancement pass params to view.

    If with this url:
    How to decode this "Kier"? Then I can write a hello Kier application.

    If I have a form in template, how to decode form params?
  11. SheepCow

    SheepCow Well-Known Member

    Have a look in the controller functions for bits like:

    $id $input->filterSingle("id");
    The input object is set up to allow you to get access to the inputs (e.g. post'd via form) and pull them in to your code (relatively) safely.

Share This Page