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

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

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

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.

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
