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 中方法。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function