• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • 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.

#1
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:
HTML:
<div style="text-align: center;">
	<h2 style="font-size: 20px;">Hello World</h2>
</div>
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:
PHP:
<?php

	class HelloWorld_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
	{
		public function actionIndex()
		{
			return $this->responseView('HelloWorld_ViewPublic_Index', 'helloWorld_view');
		}
	}
?>
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:

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');
		}
	}

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
 

borbole

Well-known member
#2
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:

Code:
<div align="center">
with

Code:
<div style="text-align: center">
Because the align=" center" for the div tag has been deprecated.
 
R

ragtek

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

        );

        return $this->responseView('Ragtek_AS_View_Article', 'ragtek_article_view', $params);
And then you can use {$canLike} {$otherArticles} ,.... in your template
 

Weiyan

Well-known member
#10
Just follow this tutor, its very easy to create a page, with ragtek's enhancement pass params to view.

If with this url:
HTML:
http://mysite/hello-world/Kier
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?
 

SheepCow

Well-known member
#11
Have a look in the controller functions for bits like:

PHP:
$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.