Creating a Simple Module
For our example here,??the first thing we will do is create a simple module. Our first module will be called hello.mod. So initially we will create a couple of directories. First, we will create a module directory (we’ll call it modules), under that we will create a directory called hello.mod to keep out module pieces. Finally, within our hello.mod directory we will create our main module file named module.php.
Our module.php file is going to simply contain the following:
???????? echo "Hello, I'm a module</br>";
A few things to note: since simplicity is key here, there need to be a few rules for consistency.?? First of all, all module packages (i.e. folders containing the module code) need to end with “.mod” (you could use any other extension if you wanted, but you’ll have to change some code below, and whatever you choose, you need to stick with it). Second, all module packages need to be kept in the same modules directory. Finally, each module must have a primary file (in this case module.php) and each module must name this file identically (this file acts just like an index.html file in a way).?? Now each one of these rules can be broken, but doing so will require you to add new code to this example and add complexity to the project as a whole.
A quick look at the directory structure of our example is as follows:
./ |_ modules/ ?????????? |_?? hello.mod/ ???????????????????????????? |_ module.php
Any additional modules can be dropped in. For example let’s add helloagian.mod and in that moudule.php file we’ll??type:
?????? echo "Hello again... I'm a module too!</br>";
Now, our directory structure will look like:
./ |_ modules/ ?????????? |_?? hello.mod/ ?????????? |???????????? |_ module.php ?????????? | ?????????? |_?? helloagain.mod/ ?????????????????????????? |_ module.php
Creating the Module Sidebar
The code for this is very easy, though for use in a real web page you’ll want to add styles and perhaps some other things (we’ll get to a few later). What we are going to start with is simply:
???????? $mod_path = "./modules/"; ???????? $mod_index_file = "module.php"; ???????? include_once('File/Find.php'); ???????? $modpkg = File_Find::glob(".mod", $mod_path); ???????? foreach ($modpkg as $module) { include("$mod_path/$module/$mod_index_file"); ???????? }
There should only need to be a few points of explanation needed here. First of all a quick look at some of the initial variables.
Once we define those variables we include out File_Find class from which we will use the glob() function. The glob() function will search for any module package (by searching for the .mod extension) in our $mod_path . Each package it finds it will add to the $modpkg array.
Finally, we will loop through our $modpkg array, and for each $module we will include the $mod_index_file and thus each module will get added to our module bar.
Solving a Few Immediately Noticeable Issues
Ok, so the code works, and now we have a simple way including modules. There are two immediate issues which we will want to solve: style and load order.
The style issue is easily solved by changing our module code as follows:
?????? $mod_path = "./modules/"; ???????? $mod_index_file = "module.php"; ???????? include_once('File/Find.php'); ???????? $modpkg = File_Find::glob(".mod", $mod_path); ???????? foreach ($modpkg as $module) { ???????? print "" include("$mod_path/$module/$mod_index_file"); print "" ???????? }
This will allow you to attach a style for .module for all modules. Certain modules may require special unique styling too. This, however, should likely be done within the module code.
The load order of our modules is a slightly bigger problem. Right now there is no guarantee that the modules will load in the order you want. Keeping with our simplicity theme, we can easily sort our modules with PHP’s sort() function. To do this we once again add to our module code which will end up looking like:
???????? $mod_path = "./modules/"; ???????? $mod_index_file = "module.php"; ???????? include_once('File/Find.php'); ???????? $modpkg = File_Find::glob(".mod", $mod_path); ???????????????????? sort($modpkg); ?????????? // sort our modules // to control load order ???????? foreach ($modpkg as $module) { ???????? print "" include("$mod_path/$module/$mod_index_file"); print "" ???????? }
Now we need to alter our module names so that they sort in the proper order. An easy way to do this is to barrow number our modules so that hello.mod becomes 010_hello.mod and helloagain.mod becomes 020_helloagian.mod. Those of you who have learned good old fashioned Basic (QBasic, AppleBasic…etc.) will note the familiar numbering. By leaving gaps in the numbering scheme you can easily slide a new module in between two existing ones without needing to change the existing modules. So, if I wanted to later add a module to load between the “hello” and “helloagain” modules I can easily name it 015_inbetween.mod.
What’s next?
Well, clearly the next thing is to create some real, useful modules, but that’s another article. There are also a number of ways to take the basic ideas here and alter or improve upon them. PHP is incredibly flexible in that way. But until next time… enjoy.