Article

eZ publish: PHP's Killer App - Parts 1-3

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Next

Plugging In

So how do we use our classes with eZ publish and where does the creation of XHTML pages come in?

In part 2 of this series, we had a good look at the eZ publish directory structure. For our eZFaq module we need to recreate that structure, so here's what we'll need to create below our [eZPublish_root] directory:

919_ezfaqDirectory

The ZIP at the end of this article contains this structure, so you should be able unzip it directly into your [eZPublish_root].

The next thing we need to do is tell eZ publish that it has a new module, by updating site.ini (remember ours is called site.ini.php as a result of the way we installed eZ publish in part 1). We need to change this line:

EnabledModules=eZArticle;eZFaq;eZImageCatalogue;eZMediaCatalogue;                    
eZForm;eZStats;eZURLTranslator;eZSiteManager;eZUser

Then we need to add a section for eZFaq lower down to tell eZ publish about any "module specific" variables:

[eZFaqMain]                    
AdminTemplateDir=templates/standard/                    
TemplateDir=templates/phppoint/                    
Language=en_GB                    
DefaultSection=1

Note that with "TemplateDir" we're pointing eZ publish at the directory that contains our user templates.

Don't forget to clear your cache after making the change!

Our next move is to put together the user interface that will be presented to phpPoint visitors.

The Data Supplier

In the [eZ publish_root]/ezfaq/user directory, we need to create a file called datasupplier.php. The eZ publish framework "looks" for this file and passes it information about the current "state" of eZ publish (i.e. what a user is doing). The data supplier then acts on that information to call our logic classes then generate the right user interface (actually by including some other PHP scripts). Here's the data supplier for eZ faq:

<?php                    
// Decides which "event" to perform based on the URL                    
switch ( $url_array[2] ) {                    
   case "faqview":                    
       // Default to first category                    
       if ( !$url_array[3] )                    
           $categoryID=1;                    
       else                    
           $categoryID=$url_array[3];                    
                   
       // Default to first heading                    
       if ( !$url_array[4] )                    
           $headingID=1;                    
       else                    
           $headingID=$url_array[4];                    
                   
       // Default to first faq                    
       if ( !$url_array[5] )                    
           $faqID=1;                    
       else                    
           $faqID=$url_array[5];                    
                   
       include( "ezfaq/user/faqview.php" );                    
   break;                    
                   
   case "faqlist":                    
       // Default to first category                    
       if ( !$url_array[3] )                    
           $categoryID=1;                    
       else                    
           $categoryID=$url_array[3];                    
                   
       include( "ezfaq/user/faqlist.php" );                    
   break;                    
                   
   case "faqsearch":                    
       include( "ezfaq/user/faqsearch.php" );                    
   break;                    
                   
   default :                    
       // go to default module page or show an error message                    
       print( "Error: your page request was not found" );                    
}                    
                   
?>

The important variable here is $url_array. eZ publish hands the data supplier this variable using a switch statement, and the data supplier includes the relevant PHP script to handle presentation of the user interface. The $url_array variable is an array that's populated using the URI of the current page the user is looking at. This is easiest to see with an example:

http://localhost/public/ezpublish_2_2_6/index.php/faq/faqlist/

Here $url_array[1] is the eZ publish module name (i.e. "faq" for ezFaq).

$url_array[2] will contain the value "faqlist" -- this is the one our data supplier will respond to.

We can put further variables into the URL, and eZ publish will provide these to the data supplier as well.

For example:

http://localhost/public/ezpublish_2_2_6/index.php/faq/faqview/1/2/4/

This gives:

$url_array[1]="faq";                    
$url_array[2]="faqview";                    
$url_array[3]="1";                    
$url_array[4]="2";                    
$url_array[5]="4";

The last three might refer to a category ID, a heading ID and a FAQ ID, respectively. That should give you a big clue as to how our module will do things like displaying an individual FAQ.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links