Got a question?  Ask a developer in our Developer's Corner Forum

Back To The Developer's Corner Main Page

Joomla! Component Tutorial - Generate Menu XML

A user (krest) on the Joomla! forums wanted to know how to get information about the Joomla! menu on his site so he could use it in a Flash menu application.  To do this, I suggested creating a very simple component and figured I'd walk through my process of creating it here for anyone else that is interested in doing something similar.

 

First, we create a directory to hold our files.  I am going to call mine menuxml (which will also be the name of my component).  Next, we need to create an xml install file.  This file is used when you install the component to tell Joomla! what to do.  I will only be populating some of the information, but you can find more information on help.joomla.org.  I named my xml install file menuxml.xml (same as my component) and paste the following into it:

<?xml version="1.0" encoding="utf-8"?>

<install type="component" version="1.5.0">

<name>menuxml</name>

<creationDate>2009/11/11</creationDate>

<author>CMS Market</author>

<authorEmail>will[at]cmsmarket.com</authorEmail>

<authorUrl>http://www.cmsmarket.com</authorUrl>

<copyright>Copyright (C) 2009 CMS Market. All rights reserved.</copyright>

<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>

<version>1.0</version>

<description>This component will query the database for the given menu and return XML with all the information about the menu</description>

<files folder="component">

<filename>index.html</filename>

<filename>menuxml.php</filename>

</files>

<administration>

</administration>

</install>

 

You will notice in the xml install file that we list all the files included in our package.  We also specify the folder where these files live in our package (in my case, component).  Most Joomla! components have a backend administrator side and a frontend user side.  Files for the backend will end up being copies to .../administrator/components/com_menuxml/ and files for the front end will end up being copied to .../components/com_menuxml/.  In my example, I don't include any backend files.  To make development easier, I typically keep the files in separate folders (admin and component) in my install package as well.

 

Before we move on, you will notice an index.html file in the package.  This is more of a protection measure.  It is not required, but it is good practice.  If a folder on your site has an index.html file in it, that file will be served to the user if they specify a direct URL to the path.  Basically, just throw an index.html file with the following in every folder you create:

<html><body bgcolor="#FFFFFF"></body></html>

 

On to the files.  In your menuxml folder, create a component folder.  Drop in the index.html file (remember to do this in all your folders).  Then create a file called menuxml.php and paste the following code into it:

<?php

// Check to ensure this file is included in Joomla!

defined('_JEXEC') or die('Restricted access');

$dbo = &JFactory::getDBO();

$menutype = JRequest::getVar("menutype");

if($menutype)

{

$query = "SELECT * ".

"FROM #__menu ".

"WHERE menutype = ".$dbo->quote($menutype);

$dbo->setQuery($query);

$menuitems = $dbo->loadObjectList();

print "<menu>\n";

foreach($menuitems as $menuitem)

{

print "<menuitem name=\"".$menuitem->name."\" link=\"".$menuitem->link."\" />\n";

}

print "</menu>\n";

}

?>


This file is the main entry point for the front end of your component.  When you call to Joomla! and tell it to execute a component (by passing the option=com_menuxml query string parameter), this is the entry point file.

 

A few things to note about this file:

  • The following line is also a security feature and should be added to the top of every php file you create.  It will ensure that no one can call your script directly (to try to hack your site) without going through the Joomla! framework:
    defined('_JEXEC') or die('Restricted access');
  • We get our database object from the JFactory class.  This class is a great entry point to get most of the info you need from Joomla!:
    $dbo = &JFactory::getDBO();
  • To allow the user to specify which menutype they want info for, we get the value from the menutype query string parameter using this call:
    $menutype = JRequest::getVar("menutype");
  • When creating SQL queries, it's a good idea to quote anything variable.  This is another security best practice.  In our example, we quote the $menutype variable before appending it to our query string:
    "WHERE menutype = ".$dbo->quote($menutype);
  • In my example, I am asking JDatabase for an Object List.  The JDatabase class supports many load functions that return different types:
    $menuitems = $dbo->loadObjectList();
  • The rest is just looping over my results and printing out the xml output.

 

Once you have the component installed, you can call it by going to this URL on your site:

http://www.YOURDOMAIN.com/index.php?option=com_menuxml

 

For our example, you will want to pass in the menutype you want returned as well as format=xml to let Joomla! know not to render the template and all the associated HTML:

http://www.YOURDOMAIN.com/index.php?option=com_menuxml&menutype=mainmenu&format=xml


If you call this URL in a browser, be sure to view the source because the browser will not render the XML properly.

 

And that about does it.  If you want to get the package, you can download it here.  Get it, check it out, and play with it.  At this point, you just need to change the PHP to do what you want.

 

 

Part of the SourceCoast Network:

SourceCoast / Cooking Allergy Free / CMS Market

© 2010 CMS Market. All rights reserved.

Joomla Facebook integration by JFBConnect

GTranslate Joomla Module