phpcms V9 add module (transfer), phpcmsv9_PHP tutorial
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:
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 ?>
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:
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 ?>
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'
内容如下:
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 ?>
书写数据库模型类注意一下几点:
1. 数据库模型类名称必须与文件名称相同。
2. 必须继承与数据库模型基类model。
3. $this->db_setting = 'default'为数据库配置文件中配置数据库链接池名称,默认为default,一般情况下不需要修改。
4. $this->table_name = 'test'为数据表名称。
这样我们就建立好一个数据库模型类。那么,怎么使用呢?
在模块的控制器中使用(加载方式):
$this->db = pc_base::load_model('test_model');
具体如下:
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 ?>
其中$this->db中所支持的方法请参照父类 phpcms/libs/classes/model.class.php 中方法。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to jump to the details page in phpcms: 1. Use the header function to generate a jump link; 2. Loop through the content list; 3. Get the title and details page link of the content; 4. Generate a jump link.

PHP CMS is a PHP-based open source content management system for managing website content. Its features include ease of use, powerful functionality, scalability, high security, and free open source. It can save time, improve website quality, enhance collaboration and reduce development costs, and is widely used in various websites such as news websites, blogs, corporate websites, e-commerce websites and community forums.

Title: WeChat Login Integration Guide: PHPCMS in Action In today’s Internet era, social login has become one of the essential functions of a website. As one of the most popular social platforms in China, WeChat’s login function is also used by more and more websites. This article will introduce how to integrate the WeChat login function in the PHPCMS website and provide specific code examples. Step 1: Register a WeChat Open Platform Account First, we need to register a developer account on the WeChat Open Platform and apply for the corresponding development permissions. Log in [WeChat open platform]

phpcms is not completely free. phpcms is an open source cms system, but open source does not mean free. It has two versions: free version and commercial version. The free version is limited to personal non-commercial use, while the commercial version requires purchasing a license; individuals can use it for research, and if it is commercial application , you need to pay a certain fee.

PHPCMS is a free and open source content management system (CMS) that features: open source, modularity, flexibility, user-friendliness and community support. It can be used to create various types of websites, including corporate websites, e-commerce websites, blogs, and community forums. Technical requirements include: PHP 5.6 or higher, MySQL, MariaDB or PostgreSQL database, and Apache or Nginx web server.

There are two well-known versions of phpcms, namely: 1. phpCMS4, which supports custom URL rules. The website management background is beautiful and easy to use, and has many front-end plug-ins, which can freely expand functions; 2. phpCMS2008R1, which supports multi-language, multi-site management, and page The manager is convenient, flexible, very lightweight, and runs fast.

phpcms uses mysql database. phpcms is a PHP open source website management system, developed using PHP+MYSQL as the technical basis. PHPCMS V9 adopts OOP method to build the basic operating framework. The supported PHP version is PHP5 and above, and the supported MYSQL version is MySql 4.1 and above.

PHPCMS user name security setting strategy revealed In website development, user account security has always been an aspect that developers attach great importance to. The security settings of the username are also crucial, because the username is not only the user's login credentials, but may also expose the user's personal information and even cause security risks. This article will reveal the username security setting strategy in PHPCMS and give specific code examples for developers to refer to. 1. Prevent common usernames. In order to improve the security of usernames, developers should prevent users from using excessive
