Model-View-Controller (MVC)
- Details
- Written by: Matthew Murphy
- Category: How-To Guides
- Hits: 79
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
View2
Tmpl
View1.php
View2.php
View.html.php
Models
Model1.php
Model2.php
Admin.component.php
Controller.php
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.
The component we will create can be downloaded here.
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
