Aug/090
Joomla 1.6 native MVC blog component tutorial 1: the basics
This is the first part of a series of tutorials about Joomla 1.6 native
Part I: intro to Joomla components
Every Joomla component is divided in two parts, the admin part or the back-end and the front-end part. The files for your component reside in their own folder called com_+{component_name}. The admin part and the front-end part are independent of each other, so we can publish components only for the admin or only for the front-end user, but usually every component has both parts present. For the front-end part, the component folder must be inside the components folder inside your joomla installation: {joomla_server_path}+/components, and for the admin, inside the administrator components folder: {joomla_server_path}+/administrator/components.
The models and the views have their own separate folders called {joomla_server_path}+/components/com_+{component_name}+/views or {joomla_server_path}+/administrator/components/com_+{component_name}+/views and {joomla_server_path}+/components/com_+{component_name}+/models or {joomla_server_path}+/administrator/components/com_+{component_name}/models.
Each part of every component has only one entry point represented by a PHP file with the same name of the component.
Accessing our component is done by adding index.php?option=com_blog_tutorial after your Joomla installation home page's URL
Let's create our entry file for the front-end and just output a string, just for test
<?php echo 'Hello from Joomla blog tutorial component'; ?>
If the hole thing about the structure wasn't clear enough, here a representation of the file system and a download link
Part II: intro to MVC
MVC stands for Model-View-Controller. I won't talk much about the general concept of MVC exept for what is specific to Joomla.
Models inside Joomla
Models deal with retrieving information from the database and passing it to the view. Topically, models are loaded and assigned to views by the controller, one model to one view, but in certain circumstances the view can use more than one model. A model can use other models in the process of retrieving the information
Models are located in a single folder called 'models', inside the component folder. Each model file name is the model's name. The model's class name must have the following structure (let's say that your model is called 'posts'): {Component_name}+Model+{Model_name}
Views inside Joomla
The views for a component are located each in their own folder with the same name, all of that folder inside the views folder of your component. The file names for the views must follow this rule: view+{view_type}+.php. In this example our view is called 'posts' and the view type is 'html', the type of the view can vary, it can be for example 'rss', 'pdf', it basically describes the content.
the role of the views is to retrieve information from the modules, manipulate the information if necessary and than pass it to the 'layouts' as i call them, to display them.
Layouts
Let's say a few words about layouts in Joomla.
Layouts actually display the information, they wrap the HTML tags arrond our posts, let's say, so they can be integrated into an unordered list for example. Ideally they contain only HTML markup and basic control structures, there are examples out \there of functions declared inside layouts, not good, if you have repetitive tasks to do in your layouts, use helpers, but that's another lesson
Layouts reside in the 'tmpl' folder inside the views folders to whom they belong.
Controllers
And finally, the controllers. The role of the controller is to make the connection between the models and the views, this is the typical situation of a GET request. On a POST request, the controller takes over some of the view's roles, meaning that the controller will control the models for, let's say, deleting a comment. There are examples out there that in the case of a POST request, the controller accessed the database to delete things.
This tutorial will help you understand proper techniques for developing native MVC components within Joomla 1.6 environment.
At the time i started this tutorial, Joomla 1.6 was in the alpha state, the development and testing was done with the latest development version from the SVN. I will keep an eye on the development, so this tutorial will be up to date for any changes that will occur with Joomla system.
If you find mistakes, typos, security holes please let me know by posting a comment or by mailing me.
Through this tutorial we'll use the following conventions:
- Your site name: {site_name}
- Your component name: {component_name}
This tutorial was made by studying the core and the components of the Joomla CMS and from experience with developing joomla 1.5 components. I haven't used any copyrighted material, or materials from other tutorials. This tutorial represents just my point of view regarding Joomla development. Also, i am not affiliated in any way with Joomla or Open Source Matters, and this tutorial can not be considered an official one.
This tutorial and the components resulting from it are offered free, and will always be free in some form, you can use them in any way you like without having to ask for permission, but things like: link back, keeping the author and author site (P18X, P18X.com) where they should be, mail or comment with impressions are always welcomed