Php Note: Initial Exploration of PHPcms module development Introduction _ php instance

Source: Internet
Author: User
This article introduces the application of the PHPcms module. If you need a friend, please refer to the following: Due to work relationship, you can only temporarily stop the mongodb study and start to study PHPcms.

So far, I have basically completed the development of the module. I am here to make a summary over the weekend. I found that phpcms is well written, but there are not many documents.

Let's not talk nonsense. For the module development of phpcms, first we need to understand the directory structure of the module.

We can be in http://v9.help.phpcms.cn/html/2010/structure_0928/69.html

Find the directory structure and the stuff (that is, the module) We want to develop is under/phpcms/modules /.

If there is nothing special, before developing a module, we should first establish a directory according to the directory structure and design a database table structure. For example, we should create a module named my_test.

The directory structure in mytest is as follows:

Mytest

-- Class // This is the class used by the mytest module.

-- Function // functions used by the mytest Module

-- Install // install some configuration files required by this module and create myslq statements for data tables.

-- Language // used in multiple languages

-- Config. ini. php // This configuration file is used to describe some information about the entire module.

-- Extention. inc. php // This is used to create a directory structure. This file is also used to control permissions.

-- Model. php // which data models are used by the module (which tables are used .)

-- Model. SQL // records of the model inserted into the database

-- My_test. SQL // This file will be executed during installation, and the SQL statement for creating a database table will be put in

-- Templates //, template file used by the mytest Module

-- Uninstall // The configurations and files used to uninstall the module

I have not studied the files in this file.

My_test.php // This is the background controller file of the mytest module'

Index. php // This is the front-end controller. I have nothing to write about.

After creating such a structure, we also need to create our data model under/phpcms/model /.

For example, my_test_model.class.php (this uses a typical factory Mode)

What is written in each file? Let's look at it one by one. First, let's look at the file we wrote under the model folder.

The Code is as follows:


Defined ('in _ PHPCMS ') or exit ('no permission resources .');
Pc_base: load_sys_class ('model', '', 0 );
Class my_test_model extends model {
Public function _ construct (){
$ This-> db_config = pc_base: load_config ('database ');
$ This-> db_setting = 'default'; // default database configuration. // You can select multiple databases here.
$ This-> table_name = 'my _ test'; // this is the table name. No table prefix is required.
Parent: :__ construct ();
}
}
?>


The role of the first line is to determine whether it is in the phpcms runtime framework.

The second line loads the model class of the system. The following parameter 0 indicates that it is not instantiated.

The last line calls the constructor of the parent class. It can be found in phpcms/libs/classes/model. class. php.

This model class defines many basic addition, deletion, modification, and query operations on data. I will discuss in detail some basic methods of the model later.

Next let's take a look at the stuff in modules.

The first language is used to support multi-language menus.

Then config. ini. php, which contains information about the module installation.

This structure is in the file.

The Code is as follows:


$ Module = 'mytest'; // model used
$ Modulename = 'here is the module name ';
$ Introduce = 'module description information ';
$ Author = 'author ';
$ Authorsite = 'author website ';
$ Authoremail = 'author email ';


The content is clearly marked.

Next, the extention. inc. php file is used to create the directory structure of the background management menu and to control permissions.

The Code is as follows:


$ Id = $ menu_db-> insert (array ('name' => 'Operation name', 'parentid' => parent ID, 'M' => 'module ', 'c' => 'controller', 'A' => 'action', 'data' => '', 'storder' => sort, 'display' => ''), true); // the last true value is used to return the ID


The file should end with an array, which is used to insert the system's \ language \ zh-cn \ system_menu.lang.php file in the format below

The Code is as follows:


$ Language = array (
'Here is the name of your operation '=> 'here is the Chinese translation of the operation ',
Similar to: 'mytest _ init' => 'display list'
);


Then there is model. php. This is what data models you use, which tables can be understood as used.

The Code is as follows:


Return array ('mytest', 'My _ test_artcle ');


Then there is model. SQL, which is used to insert data into the system model table.

The Code is as follows:


Insert into 'phpcms _ module' ('module', 'name', 'url', 'iscore', 'version', 'description', 'setting', 'storder ', 'Disabled ', 'installdate', 'updatedate') VALUES ();


Then the mytest. SQL statement used to create your database table should be written in this file.

Then, the template you are using should be placed in the templates. The naming rule should be mytest_add.tpl.php.

Finally, you have made some research on your controller. In the controller, the action passed to each url is a =? The default action is init.

The Code is as follows:


Defined ('in _ PHPCMS ') or exit ('no permission resources .');
Pc_base: load_app_class ('admin', 'admin', 0 );
Class mytest extends admin (){
Public function _ construct (){
Parent: :__ construct; // call the constructor of the parent class.
}
Public function init (){
Echo "here is the default Operation Method ";
}
Public function add (){
Include $ this-> admin_tpl ('mytest _ add'); // use the template method.
}
}


After writing all the above files in the controller, we can install our module.

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.