0

I am looking for examples of how people organise their code in a Joomla extensions, particularly components, that is beyond the basics shown in tutorials teaching you MVC and Joomla.

A look through existing popular, and otherwise, extensions I see plenty of SQL in Controllers and Models doing a lot more that just managing the data and Helpers that seem to be a catch-all for everything else.

In reading blogs about PHP, OO & MVC on good coding practices two points that get mentioned is to keep business logic out of the MVC files and in particular Controllers should only redirect and not do any processing themselves. However there is not much in the way of examples that show where that functionality should go.

A look at Laravel shows a page that describes their way of doing things, https://laravel.com/docs/8.x/structure#the-rules-directory, which is in the direction of what I am looking for in Joomla based on more experienced peoples habits.

To kick things off my own convention so far has been to put utility code(a .csv reader and FTP method) I use across some extensions into a /administrator/com_mycomponent/library/ folder and for methods used across all my extension to handle SQL functions not available in Joomla into a system wide folder /libraries/mycode/database/ that is loaded via plugin.

Still I am struggling with where to put the business logic code and in general better organise my extension code.

Any thoughts appreciated, thanks.

1
  • joomla.stackexchange.com/help/dont-ask Please ask questions that are not subjective / opinion-based. Because 100 people might have 100 different opinions and these opinions may generate debate /discussion, your question may be better suited to a different platform. Commented May 14, 2021 at 7:27

1 Answer 1

1

Think of MVC as a Remote Control, a TV Screen and a DVD player. The remote control (Controller) doesn't know anything about the TV (view) besides controlling the volume + video source. The remote control doesn't know anything about the DVD player (the model with the data) but it can control the player (model) to sends its data to the tv (view).

Joomla follows that Model–View–Controller design pattern. However while Joomla components often have controllers in the /controllers/ folder, Joomla uses the view a bit like a controller for the display task. A view (/[administrator]/components/[some-component]/views/[some-view]/view.html.php) will ask for forms, items (from the model) before it uses that the HTML output from /tmpl/ etc.

Besides that small deviation of the MVC pattern, I would use the models for building the query objects (with SQL), the controllers for tasks besides the default "display" task, the views for all HTML output and the helpers for other things that don't fall under the model/controller, like the back-end menu method addSubmenu (only in Joomla 3, not in Joomla 4).

I would use Joomla's own code as guide to create your own custom components (in combination with the Joomla code standards https://developer.joomla.org/coding-standards/html.html and PHP Code Sniffer https://docs.joomla.org/Joomla_CodeSniffer). That way it will be easier for you to understand Joomla's code. And your code will be easier to understand for other people who work with Joomla's code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.