Home  >  Article  >  Backend Development  >  A preliminary introduction to PHPcms module development_PHP tutorial

A preliminary introduction to PHPcms module development_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:13726browse

Due to work, I can only give up the research on mongodb temporarily and start researching PHPcms.

So far I have basically completed the development of the module. I will come here to make a summary during the weekend. I found that phpcms is pretty good, but there are really not many documents.

No more nonsense. For phpcms module development, you must first understand the directory structure of the module.

We can download it at http://v9.help.phpcms.cn/html/2010/structure_0928/69.html

Find its directory structure. The stuff we want to develop (that is, the module) is under /phpcms/modules/

If there is nothing special, before developing a module, you must first establish the relevant directories according to the directory structure and design the database table structure. For example, we create a module called my module my_test

The following should be the directory structure under mytest


mytest

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

--function//Function used by mytest module

 --install//Some configuration files needed to install this module and create data table myslq statements are here

 --language//Will be used when using multiple languages ​​

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

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

 --model.php//What data models are used by the module. (It can be understood as which tables are used.)

 --model.sql//This is the record of inserting the model into the database

 --my_test.sql//This file will be executed during installation, put the sql to create the database table

--templates //, template files used by the mytest module

--uninstall //Configuration and files used when uninstalling the module

I didn’t study the files in this. I will study them later and make up for them.

my_test.php //This is the background controller file of the mytest module`

index.php//This is the front-end controller, I didn’t write anything on this.


After establishing such a structure, we still need to establish our data model under /phpcms/model/

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

Exactly 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.//If you have multiple libraries, you can choose the library here
$this->table_name = 'my_test';//This is the table name, no table prefix is ​​needed
parent::__construct();
}
}
?>


The function of the first line is to determine whether it is within the running framework of phpcms.

The second line loads the model class of the system, and the following parameter 0 means 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

And this model class defines a lot of data operation methods, the most basic addition, deletion, modification and query. I will talk about some basic methods of model in detail later.

Let’s take a look at the stuff inside modules

Let’s look at the following one by one. The first language is used to support multi-language menus.

Then there is config.ini.php, which contains some information about module installation.

The file has this structure

The code is as follows:
$module = 'mytest';//model used
$modulename = 'Here is the name of the module';
$introduce = 'Module description information';
$author = 'Author';
$authorsite = 'Author website';
$authoremail = 'Author email';


It’s clearly marked inside

Then there is extension.inc.php. This file is used to create the directory structure of the background management menu and is also used to control permissions

The code is as follows:
$id= $menu_db->insert(array('name'=>'The operation name is written here', 'parentid'=>Parent ID, 'm'=>'Module', 'c'=> ;'Controller', 'a'=>'Action', 'data'=>'', 'listorder'=>Sort, 'display'=>'Whether to display'),true);// The last true is used to return the ID


There should be an array at the end of the file. This array is used to insert into the system's languagezh-cnsystem_menu.lang.php. The format is as follows

Copy code The code is as follows:
$language = array(
'Here is the operation name you gave' => 'Here is the Chinese translation of the operation',
Similar:'mytest_init'=>'Show list'
);


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

Copy code The code is as follows:
return array('mytest','my_test_artcle');


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

Copy code The code is as follows:
INSERT INTO `phpcms_module` (`module`, `name`, `url`, `iscore`, `version`, `description`, `setting`, `listorder`, `disabled`, `installdate`, `updatedate`) VALUES ();


Then mytest.sql. The statements to create your database table should be written in this file

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

Finally, there is your controller. This has been researched. The controller contains the action passed for each URL, which is the action of 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');//How to use templates
}
}


It's written in the controller. After we finish writing the above files, we can install our module.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/372002.htmlTechArticleDue to work, I can only give up the research on mongodb temporarily. I started researching PHPcms. So far I have basically completed it. The development of the module. I came here to make a summary during the weekend. I found php...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn