简体   繁体   English

如何构造PHP模块?

[英]How to structure PHP Modules?

I have a question for you. 我有一个问题问你。 I'm not sure how to structure a PHP Module for CMS maybe when I create a new Page I want to select the Module which are created, for example News Module, Gallery Module etc.. 我不确定如何为CMS构建PHP模块,也许是在创建新页面时想要选择创建的模块,例如新闻模块,图库模块等。

How can I structure this and implement those modules in PHP CMS ? 我如何构造它并在PHP CMS中实现那些模块?

In your database you should hold a modules table that consists of the following: 在数据库中,您应该拥有一个包含以下内容的模块表:

  • id ID
  • module_name MODULE_NAME
  • module_desc module_desc
  • module_folder module_folder
  • module_active module_active

so that you can keep modules organized, in the table where you have module_folder this should be the location of the module such as 为了使模块保持井井有条,在具有module_folder的表中,这应该是模块的位置,例如

cms_root() . "/modules/%module_folder%/main.module.php"

This is where interfaces would come in handy :) 这是接口派上用场的地方:)

interface IModule
{
    public function __run($param);
    public function __version();
    public function __shutdown();
    //...
}

you should also have a class called Module where the module would extend and gather rights to templates/database ect. 您还应该有一个名为Module的类,其中该模块将扩展并收集对模板/数据库等的权限。

class Module
{
    public $DB,$TPL; /*...*/

     /*
         Functions ehre to help the module gain access to the instance of the page.
     */
}

Also the Module class should be able to execute modules and keep track of executed modules, so in your core code you can say $Module->RunAll() and it would run them. 另外,Module类应该能够执行模块并跟踪已执行的模块,因此在您的核心代码中可以说$Module->RunAll()并将其运行。

A module file would probably look like: 一个模块文件可能看起来像:

class Gallery_Module extends Module implements IModule
{
    public function __version()
    {
         return '1.0';
    }

    public function __run()
    {
         //Assign module data to the template :)
    }

    public function __shutdown()
    {
         //Clean old records etc from DB
    }
}

And within your core as said above, you can get the Module class to read active modules from the database load the files from the folder and create an instance of the class, followed by there execution. 在如上所述的核心内,您可以获取Module类以从数据库中读取活动模块,然后从文件夹中加载文件并创建该类的实例,然后在其中执行。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM