Chris
Chris

Reputation: 11

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..

How can I structure this and implement those modules in PHP CMS ?

Upvotes: 1

Views: 1852

Answers (1)

RobertPitt
RobertPitt

Reputation: 57268

In your database you should hold a modules table that consists of the following:

  • id
  • module_name
  • module_desc
  • module_folder
  • 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

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.

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.

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.

Hope this helps.

Upvotes: 3

Related Questions