Trying To Code Very Simple Addon - And I Need Your Help!

TheBigK

Well-known member
After spending several days reading tutorials; I've decided to take my own baby steps in coding addons, so that I understand how the entire system works. However, it looks like I'm stuck at the very first step itself!

I've decided to code an addon that will -
  1. Have its own location: localhost/xf/adder/
  2. This page will show two small input boxes to accept two numbers viz: [ No#1 ] + [ No #2 ] = (Calculate)
  3. User will enter two numbers and press (Calculate) button.
  4. The addon will calculate the result and store the result in the thread ID , which is specified in the admin option.
  5. So the add-on will create a thread which stores the results; each new calculated result as a new post in the thread.
Here's my thought process & prep-work so far:-

  1. Created a new Addon in AdminCP, as 'Adder'
  2. Created a new folder named 'Adder' in the /xf/library/
  3. Installation/Uninstallation Class::Method - Not provided (I'm going to need it to create the tables in DB).
...and I'm staring for the past 20 minutes at the folder 'Adder' :D.

Okay!

I'm thinking I need to do this:-
  1. Create a Route Prefix called 'adder' - because I need to build path http://localhost/xf/adder/
  2. I need to create a template that will create input form & buttons on the path. [I am guessing that this is the 'VIEW' part of the MVC thingy]
  3. Write a MODEL that adds the numbers entered by the user and store the result in Database. I will need to create a DataWriter for this.
Step # 1 - Creating The Route Prefix

I went to AdminCP -> Development -> Navigation -> Route Prefixes -> Create New Route Prefix

Route Prefix: adder
Route Type: Public
Route Class: HUMM -> Gotta Write This Myself
Use Class To Build Link: Always
Addon : Adder

Now created -> Adder/Route/Prefix/Index.php with following code:-
Code:
class Adder_Route_Prefix_Index implements XenForo_Route_Interface
{
   //SOMEONE TELL ME WHAT SHOULD GO HERE AND WHY?
}

Looking for the angels here to give me a push in the right direction.
 
Does XenForo use any route classes anywhere for anything?

How does it do them?
Yes! So I referred to Kier's scratchPad addon (tutorial) and found this -

Code:
public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
    {
        return $router->getRouteMatch('Scratchpad_ControllerPublic_Index', $routePath);
    }

In my case, (I guess) it'd be -

Code:
return $router->getRouteMatch('Adder_ControllerPublic_Index',  $routePath);

But why do I need this 'Adder_ControllerPublic_Index' ? What's it going to do?

PS: I hope this is in the right direction.
 
But why do I need this 'Adder_ControllerPublic_Index' ? What's it going to do?.
The Adder_ControllerPublic_Index puts the C into MVC and basically dictates what you want to do.

So for example create a file called Adder/ControllerPublic/Index.php that should be roughly like:
Code:
class Adder_ControllerPublic_Index  extends XenForo_ControllerPublic_Abstract
{
  
  protected function _preDispatch($action)
  {
  parent::_preDispatch($action);
  }
  public function actionIndex()
  { 
   $adderModel=getModelFromCache("Adder_Model_Adder");
  $adderModel->processSomeStuff($somedata); //(processing stuff)
  return $this->responseView('Adder_ViewPublic_Whatever', 'Adder_whatever', $viewParams);//(preparing view)
}

Best thing to do is to see what other addons are doing
 
I spent quite some time trying to understand how the scratchpad addon is doing it. I found out that the 'Scratchpad_ControllerPublic_Index' has a set of actions :

actionIndex() -> Seems to bring options from AdminCP and do other preparatory work like getting latest notes, dates
actionPostNote() -> Creates a DataWriter and interacts with database to post the notes text to the Database. It also returns a 'view' (this is bit confusing).

Basically, it seems to be doing the real backend job for the addon.
--------------------
My addon needs to fetch only one option from admincp. So I created this AdminCP Option -

Screen Shot 2014-11-20 at 6.13.04 pm.webp

PS: Not sure about Validation Callback and Format Parameters. Do I need to fill anything into those fields?

I then created Adder/ControllerPublic/Index.php ->

PHP:
<?php
class Adder_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
{
    public function actionIndex()
    {
        //Get The Thread ID From Admin Options
        $threadNum = XenForo_Application::get('options')->threadnumber;
    }
    public function postToThread()
    {
        // What all activities should this function do?
    }

}

I'm stuck at this file. Do I need a separate MODEL like in scratchpad addon or perform addition in this file itself? Also, what all methods should I create inside the Adder_ControllerPublic_Index class?
 
Progress Update:

I went ahead and built the installer for the addon. I now have a database table that stores everything: number 1, number 2, result, and calculation ID. I don't know whether I'll need calculation ID as an index; but I've decided to define it anyway.

I also updated the addon-settings to reflect my new installation class::method.

I however couldn't figure out the Controller and Model thingy for this addon. Too many jumbled up thoughts in my head about the controller. :confused:
 
Your controller should do two things:
  • Render a view with form, inputs and button
  • Get inputs (number one and number two) from user submitted form
For your simple task, you don't need a model IMHO.
 
Back
Top Bottom