The Basic Structure


All PHP-Nuke modules are loaded by a file called modules.php, which lives in the root of your Web site. Visit any page on your site (other than administrative pages), and you'll see that the URL of each page begins with something like www.mysite.com/modules.php?name=ModuleName. ModuleName always corresponds to a specific folder in the modules folder of your site: www.mysite.com/modules.php?name=Sections refers to modules/Sections, while www.mysite.com/modules.php?name=Forums refers to modules/ Forums, and so forth. When you access a module, it's the modules.php page that actually loads; it then runs out and grabs the index.php file from the appropriate module folder. Note that module names are case sensitive: If the module's folder name is Sections, it must be referred to as www.mysite.com/modules.php?name=Sections, not www.mysite.com/modules.php?name=sections.

So the actual guts of any module lives in a file named index.php, within the folder named after that specific module. PHP-Nuke ships with a module named Addon_Sample, which contains an index.php file, a file named f2.php, and a file named copyright.php. The index.php file contains the following code:

[View full width]

<?php if (!eregi("modules.php", $_SERVER['PHP_SELF'])) { die ("You can't access this file directly..."); } $module_name = basename(dirname(__FILE__)); $index = 1; function one() { global $module_name; include("header.php"); OpenTable(); echo "Addon Sample File (index.php) function \"one\"<br><br>"; echo "<ul>"; echo "<li><a href=\"modules.php?name=$module_name&amp;file=index\">Go to index.php</a>"; echo "</ul>"; CloseTable(); include("footer.php"); } function two() { global $module_name; include("header.php"); OpenTable(); echo "Addon Sample File (index.php) function \"two\""; echo "<ul>"; echo "<li><a href=\"modules.php?name=$module_name&amp;file=index\">Go to index.php</a>"; echo "</ul>"; CloseTable(); include("footer.php"); } function AddonSample() { global $module_name; include("header.php"); OpenTable(); echo "Addon Sample File (index.php)<br><br>"; echo "<ul>"; echo "<li> <a href=\"modules.php?name=$module_name&amp;file=index&amp;func=one\">Function One</a>"; echo "<li><a href=\"modules.php?name=$module_name&amp;file=index&amp;func=two \">Function Two</a>"; echo "<li><a href=\"modules.php?name=$module_name&amp;file=f2\">Call to file f2.php</a>"; echo "</ul>"; echo "You can now use Administration interface to activate or deactivate any module. As an Admin you can always " ."access to your Inactive modules for testing purposes."; CloseTable(); include("footer.php"); } switch($func) { default: AddonSample(); break; case "one": one(); break; case "two": two(); break; } ?>

Let me break that down a bit:

  • The page starts with a short if statement that checks to see if the module's index.php page is being accessed through modules.php, as is proper, or if it's being accessed directly. Modules that are accessed directly display "You can't access this file directly" and then quit processing.

  • The variable $module_name is set to the folder name that the index.php file lives in. In this case, it is Addon_Sample since that's the name of the folder containing this file.

  • Three functions are listed: one(), two(), and AddonSample(). These functions do not do anything (meaning that they will not execute) unless they are explicitly called. So, the first time running through this file, PHP-Nuke skips all three functions.

  • Last is a switch construct, which decides what to do next. There are three possibilities:

    • The page is accessed with the URL modules.php?name= Addon_Sample&func=one, in which case $func contains one. The select construct matches that to case one and executes the one() function.

    • The page is accessed with the URL modules.php?name= Addon_Sample&func=two, in which case, $func contains two. The select matches that to case two and executes the two() function.

    • The page is accessed with some other URL, which might not include a &func= portion at all. In this case, the select construct will not be able to match either of the existing case conditions; it instead uses the default condition and executes AddonSample().

It's this last select construct that decides which of the three functionsone(),two() , or AddonSample()to execute.

In many other PHP-Nuke modules, you'll see the switch construct at the bottom using $op instead of $func; you'll also notice that most PHP-Nuke module URLs contain something like modules.php?name=modulename&op=something.



    PHP-Nuke Garage
    PHP-Nuke Garage
    ISBN: 0131855166
    EAN: 2147483647
    Year: 2006
    Pages: 235
    Authors: Don Jones

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net