SoFunction
Updated on 2025-03-10

PHPcms Notes: A preliminary study on the development of PHPcms modules

Due to working relationships, I can only temporarily give up my research on mongodb. I started to study PHPcms.

So far, I have basically completed the module development. I came here to make a summary while I was on the weekend. I found that the phpcms is pretty good, but there are really not many documents or anything else.

Let’s stop talking nonsense. For the module development of phpcms, you must first understand the directory structure of the module.

We can do it in /html/2010/structure_0928/

Find its directory structure. The thing we want to develop (that is, modules) is below /phpcms/modules/

If nothing special, before developing a module, you must first establish related directories and design the database table structure according to the directory 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 a class that mytest module will use

--function// Functions used by mytest module

--install// Install this module requires some configuration files and creating data table myslq statements and what's here

--language//I will use it when multilingual

--//This configuration file is used to describe some information about the entire module

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

--//What data models does the module use? (It can be understood as which tables are used.)

--//This record of inserting the model into the database

--my_test.sql//This file will be executed during installation, and the SQL that creates the database table will be put in

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

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

I haven't studied the files in this one. I've studied them again and added them.

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

//This is the front desk controller, I didn't write anything about this.


 

 

After establishing such a structure, we also need to build our data model under /phpcms/model/

For example, my_test_model. (This uses a very typical factory model)

Let’s take a look at what we write in each file one by one. First, let’s take a look at the file we write under the model folder.

Copy the codeThe code is as follows:

<?php
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 multiple libraries are available, you can select the library here
$this->table_name = 'my_test';//This is the table name, no table prefix is ​​required
        parent::__construct();
    }
}
?>    

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

The second line loads the system's model class, 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/

This model class defines many data operation methods. The most basic addition, deletion, modification and search. I will talk about some basic methods of model in detail in the future.

Let's take a look at the modules

Let's look down one by one. The first language is used to support multilingual menus.

Then, this one writes some information about the module installation.

This structure is inside the file

Copy the codeThe code is as follows:

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

It's marked very clearly

Next, this file is used to create the directory structure of the background management menu, and is also used to control permissions.

Copy the codeThe 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' is displayed'), true);//The last true is used to return the ID

The file should have an array at the end, which is used to insert the system's \language\zh-cn\system_menu. The format is as follows
Copy the codeThe code is as follows:

$language = array(
'This is the name of the operation you named'=>'This is the Chinese translation of operation',
Similar: 'mytest_init'=>'Show List'
    );

Then this is what data models you use. It can be understood as what tables you use.
Copy the codeThe code is as follows:

return array('mytest','my_test_artcle');

Then this is used to insert data into the system's model table
Copy the codeThe code is as follows:

INSERT INTO `phpcms_module` (`module`, `name`, `url`, `iscore`, `version`, `description`, `setting`, `listorder`, `disabled`, `installdate`, `updatedate`) VALUES ();

Then the statement to create your database table should be written in this file

Next is the template you are using. It should be placed in templates. The naming rule should be mytest_add.

Finally, it is your controller. Some of them have studied this. The controller is an action that is passed on to each url, that is, a=?. The default action is init

Copy the codeThe code is as follows:

<?php
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;//Calling the constructor of the parent class
}
public function init(){
echo "This is the default operation method";
}
public function add(){
include $this->admin_tpl('mytest_add');//Methods to use templates
}
}

The controller has been written. We can install our module after we have finished writing all the files above.