Home Backend Development PHP Tutorial php笔记之:初探PHPcms模块开发介绍_php实例

php笔记之:初探PHPcms模块开发介绍_php实例

Jun 07, 2016 pm 05:24 PM

由于工作关系,只能暂时放弃对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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles