- Details
- Written by: Matthew Murphy
- Category: How-To Guides
- Hits: 3132
JDOC:Words to watch - Joomla! Documentation - http://docs.joomla.org/JDOC:W2W
CSS - Joomla! Documentation - http://docs.joomla.org/CSS
Understanding Output Overrides - Joomla! Documentation - http://docs.joomla.org/Understanding_Output_Overrides
- Details
- Written by: Matthew Murphy
- Category: How-To Guides
- Hits: 2995
Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present lots of data to the user, one often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface do not impact the data handling, and that the data can be reorganized without changing the user interface. The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. It is common to split an application into separate layers: presentation (UI), domain, and data access. In MVC the presentation layer is further separated into view and controller. MVC encompasses more of the architecture of an application than is typical for a design pattern.
Model The domain-specific representation of the information on which the application operates. In Joomla it is MySQL database tables. Joomla model classes basically contains table schemes. Formerly known as mosTable.
View Renders the model into a form suitable for interaction, typically a user interface element. In Joomla it is set of View class and 1 or more templates.
Controller Processes and responds to events, typically user actions, and may invoke changes on the model. In Joomla process tasks. to trigger tasks, Every thing you need is to create methods in controller class with the same name as a task.
Joomla MVC Works as follows
- User access Component (without any task or controller variables)
- Default Controller Class constructed. Then controller call default view and displayed. Of course this controller render default view.
- User click on any control. The control should have task and controller variables in URL, or only task. (E.g. index.php?option=com_mvc&controller=books&task=view)
- If controller passed Joomla looks for new controller file and construct it. Then Render according the task.
Where is the Model? Model is used inside controllers' task functions. If it is for example Publish task, function publish will launch appropriate model to change published field.
File structure explanation
- Component
- Controllers
- Controller1.php
- Controller2.php
- Views
- View1
- Tmpl
- View1.php
- View2.php
- View.html.php
- Tmpl
- View2
- Tmpl
- View1.php
- View2.php
- View.html.php
- Tmpl
- View1
- Models
- Model1.php
- Model2.php
- Admin.component.php
- Controller.php
- Controllers
Controller folder contains controller classes. Foe example for Book Library, Book Category controller, Book controller, Book Publisher controller and so on.
Models contain Model classes. One Model class is equal to one DB table.
View folder contains view classes and templates. Every view class may have few templates that are stored in tmpl folder. Every view class has the same name view.html.php. And tmpl folder contains html template files. Admin.component.php is a component launch file and controller.php is default controller. Let's create most simple MVC Component.
We will create component called mvc.
- Create folder administrator/components/com_mvc.
- Create file administrator/components/com_mvc/admin.mvc.php. This is standard Joomla file that will be launched on access to the component.
File listing: admin.mvc.php
- <?php
- require_once (JPATH_COMPONENT.DS.'controller.php');
- if($controller = JRequest::getVar('controller')) {
- require_once (JPATH_COMPONENT.DS.'controllers'.
- DS.$controller.'.php');
- }
- $controller = new $classname( );
- $controller->execute( JRequest::getVar('task'));
- $controller->redirect();
- ?>
Line 2 is to protect document from direct access. line 4 to require default controller. This controller will render pages if controller GET/POST variable is not set. 6-8 to include other controller class if controller GET/POST variable was set. 10-11 create new controller class. 13-14 execute controller or render page according task
This file is quite simple. First it includes controller, then create class, then execute.
- Create administrator/components/com_mvc/controller.php. This is controller we includes in line 4 of admin.mvc.php. This controller will be launched if there was not any GET/POST controller variable found.
File listing: controller.php
- <?php
- jimport('joomla.application.component.controller');
- class MvcController extends JController
- {
- function display() {
- parent::display();
- }
- }
- ?>
2d line to include Joomla API controller class for extending new one created in line 4. Function display render default view. More about that later.
Basically that is all we need. If instead parent::display(); we put echo "test this"; we will see this on the main page of the component. But let's create fully structured MVC component. For that we need to create View for this controller.
Default view should have the same name as the component.
- Create folder administrator/components/com_mvc/views.
- Create folder administrator/components/com_mvc/views/mvc. To create default view we need to name this folder as we named component.
-
Create file administrator/components/com_mvc/view/mvc/view.html.php. This file will have view class. We do not use "mvc" word in the name of this file. Every view class has different folder but the same file name. it is view.html.php
File listing: views/mvc/view.html.php
- <?php
- jimport( 'joomla.application.component.view');
- class MvcViewMvc extends JView
- {
- function display($tpl = null)
- {
- parent::display($tpl);
- }
- }
- ?>
Line 2 we include Joomla API View class to extend new one we create in line 3. 5-10 create function display(). This function is called by default, by MvcController class if not task called.
I used JToolBarHelper::title() to display tile. Here you can also use other JToolBarHelper methods. Also here you can make DB queries and prepare HTML elements to render such as Published Yes/No radio button set, Public/Registered/Special access select list, pageNav class to create navigation and other. Please, note the difference. In this view class we use Models(tables) and SQL queries to get information to create HTML elements to render. But in controller we make SQL queries and use models to control and operate records. Something like delete, publish, save, ...
Function display() will be called by default if there is no GET/POST task variable set. parent::display($tpl); will call default template if POST/GET layout variable is not set. So let's create default template.
- Create file administrator/components/com_mvc/view/mvc/tmpl/default.php.
File listing: views/mvc/tmpl/default.php
<h1>MVC Main</h1>;
<a href="index.php?option=com_mvc&controller=list">List something</a>;This is HTML document. This template will display H1 header and link to other controller. This is complete MVC minimum component. We only lack Models.
Please, save all files and try to call your new component. Login to Joomla admin and put to address line /index.php?option=com_mvc. Before you continue please, be sure all code works fine till this point.
Everything you will read below just to explain some more things. How to control different tasks and different Views within one Controller. For that, let's create another section that will be controlled by Controller list. First link to controller is without task so we will call default View again and create some more links with tasks to call other Views.
- Lets imagine we clicked on index.php?option=com_mvc&controller=list link. That means we enable following lines in admin.mvc.php.
File listing: views/mvc/tmpl/default.php
- if($controller = JRequest::getVar('controller')) {
- require_once (JPATH_COMPONENT.DS.'controllers'.
- DS.$controller.'.php');
- }
That means we need to create file administrator/components/com_mvc/controllers/list.php.
- Create it.
File listing: controllers/list.php
- <?php
- jimport('joomla.application.component.controller');
- class MvcControllerList extends JController
- {
- function __construct()
- {
- parent::__construct();
- }
- function display() {
- JRequest::setVar('view', 'list');
- parent::display();
- }
- }
- ?>
JRequest::setVar('view', 'list'); tells this controller to look for View in views/list folder. If we skip or delete this line, this controller class will look for View in views/mvc default folder. So if we told controller to look for View in list folder, let's create it.
- Create folder administrator/components/com_mvc/view/list
- Create folder administrator/components/com_mvc/view/list/tmpl
- Create file administrator/components/com_mvc/view/list/view.html.php
File listing: views/list/view.html.php
- <?php
- jimport( 'joomla.application.component.view');
- class MvcViewList extends JView
- {
- function display($tpl = null)
- {
- parent::display($tpl);
- }
- }
- ?>
I won't explain this file. It is the same as views/mvc/view.html.php, only name of the class is different.
- Create file administrator/components/com_mvc/view/list/tmpl/default.php
File listing: views/list/tmpl/default.php
-
That is it. It should work ok now. I mean you can easily navigate from MVC component homepage to list controller homepage.
But this file will display few links that have different tasks within same Controller. Here is how you can handle them. Start with task=new_task.
- Create function new_task in File controllers/list.php.
File listing after adding function: controllers/list.php
- <?php
- jimport('joomla.application.component.controller');
- class MvcControllerList extends JController
- {
- function __construct()
- {
- parent::__construct();
- }
- function display() {
- JRequest::setVar('view', 'list');
- parent::display();
- }
- function new_task()
- {
- JRequest::setVar('view', 'list');
- JRequest::setVar('layout','newtask');
- parent::display();
- }
- }
- ?>
We created function new_task() in line 13-18, that will be called if GET/POST task variable equal to new_task. JRequest::setVar('view', 'list'); tells controller to look for View in views/list/ folder. JRequest::setVar('layout','newtask'); tells controller that View should display newtask template or really newtask.php. Let's create it.
- Create file administrator/components/com_mvc/view/list/tmpl/newtask.php
File listing: views/list/tmpl/newtask.php
This file displays some text and link back to list controller homepage. Save all files and try it. Try to navigate to list controller homepage and click on new_task link. If you see text of above listing you are done. Congratulation. You have just created new task.
-
One more thins to know. It is possible to call same method with different tasks. For that add $this->registerTask( 'test' , 'new_task' ); to construct method of controllers/list.php.
Final listing: controllers/list.php
Save all files and now try click test link on list controller homepage.- <?php
- jimport('joomla.application.component.controller');
- class MvcControllerList extends JController
- {
- function __construct()
- {
- parent::__construct();
- $this->registerTask( 'test' , 'new_task' );
- }
- function display() {
- JRequest::setVar('view', 'list');
- parent::display();
- }
- function new_task()
- {
- JRequest::setVar('view', 'list');
- JRequest::setVar('layout','newtask');
- parent::display();
- }
- }
- ?>
- Later I'll write a lesson on how to use Models and we will create simple guestbook component.
- Details
- Written by: Matthew Murphy
- Category: How-To Guides
- Hits: 1066
Joomla Nativity Standards (JNS)
Designing DB
- Every table should contain published field where it is applicable
- Every table should contain checked_out and checked_out_time fields if this table records managed in backed.
- Every table should contain access field where it is applicable
Development
- Component should be developed in complete MVC model of Joomla 1.5 taking in account all Joomla MVC standards
- MVC implementation should be done through core Joomla JController, JView, JModel classes.
- File structure should conform
- In administration component should use Joomla core Toolbar to create Functional buttons
- Every list of records have to have checkin and checkout functionality implemented to prevent double editing record at the same time.
- Every list of records should have published/unpublished opportunity
- Every list of records should have access column where it is applicable and its implementation should work in frontend.
- All error messages should be outputted through JError class
Styling Admin
- All standard toolbar buttons functions like publish, unpublish save, apply, close, edit, etc should use template images
- Custom button icon should be in the same style
- You should think out interface using Joomla interface elements like Sliders, Tabs, Toolbar, ... as much as possible
- Generating tables and forms standard CSS classes like admintable, adminform, ... should be used
- Everything component generate and display either in back-end or front-end should be formatted with appropriate core Joomla classes that should guaranty best outlook with any template.
- Personal formatting may be used but it should not format colors and borders
Styling Frontend
- All elements rendered on frontend should be formated by core Joomla CSS classes. For example main title: .componentheading, subtitle: .contentheading, data/time: .createdate, .modifydateand so on.
- Special attention to format tables that list items. Do not forget table classes like .sectiontableheader, .sectiontableentry1, .sectiontableemtry2. I noted that many ignore that.
- Elements should be placed on the page in general Joomla style.
By one word, user should not think a lot how to adopt component to looks great in his currect template but just install component and use it. I mean that even if user need to edit template just to change positions and order of elements but not color and font size.
- Details
- Written by: Matthew Murphy
- Category: How-To Guides
- Hits: 2695
- Details
- Written by: Matthew Murphy
- Category: How-To Guides
- Hits: 1515
The difference between modules and components
Unless you understand the difference between a module and a component, you will find it very difficult to administer your Joomla installation. This article will explain the difference between modules and components and give examples of the usage of each of them.
Modules
Modules are usually small pieces of functionality designed to present information in your site. They can appear a number of times, on a number of pages in various positions. On the contrary components are a full blown application, usually quite complex and are to the main body if a page. Also each module can be viewed in various pages, and the position of each module can be easily changed. You are allowed to select the menu items where a module is displayed. A good example of the usage of this functionality is targeted advertising. If your site has various sections, you can create module for each section. Each module will contain a targeted advert for the section. Now instead of showing all the modules, all the time, you customise the visibility of the module such that each advert is displayed only in the relevant menu links. Below you can see a screenshot of how to choose the menu items where a module is displayed. The module is displayed in the highlighted menu items only.
Typical examples of standard Joomla modules are the following. As we can see the function of each of the modules mentioned is quite restricted.
-
Main / User / Top and other menus
-
Latest News
-
Popular Articles
-
Polls
-
Login Form
-
Syndication Details
-
etc...
3rd Party Modules include
-
Modules for displaying AdSense code
-
Modules for displaying featured articles
-
Modules for rotating images / scrolling images etc.
Installed modules can be found in the Modules > Site Modules menu, or Installers > Modules. Configuration of modules is typically very easy, and is done by clicking on the module in Site Modules page.
