Route, Listener, ControllerPublic, What?

Status
Not open for further replies.

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,189
Sometimes I get the question: How do I know when to use what, or what does this stand for?
I do not know the answer, well, a little. I am still learning, but I will share what I know so far.

XenForo uses a framework, some stuff is from Zend, the majority is their own. But compared to many frameworks for PHP it's kinda the same, kinda.

You will find add-ons with directories such as Route/ Listener/ Controller/ Model/ etc.

Here's my quick and dirty explanation that helps you get started. And by all means, please do extend on it in the comments, I need to improve what I know (big time).

Route
Usually not used, unless you have to introduce a new route or manipulate one.
For example, the XenFans "Popular Content" plugin uses a route, because it is a new .com/popular/ page.

Listener
Used when you have code event listeners, such as injecting your own template inside existing templates. Great for certain customizations where you want upgrades to new versions to be smooth, without touching the original templates. The XenFans "Extra Tabs" plugin uses this to add additional tabs to the navigation template.

Model
Used to communicate with the database, you get data in there, use it to query the database, and get data out of there. The XenFans "Promoted Accounts" add-on for example uses this to get the right users from the database. So we know which accounts are promoted (that we want to display later).

Views
Or ViewPublic or Public, this seems to vary a bit with XenForo, but it's a helper for the templates. Basically once we're done with whatever we have, we parse it back on to the templates and get an output to the user. Maybe it's that English isn't my first language, but I find it a bit hard to explain this one properly.

Controller
This is basically the guy that takes the input, decides how to handle it and directs the rest of the system as to what to do next.

Now that we have a beginners idea of what things are, in a very dirty and simple visual explanation you could view it like this.

When someone goes to say /popular/ it has to be a route that exists. So we need a Route.
Since we now want something, let's provide the Controller with some data. Which could call a Model if some data has to be queried. And pass it on to a public viewer that handles the template and gives it what it needs.

So some plugins don't need a new Route, some don't need a model.
Sometimes it's as simple as 'hook into this template, with our template' and creating a code event listener for this is enough.

And basically if you need to think about creating a plugin you need to go through the thinking steps of 'is it a separate new page? does it need to query data from the database? do i need to create my custom templates? etc.' Which might help you get started prototyping your add-on. Slowly populating the files until you end up with a working alpha ready for testing.

I am looking forward to you guys posting better explanations, perhaps I am simply seeing this incorrectly, please let me know.
 

Mikey

:mikey:
Staff member
Joined
Jan 26, 2008
Messages
17,835
MVC in general is a pretty hard (and abstract! nothing "real world" about it) concept to grasp at first, along with Object Orientated Programming. This is how I got it:

I like to think of the controller model relationship as a roundabout (circle) sort of thing. Route asks controller what to get, controller consults with model and then model sends back any validation data, any save actions, to the controller which eventually spits out the view to the user.

Diagram to help visualise maybe...?

mvc.png
 

Fuhrmann

OMG Member
Joined
Oct 27, 2011
Messages
93
I have to say, this was a really good article. I really enjoy.

I love making tutorials. I am currently writing a basic one...Floris, do you mind if I take a paragraph or other from this article?

I'll credit to you when I publish.

@topic

I started learning MVC when I started developing add-ons to XenForo. I still have so many doubts, but I think the way you wrote the "Model, Controller, View and Route" explanation to us it is basically what it is.

The diagram that Mikey posted it is very helpfull too. I whish i have more learning of MVC to make a huuge tutorial to everyone.

Thank you Floris!
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,189
Thank you for the kind words, and for asking. Yeah, that is ok.
 
Status
Not open for further replies.
Top