|
Articles -
Mambo
|
|
Page 2 of 7
Frontend files
Files in this group are used to display a component to website visitors. Meaning if you link a component to a menu item (using Menu -> Main Menu -> New -> Component), these files control what people see when they click on that menu (and subsequently to any links or buttons displayed by your component).
All front end files must have the following line to prevent being loaded directly in the browser.
<?php
/** ensure this file is being included by a parent file */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
?>
mycomp.php
This is the default file loaded by Mambo to display a component. You can have something as simple as displaying data from text file or database, or a switch statement that calls different functions according to what user choose.
A simple switch statement can look like this:
<?php
switch($task) {
case 'show_form':
// Display a form
break;
case 'save':
// Save data from the submitted form
break;
default:
// Perhaps display some data, etc
break;
}
?>
Where does $task variable come from? You need to define it in your form (as hidden variable) or include it in links so it can be sent to the server. You can use any variable name but 'task' is kind of the standard used in most of the components. Other variables that you need to pass to the server are $option (directory name of your component, e.g. com_mycomp) and $Itemid (menu ID of your component). Variable $Itemid is used to generate pathway and highlight the active menu. If you remove it from the URL, the page is still loading correctly but none of the menu is highlighted and pathway is not displayed correctly.
In this page you can also use $mainframe and $database objects to get configuration values, get information of the logged in user, run queries, etc.
mycomp.html.php (optional)
This is where you should put GUI classes and functions. Of course you can dump everything in mycomp.php file above but it is better to have them here.
Mambo's common convention used for class name is HTML_mycomp. There is no need to create a constructor function because we won't create an instance for this class.
<?php
class HTML_mycomp {
function displayForm() {
// Print form here
}
function displayData($db_result) {
// Print query result in a nice table, etc
}
}
?>
All forms must be submitted to index.php. You can use sefRelToAbs() function to convert a URL into search engine friendly form.
<?php
echo '<form method="POST" action="'. sefRelToAbs("index.php?option=$option&Itemid=$Itemid").'">';
?>
So how do we link these files? $mainframe object provides function called getPath() to do this easily. Just add the following line to mycomp.php.
<?php
// Load the HTML class
require_once($mainframe->getPath('front_html'));
?>
The string 'front_html' is Mambo predefined key to load mycomp.html.php file. There are other keys available which I’ll explain later. For the complete list of getPath keys and where Mambo looks for the files, please refer to Appendix B.
Now we can call the above functions from mycomp.php as follows:
<?php
HTML_mycomp::displayForm();
HTML_mycomp::displayResult();
?>
|