Analysis of Joomla 2.5 MVC

Source: Internet
Author: User
Tags one table

The entire system of Joomla, composed of core, component, module, plugin, and component is the only part with data structure features, so Joomla designs it as an MVC structure to make the data structure more clear. And component is the most complex part of the development of Joomla expansion.

Joomla provides the base class for MVC: JView, Jmodel, Jcontroller, JTable. A complete Joomla MVC must inherit from these four classes or its derived classes, and Joomla2.5 has richer derived classes to satisfy joomla2.5 more new features. You can read the Com_content code to understand the relationship.

JView: Views, including templates and data output logic. The typical view.html.php is an implementation of JView, which obtains data from the model and outputs it to the template.

Jcontroller: Controller, a component can have multiple controllers, each controller will have more than one task, it is the entrance of the program, want Joomla to do for you, you have to give it a task. For example User.edit, User.save.

JTable: A data table that deals directly with databases and abstracts database tables into a JTable object. It provides actionable data to the model and can preprocess table operations. For example, when writing to a table, the title is not very empty value judgment. In general, it interacts only with the model, and the rest of it is best not to use jtable directly.

Jmodel: The model, which provides more abstract data management for the system, can make it possible for developers not to care about database operations.

MVC Class Naming conventions

By default, the host controller Acontroller, child Controller Acontrollerb Association (AMODELB,TABLEB,AVIEWB)

And TableB this name is easy to conflict with other component names, so it is generally specified in the Amodelb prefix, the TableB changed to Atableb.

Relationships such as MVC are created automatically and do not require additional declarations. For example, using Getmodel in AVIEWB, the system automatically acquires Amodelb, and gettable is automatically obtained when AMODELB is used. Just contact with the development of Joomla, may be due to write the wrong class name and error.

A component from the zero architecture is a headache, because there are also a lot of architecture code, fortunately there are online component generators:

Http://www.notwebdesign.com/joomla-component-creator/component/combuilder/components

You can use it to build a component installation package based on the table structure you define, and you can immediately have background functionality after installation, eliminating the amount of architectural time.

The relationship between MVC

Table is a data structure that is a structural prototype of data stored on a database. It can only represent one table. Each table of Joomla must have a corresponding jtable inheritance, which gives the model the details of the table. As long as the jtable inheriting class is built, it is generally only manipulated in model, and other places do not have to manipulate jtable instances.

To get a jtable in Jmodel:

$table = $this->gettable (' Users ', ' userstable ');

If it is necessary for a domain to access table across components, you can load additional table paths using the following methods

Jtable::addincludepath (Jpath_administrator. Ds. ' Tables ');

Model is a data modeling, with it, users do not have to understand the details of the data storage, just tell the model how you want to manipulate the data, such as you want to get a list of users, or delete a user, tell it, it will do what you want to do. In general, to get the data for a component, you first need to find its Jmodel inheriting class and interact directly with it.

Controller and model have a direct relationship, so Jcontroller get a Jmodel have shortcut:

$model = $this->getmodel (' Active ', ' Usersmodel ');

In model, you may need to use the model of other components, such as content and Category,model, to get other model methods

Jmodel::addincludepath (Jpath_administrator. Ds. ' Components '. Ds. ' Com_content '. Ds. ' Models ');

$model = jmodel::getinstance (' articles ', ' Contentmodel ', Array (' ignore_request ' = true));

PS: Because PHP is very simple and flexible, can many phper do not pay attention to the development of norms, so long-term result is the project quality decline. For example, in Joomla to manipulate the database, most people will write directly to SQL, read write. This may not have much to do with Joomla1.5, because joomla1.5 's tables are not closely related. However, there are many association tables in Joomla2.5, such as ACL function, User group, there are a large number of related tables, simply write a table, the system may not be recognized. In the MVC system, model is the part that writes the database to deal with, all SQL should write in model, want to use data, call model method.

Call the model example:

$model = jmodel::getinstance (' articles ', ' Contentmodel ', Array (' ignore_request ' = true));

$model->setstate (' filter.state ', 1);

$model->setstate (' list.ordering ', ' publish_up ');

$items = $model->getitems ();

PS: If there is a ignore_request, the populatestate is not called, that is, the state is not obtained from the front-end supply.

State

The most standard way to interact with model is with state, which is a class built-in data structure.

$model = $this->getmodel (' Active ', ' Usersmodel ');

$model->setstate (' limit ', 5);

$model->getitems ();

Sub-controller

JOOMLA2.5 supports multiple sub-controllers for one component

Call method: task= controller suffix name. method name, i.e. Task=user.save, Task=users.delete, Task=user.edit

If there is a sub-controller userscontrolleruser, there is a way to save, call is Task=user.save

Controllers with management functions

Jcontrollerform primarily for editing pages

Jcontrolleradmin primarily for managing list pages

The list page uses Jcontrolleradmin, and the edit page uses Jcontrollerform

Task expression for the child controller: task= controller name. Method Name, Task=user.save, Task=users.delete, Task=user.edit

Jcontrollerform main task has edit, add, save

Jcontrolleradmin main task has delete, publish, Saveorder

If there is a Jcontrollerform derived class Usercontroller, then its add address is: Index.php?option=com_user&task=user.add

Modified address is: index.php?option=com_user&task=user.edit&id=1

It must have task=user.save in the edit form

Similarly, the Jcontrolleradmin derived class Userscontroller, there is Task=users.delete, task=users.publish

Note: Do not attempt to use other access addresses, and if you do not use Jcontrollerform's task, the system will assume that the source of the operation is not trusted and will prohibit your operation

Model classes with management capabilities

Jmodeladmin is a derived class for jmodelform, and for implementing a comprehensive management function, you can use only jmodeladmin

Use Jmodelform to at least rewrite Save,getitem,loadformdata and getform

Save: Operation when data is accessed

GetItem: Used to extract item data for form

GetForm: To get a Form object, the general practice is to return the loadform result, Loadform reads the generated jform data, which is the XML file under the form directory

Loadformdata: The default value used to generate Jform, used to edit the data of form

Using jmodeladmin only needs to rewrite Getitems,loadformdata and GetForm

Note: The form needs to have token, <?php echo jhtml::_ (' Form.token ');?>

Model classes with management capabilities

Jmodeladmin is a derived class for jmodelform, and for implementing a comprehensive management function, you can use only jmodeladmin

Use Jmodelform to at least rewrite Save,getitem,loadformdata and getform

Save: Operation when data is accessed

GetItem: Used to extract item data for form

GetForm: To get a Form object, the general practice is to return the loadform result, Loadform reads the generated jform data, which is the XML file under the form directory

Loadformdata: The default value used to generate Jform, used to edit the data of form

Using jmodeladmin only needs to rewrite Getitems,loadformdata and GetForm

Note: The form needs to have token, <?php echo jhtml::_ (' Form.token ');?>

In addition, several new derived classes of Joomla are introduced:

Jmodellist

In most CMS, pagination and sort lists are already standard features, and developers don't have to waste time on paging and sorting, and Jmodellist has already implemented both functions.

GetItems: Gets the list data, the general model uses it to obtain the list, inherits from the Jmodellist does not have to overload to it.

Getlistquery: Abstract method. query string or query, this is Jmodellist unique method for GetItems query data, order query statement must be written in this method, limit and pager do not have to write.

Populatestate: Called at GetState to process the front-end commit data for user interaction. Jmodellist has extended the interaction of sorting and paging to it, and can reload it if more interactivity is required.

PS: If you do not call GetState before using model, you will not be able to receive user-submitted state.

Jmodelform

Optimize model's form capability

Loadform: Loads the form. (The form here is joomla2.5 's jform, a technique for defining a form with XML, so the XML file must be built first.)

GetForm: Abstract method. Get a form instance, usually by calling Loadform to get the form instance, and then processing it.

Loadformdata: Abstract method. To provide data to the edit form, you can directly use the GetItem data.

Ps:form need to have token, <?php echo jhtml::_ (' Form.token ');? >,token is a form's authentication signature, in order to prevent the form from being submitted across domains.

Jmodeladmin

The function is very rich to manage the data model, inherits from Jmodelform, has the form and the Save, the delete,publish,saveorder,batch,checkin,checkout and so on function.

Transferred from: http://www.cnblogs.com/catcat811/archive/2012/07/22/2590738.html

Analysis of Joomla 2.5 MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.