Introduction to the development of PHPcms module

  • 2020-06-01 08:44:54
  • OfStack

Due to my work, I can only give up the research on mongodb for a while and start to study PHPcms.

So far, I have basically completed the module development. I came here this weekend to make a summary. I found that phpcms is quite good, but it doesn't have much documentation.

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

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

Find his directory structure and the stuff we're going to develop (that is, the modules) is right under /phpcms/modules/

If there's nothing special about developing a module you have to set up a directory structure and you have to design a database table structure and let's say we build a module called my module my_test, right

The following should be the directory structure under mytest

mytest

--class // this is the class that the mytest module will use

-- functions used by function//mytest module

--install// what are the configuration files and myslq statements needed to install this module

--language// is 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 to create a directory structure. This file is also used to control permissions

-- what data models are used by the model.php // module?

-- model. sql// this inserts a record of the model into the database

-- my_test.sql // this file will be executed at installation time, putting in sql to create the database tables

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

--uninstall // configuration and files used to uninstall the module

I didn't study the files in this file.

my_test.php // this is the background controller file 'of the mytest module

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

After setting up one such structure, we need to set up our data model under /phpcms/model/

For example, my_test_model.class.php (which USES a very typical factory pattern)

Let's look at each file one by one. First, let's look at the file we wrote under the model folder.


<?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 .// Multiple libraries can be selected here 
        $this->table_name = 'my_test';// This is the table name , Don't prefix the table 
        parent::__construct();
    }
}
?>     

The purpose of line 1 is to determine if it is within the phpcms running framework.

Line 2 loads the system's model class, followed by the parameter 0 which means not instantiated.

Last 1 call the superclass constructor. Can in phpcms/libs/classes/model class. Found in php

And this model class defines a lot of operations on the data the most basic add, delete, change and check. I'll talk more about some of model's basic methods later.

Then take a look at what's inside the modules

Let's take a look at the first language that supports multilingual menus.

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

This is the structure in the file


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

It's clearly labeled inside

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


$id= $menu_db->insert(array('name'=>' The name of the operation is written here ',      'parentid'=> The father ID, 'm'=>' The module ', 'c'=>' The controller ', 'a'=>' action ',      'data'=>'', 'listorder'=> The sorting , 'display'=>' Whether or not shown '),true);// The last of the true Is used to return ID the 

At the end of the file there should be an array, which is used to insert into the system \language\ zh-cn \ system_menu.lang.php in the following format

$language = array(
    ' Here's the name of your operation '=>' Here is the Chinese translation of the operation ',
     similar :'mytest_init'=>' Display list '
    );

And then model.php which is what data models you're using and what tables you're using

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

And then model.sql is used to insert data into the model table of the system

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

Then mytest.sql the statement that created your database table should be written in this file

And then the template you're using should be in templates and the naming rule should be mytest_add.tpl.php

And then finally, your controller and this is a little bit of research that's going on in the controller for each of the url that you pass to the action which is a=? The default action is init


<?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;// Call the constructor of the parent class 
  }
  public function init(){
    echo " Here is the default action method ";
  }
  public function add(){
    include $this->admin_tpl('mytest_add');// Use the template method 
  }
}

Once the controller is written, we can install our module after we have written all the above files.


Related articles: