search
HomeBackend DevelopmentPHP Tutorialphp笔记之:初探PHPcms模块开发介绍_php实例

由于工作关系,只能暂时放弃对mongodb的研究了 .开始研究PHPcms .

目前为止我已经基本完成了模块的开发.趁着周末来这里做个总结.我发现phpcms写的还不错,不过文档什么的确实不多.

不说废话了.对于phpcms的模块开发.首先要了解模块的目录结构.

我们可以在http://v9.help.phpcms.cn/html/2010/structure_0928/69.html

找到他的目录结构   我们要开发的东东(也就是模块)就在/phpcms/modules/下面

如果没有什么特别的 在开发一个模块之前先要按照目录结构建立好相关目录并且设计好数据库表结构 比方说 我们建立一个模块叫做我的模块 my_test

下面应该是mytest下的目录结构


mytest

  --class //这个是mytest模块会用到的类

  --function//mytest模块用到的函数

  --install//安装此模块需要的一些配置文件和建立数据表myslq语句什么的在这里

    --language//多语言的时候会用到

    --config.ini.php//这个配置文件是用来描述整个模块的一些信息

    --extention.inc.php//这个是创建目录结构  .这个文件也用来控制权限

    --model.php//模块使用了哪些数据模型.(可以理解为使用了哪些表.)

    --model.sql//这个向数据库里面插入模型的记录

    --my_test.sql//这个文件在安装的时候会被执行,把建立数据库表的sql放进来

  --templates //,mytest模块用到的模板文件

  --uninstall //卸载模块时候用到的配置和文件

    这个里面的文件我没研究  回头研究了补上.

my_test.php //这个是mytest模块的后台控制器文件`

index.php//这个是前台的控制器,这个我没写东西.


 

 

建立完一个这样的结构后 我们还需要在/phpcms/model/下面建立我们的数据模型

例如  my_test_model.class.php  (这个使用了很典型的工厂模式)

具体每个文件里面写了些什么.我们一个一个来看 .首先来看我们在model文件夹下面写的那个文件.

复制代码 代码如下:

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class my_test_model extends model {
    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';//默认的数据库配置.//多个库的话可以在这里选库
        $this->table_name = 'my_test';//这个就是表名称,不用加表前缀
        parent::__construct();
    }
}
?>    

第一行的作用是确定是不是在phpcms的运行框架内.

第二行加载系统的model类,后面的参数0 的意思是并不实例化.

最后一行调用了父类的构造方法.可以在phpcms/libs/classes/model.class.php中找到

而这个model类里面定义了很多对数据的操作方法   最基本的增删改查。以后我再详细说说model基本的一些方法。

接着来看看modules   里面的东东

我们一个个往下面看  第一个language   是用来支持多语言菜单的。

然后是config.ini.php,这个里面写的是一些关于模块安装时候的信息。

文件里面是这个结构的

复制代码 代码如下:

$module = 'mytest';//使用的model
$modulename = '这里是模块的名称';
$introduce = '模块的描述信息';
$author = '作者';
$authorsite = '作者网站';
$authoremail = '作者email';

里面标注的很清楚了

接着是extention.inc.php 这个文件是用来创建后台管理菜单的目录结构的,也是用来控制权限的 

复制代码 代码如下:

$id= $menu_db->insert(array('name'=>'这里写着操作名称',      'parentid'=>父ID, 'm'=>'模块', 'c'=>'控制器', 'a'=>'动作',      'data'=>'', 'listorder'=>排序, 'display'=>'是否显示'),true);//最后的true是用来返回ID的

文件最后应该有一个数组,这个数组是用来插入系统的\language\zh-cn\system_menu.lang.php里面的  格式如下
复制代码 代码如下:

$language = array(
    '这里是你起的操作名称'=>'这里是操作的中文翻译',
    类似:'mytest_init'=>'显示列表'
    );

然后是model.php  这个就是你使用了哪些数据模型 可以理解为使用了哪些表
复制代码 代码如下:

return array('mytest','my_test_artcle');

然后是model.sql   这个是用来向系统的模型表里面插入数据用的
复制代码 代码如下:

INSERT INTO `phpcms_module` (`module`, `name`, `url`, `iscore`, `version`, `description`, `setting`, `listorder`, `disabled`, `installdate`, `updatedate`) VALUES ();

然后是mytest.sql 建立你数据库表的语句应该写在这个文件里面

接着就是你所使用的模板  应该放在templates里面  命名的规则应该是   mytest_add.tpl.php

最后是你的控制器   这个有的研究了.控制器里面是针对你每个url传递过来的action也就是a=?的动作.默认动作是init

复制代码 代码如下:

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class mytest extends admin(){
  public function __construct(){
    parent::__construct;//调用父类的构造函数
  }
  public function init(){
    echo "这里是默认的操作方法";
  }
  public function add(){
    include $this->admin_tpl('mytest_add');//使用模板的方法
  }
}

控制器里面写好了   我们把上面的文件都写完了就可以安装我们的模块了 。
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools