Home  >  Article  >  Backend Development  >  phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:49:26993browse

phpcms V9 added module (reprinted), phpcmsv9

reprinted from: http://www.cnblogs.com/Braveliu/p/5101345.html

is The development process of creating a module in phpcms

【1】Create a module directory

Through the previous study, we already know that the modules in the phpcms V9 framework are located in the phcms/modules directory. Each directory is called It is a module.

If you want to create a module, just create a folder under the phpcms/modules directory and put your controller class in it.

For example, if I want to develop a module called test, then first create a folder in the phpcms/modules directory and name it test.

Observing the structure of other modules, we can see that the standard structure of the test module should usually be like this:

classes is the module class library package

functions is the module function library package

templates is the module template package, which usually contains controller templates containing permission control, that is, background templates.

If your template has a customized front-end template, you need to create a directory with your module name in the phpcmstemplatesdefault directory to place the front-end template. "default" is the name of your style package. We use default by default .

【2】Create module controller class

In the previous step, we have created a module named test. Next, we will continue to add two controller classes to this module.

The controller of phpcms V9 is the class file of the module, located under the phpcms/modules/module name/ directory. The name of the class file is the controller name .php. For example, if a controller is named mytest, then it can be named mytest.php. The controller class inherits the system's function library by default and can be used directly.

The class name of the controller class and the controller file name must be the same .

Controller class files include two forms:

1. Front-end browsing (excluding permission control), mytest.php controller

is in the phpcms/modules/test directory Next, create a new text file, name it mytest, change the file type to php, open it with Notepad and edit the content as:

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

 1 <?php
 2     defined('IN_PHPCMS') or exit('No permission resources.');
 3     class mytest 
 4     {
 5         function __construct(){}
 6         public function init() 
 7         {
 8             $myvar = 'hello world!';
 9             echo $myvar;
10         }
11         public function mylist() 
12         {
13             $myvar = 'hello world! This is an example!';
14             echo $myvar;
15         }
16     }
17 ?>

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

Actually, the URL access method of this controller has been introduced before, please refer to "phpcms V9 MVC Mode and URL Access Analysis"

http://www.abcd.com.cn/phpcms/index.php?m=test&c=mytest is equivalent to

http://www.abcd.com.cn/phpcms/index.php?m=test&c=mytest&a=init.

When the "a" value is not filled in, the init method is called by default.

Why is this like this? Please read "phpcms V9 MVC Mode and URL Access Analysis" again.

2. Backend management (including permission control), mytest_admin.php controller

The background controller needs to load the admin class under the admin module and inherit this class. It should be noted that because the added controller class inherits other classes, be careful that the method name of the controller class is not the same as the method name in the class, otherwise it will cause effects. Please check the methods in the admin class for details.

In the phpcms/modules/test directory, create a new text file, name it mytest_admin, change the file type to php, open it with Notepad and edit the content as:

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

 1 <?php
 2     defined('IN_PHPCMS') or exit('No permission resources.');
 3     pc_base::load_app_class('admin','admin',0);
 4     class mytest_admin extends admin 
 5     {
 6         public function __construct() {}
 7         public function init() 
 8         {
 9             $myvar = 'oh,i am phpcmser';
10             echo $myvar;
11         }
12     }
13 ?>

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

Add template call in controller

phpcms can achieve complete separation of templates and programs, so templates must be loaded in our controller program so that they can be displayed more friendly.

1. Load the frontend template

The front-end template file is in the directory with the phpcmstemplatesdefault module name. This example is also in phpcmstemplatesdefaulttest.

The method to load the template is as follows:

1 // 加载模板方法:
2 include template('test', 'mytest', 'default');

Among them, test is the module name, mytest is the template name in the template directory, default is the style name, and the default is default.

In the above example, if you want to load a mytest template for the init method in mytest.php (you can copy index.html under the content module as an alternative), as follows (so the template name is index):

1 public function init() 
2 {
3     $myvar = 'hello world!';
4     echo $myvar;
5     include template('test', 'index');
6 }

At this time, when we access the method through the URL, the corresponding template will be loaded.

2. Load background template

The background template file is in the templates directory of the phpcmsmodules module name. This example is also in phpcmsmodulestesttemplates

The method to load the template is as follows:

// 加载模板方法:
include $this->admin_tpl('mytest_admin_list');

Where mytest_admin_list is mytest_admin_list.tpl.php in phpcmsmodulestesttemplates.

Note: The template here must have .tpl.php as the suffix

In the above example, if you want to load a mytest_admin_list template to the init method in mytest_admin.php, as follows:

1 public function init() 
2 {
3     $myvar = 'oh,i am phpcmser';
4     echo $myvar;
5     include $this->admin_tpl('mytest_admin_list');
6 }

For loading the template part, you can also see the implementation of the system framework source code content module phpcmsmodulescontent content.php file.

【3】Create database model class

At this point, it is clear that the database model of each module is located in the phpcms/model/ directory.

数据模型文件的命名规则建议为:数据表名称 + '_model.class.php'

如果在我们的创建的模块中我要使用一个数据库“test”,首先需要建立一个数据库模型文件,文件名称为'test_model.class.php'

内容如下:

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

 1 <?php
 2 defined('IN_PHPCMS') or exit('No permission resources.');
 3 pc_base::load_sys_class('model', '', 0);
 4 class test_model extends model
 5 {
 6     public function __construct() 
 7     {
 8         $this->db_config = pc_base::load_config('database');
 9         $this->db_setting = 'default';
10         $this->table_name = 'test';
11         parent::__construct();
12     }
13  }
14 ?>

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

书写数据库模型类注意一下几点:

1. 数据库模型类名称必须与文件名称相同。

2. 必须继承与数据库模型基类model。

3. $this->db_setting = 'default'为数据库配置文件中配置数据库链接池名称,默认为default,一般情况下不需要修改。

4. $this->table_name = 'test'为数据表名称。

这样我们就建立好一个数据库模型类。那么,怎么使用呢?

在模块的控制器中使用(加载方式):

$this->db = pc_base::load_model('test_model');

具体如下:

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

 1 db->select(); // 调用select方法
16             var_dump($result);
17         }
18         public function mylist()
19         {
20             $myvar = 'hello world! This is an example!';
21             echo $myvar;
22         }
23     }
24 ?>

phpcms V9 add module (transfer), phpcmsv9_PHP tutorial

其中$this->db中所支持的方法请参照父类 phpcms/libs/classes/model.class.php 中方法。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1137573.htmlTechArticlephpcms V9 添加模块(转),phpcmsv9 转自:http://www.cnblogs.com/Braveliu/p/5101345.html 为phpcms创建一个模块的开发流程 【1】创建模块目录 通过前面的学...
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