Phpcms V9 Add Module (GO)

Source: Internet
Author: User

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

Create a module development process for PHPCMS

"1" Creating the module directory

With the previous study, we already know that the modules in the PHPCMS V9 framework are in the Phcms/modules directory, and each directory is called a module.

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

For example, to develop a module called test, first create a folder in the Phpcms/modules directory and name it test.

Observing the structure of other modules, it is common to see the standard structure of the test module:

Classes as a Module class library package

functions as a module function library package

Templates is a module template package that typically places a controller template with permission controls, which is a background template.

If your template has a custom foreground template, you will need to create a directory of your module name in the Phpcms\templates\default directory to place the foreground template, "default" for your style package name, and we default to use defaults.

"2" Creating the Module controller class

In the previous step, we have created a module named Test, and then we continue to add two controller classes for this module.

The controller of the Phpcms V9 is the class file of the module, located under the Phpcms/modules/module name/directory. The class file name is the controller name +.php, such as a controller named MyTest, then it is named Mytest.php. The controller class inherits the system's library of functions by default and can be used directly.

The class name of the controller class must be the same as the controller filename .

The Controller class file consists of two forms:

1. Front view (without permission control), mytest.php Controller

In the Phpcms/modules/test directory, create a new text file named MyTest, modify the file type to PHP, and open the edited content with notepad++:

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< C11/>}11 Public         Function MyList ()         {             $myvar = ' Hello world! This was an example! ';             Echo $myvar,         }16     }17?>

In fact, this controller has been described in the URL access method, see "Phpcms V9 MVC pattern and URL Access Resolution"

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.

The Init method is called by default without filling in the "a" value.

Why is it like this? Please read the Phpcms V9 MVC pattern and URL access parsing again.

2. Background management (including rights control), mytest_admin.php Controller

The background controller needs to load the Admin class under the admin module and inherit the class. It is important to note that because the Controller class that is added 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 be affected, see what methods are in the Admin class.

In the Phpcms/modules/test directory, create a new text file named Mytest_admin, modify the file type to PHP, and open the edited content with notepad++:

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 () C7/>8         {9             $myvar = ' oh,i am phpcmser ';             echo $myvar;         }12     }13?>

Adding template calls to the controller

Phpcms can achieve a complete separation of the template and the program, so in our controller program to load the template, can be more friendly display.

1. Loading the foreground template

The foreground template file is in the directory of the Phpcms\templates\default\ module name, and this example is also in Phpcms\templates\default\test.

Here's how to load the template:

1//Load Template method: 2 include template (' Test ', ' mytest ', ' Default ');

Where test is the module name, MyTest is the template name under the template directory, default is the style name, and defaults to defaulted.

In the example above, if you want to load a mytest template for the Init method in mytest.php (you can copy the 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 point, the corresponding template is loaded when we then access the method through the URL.

2. Loading the background template

The background template file is in the Phpcms\modules\ module name \templates directory, and This example is also in Phpcms\modules\test\templates

Here's how to load the template:

Load Template method: include $this->admin_tpl (' mytest_admin_list ');

Where Mytest_admin_list is mytest_admin_list.tpl.php in Phpcms\modules\test\templates.

Note: here the template must be suffixed with. tpl.php

In the example above, if you want to load a mytest_admin_list template for the Init method in mytest_admin.php, the following:

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

Loading the template part of the content can also be found in the System Framework source content module Phpcms\modules\content content.php file implementation.

"3" Creating a Database model class

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

The recommended naming rules for data model files are: Data table name + ' _model.class.php '

If in our created module I want to use a database "test", first need to establish a database model file, the file name is called ' test_model.class.php '

The contents are as follows:

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 ',         $this->table_name = ' test ';         parent::__construct ();  }14?>

Write Database model Classes notice the points:

1. The database model class name must be the same as the file name.

2. Must inherit with database model base class.

3. $this->db_setting = ' Default ' to configure the database link pool name in the database configuration file, default defaults, no modification is normally required.

4. $this->table_name = ' Test ' is the data table name.

In this way, we build the good one database model class. So, how to use it?

Used in the controller of the module (load mode):

$this->db = Pc_base::load_model (' Test_model ');

Specific as follows:

1 <?php 2     defined (' In_phpcms ') or exit (' No permission resources. '); 3     class MyTest  4     {5         private $db; 6         function __construct () 7         {8             $this->db = Pc_base::load_model (' Test_model ');//LOAD Database Model 9         }10         Public Function init () One         {             $myvar = ' Hello world! ';             echo $myvar;             include template (' Test ', ' Index '),             $result = $this->db->select (); Call the Select method             var_dump ($result),         }18 public         function MyList ().         {             $myvar = ' Hello World ' ! This was an example! ';             Echo $myvar,         }23     }24?>

The methods supported in $this->db refer to the methods in the parent class phpcms/libs/classes/model.class.php.

Phpcms V9 Add Module (GO)

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.