Thursday, July 9, 2009

Joomla component in Zend Framework example

Joomla is a nice CMS(content management system) and Zend Framework is once of the famous and widely adopted MVC Framework since its first release. One of the nice thing about Zend is its loosely coupled components.

Keep in mind that component in joomla and Zend Framework don’t refer to the same concept. Those who have worked in Joomla and Zend Framework know the difference.

In this article I’m not going to discuss what Zend and Joomla are all about and how component is different in both, but instead I’m going to share a little secret of how to develop joomla component in Zend Framework.

This article is not for those who are unaware of joomla and Zend.

Although joomla has own API for developing its components and modules, but you can use any php code in addition to its own API.

Reason is simple. Joomla is developed in php.

As Zend also used php behind the scene, so both can interact easily.

This was a bit of introduction.

While discussing all these things I assumed that you have little knowledge of joomla and have some knowledge of Zend Framework as well.

So let’s dig in.

Before writing any code you will need to successfully download and install joomla. Also download Zend and save it in the directory of your choice.

Once successfully installed joomla, open joomla/administrator/components and create a folder called com_yourcomp.

This is your component folder. You will need to create a file named yourcomp.php

If you want to add component in your administrator, the name should be admin.yourcomp.php.

That’s it, you have now created a the necessary directory structure for your joomla component development.

If you write a line “Hello world!” in youcomp.php or admin.yourcomp.php and browse your page as

http://localhost/joomla/index.php?option=com_yourcomp

you will see

“Hello world” printed.

These are the minimum requirements for developing joomla component.

Now we will using Zend Framework classes to add code to this file plus we will create Zend directory structure in order to work with Zend MVC.

Create following directory structure in your joomla/administrator/components.


Here you can see that my component name is com_advertisers.

In this directory I’ve

* admin.advertiser.php file
* application directory
* Joomla directory

The file admin.advertiser.php will serve as the bootstrap file.

Application directory contain specific directory for controllers, models and views.

The most important role is played by our Joomla directory in developing joomla component in Zend Framework.

This directory contain Controllers/Plugins/Router.php file.

This file contains code for calling specific action based on particular task. Before going to discuss code in the Router.php file, I’m going to show the code need in our admin.advertisers.php file.
<?php
define('ROOT_DIR', dirname(__FILE__));
set_include_path('.'
. PATH_SEPARATOR . ROOT_DIR . '/'
. PATH_SEPARATOR . ROOT_DIR . '/application'
. PATH_SEPARATOR . ROOT_DIR . '/application/models'
. PATH_SEPARATOR . get_include_path()
);

require_once "Zend/Controller/Front.php";
require_once "Joomla/Controllers/Plugins/Router.php";

$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);

$frontController->registerPlugin(new Joomla_Controllers_Plugins_Router());
$frontController->setControllerDirectory(ROOT_DIR.'/application/controllers');

$frontController->dispatch();

In the first few lines I have included path to our com_advertisers directory, application directory and models directory.

Next I include Zend Front Controller files and Router.php file. I’ll discuss code in the Router.php file later as it contain the most important code.

Next I initialize front controller, register Router Plugin class which is defined in our Router.php file, set controller directory and call dispatch.

That’s it. We have now defined our bootstrap file.

One important thing is setting path to our library files that contain Zend component.

You can include path using set_include_path or set it in your php.ini file.

If you don’t include this path, your application will not work as you expected.

Now go to controllers directory and create IndexController.php and write the following code in it.
<?php

require_once('Zend/Controller/Action.php');
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
}
public function editAction()
{
}
public function saveAction()
{
}
}

Noting special here. I’ve created only three actions.

Go to your application/views/scripts directory and create directory called index and create three files index.phtml, edit.phtml and save.phtml.

The code above is what you will need to work with Zend Framework MVC. In order to integrate this code with joomla will need one additional files. In my case the file is Router.php

I have create this file in com_advertiser/Joomla/Controllers/Plugins/ directory.

The code contain in this file is
<?php

require_once('Zend/Controller/Plugin/Abstract.php');
class Joomla_Controllers_Plugins_Router extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$task = $request->getParam('task')
switch($task) {
case 'edit':
$request->setActionName('edit');
break;
case 'save':
$request->setActionName('save');
break;
default:
$request->setActionName('index');
break;
}
}
}

First I’ve include plugin class and then extended my Router class from Zend_Controller_Plugin_Abstract.

This Zend_Controller_Plugin_Abstract class contain a method preDispatch that is called before any controller is called in the routing process.

In the preDispatch method, I get the param “task” and write a switch statement to check this param. If it is edit, I give control to my editAction of the default IndexController. And similarly if it is save, I route request to my saveAction and by default, if no parameter is specified, the request will be dispatched to indexAction of my IndexController.

That’s it.

Now if you write

http://localhost/joomla/administrator/index.php?option=com_advertisers&task=save

It will show what ever you have written in your save.phtml file.

And similarly if you write

http://localhost/joomla/administrator/index.php?option=com_advertisers&task=edit

it will route you to editAction and will display whatever you have define there.

Any question and suggestion are welcomed.

44 comments:

  1. FAHEEM -> thks alot for this tutorial!!!
    I will prove!!
    Suggestion: you have the base, could you make a plugin? could be great!! ;D

    ReplyDelete
  2. Hi,

    Intresting tutorial.

    I encounter the folowing problem intregrating different controllers with joomla,

    I got the folowing message:

    Class 'JModuleHelper' not found in www\joomla\administrator\templates\khepri\index.php on line 41

    Thanks

    ReplyDelete
  3. Is there Any Sample Joomla Module which is build on Zend Framework

    Dan

    ReplyDelete
  4. Great tutorial. I hesitate to use ZF for the MVC since Joomla already has a lot of overhead in it's MVC. An empty component does around 4MB per request. Adding ZF on top of that, I don't know. But great tut none the less.

    ReplyDelete
  5. Hi,
    Very goog tutorial.

    The problem with the JmoduleHelper is linked with the Zend Autoloader.

    Youm ust include the class that you need to use and not instanciate the Autoloader.

    That's all.

    ReplyDelete
  6. Great Job
    If you have a second controller or a modular ZF application, how do you manage its routing from joomla URL to ZF internal URI?

    ReplyDelete
    Replies
    1. My suggestion would be to extent the functionality of the Joomla_Controllers_Plugins_Router class.

      To illustrate how this can be achieved I'll take the following URL:

      /index.php?option=com_advertisers&module=mymodule&controller=mycontroller&action=myaction

      and then in the router plug in you have something like:

      getParams();

      // If module, controller, task/action are set assign them to the request
      isset ($params['module']) ? $request->setModuleName($params['module']) : null;
      isset ($params['controller']) ? $request->setControllerName($params['controller']) : null;
      isset ($params['action']) ? $request->setActionName($params['action']) : null;


      }
      }
      ?>

      You have two preconditions with this approach.
      1. You have set modular architecture in your front controller
      2. You have set default values for: module, controller, action because as you can see those values are set in the request only if the exist as variables in the request (params).

      Hope it helps

      Delete
  7. Good post about joomla. I think mostly website is using that joomla. because it can handle easy accept so as. Joomla Developers

    ReplyDelete
  8. Best Software Downloads and Reviews. the most comprehensive source for free-to-
    trysoftware downloads on the WebBEST 4
    DOWNLOADS

    ReplyDelete
  9. nice tutorial thnks

    ReplyDelete
  10. I wanted to stop by and let you know, I read this article a while ago and loved the idea. So I have taken the basics and run with them. Basically, I am creating a component to run the Zend apps in the system. I would love your input on my project and any thoughts you may have: http://joomlazend.rbsolutions.org/

    ReplyDelete
  11. I wanted to know if i can install joomla and zend optimizer on a local machine and check as to how the performance has improved on the same desktop.

    thanks
    Tanvi

    ReplyDelete
  12. It was just amazing information sharing and it's helpful for everyone.
    - Prom Dresses

    ReplyDelete
  13. I thought I know everything (or almost) about Joomla. I was soo-o-o wrong! Thanks for wonderful tutorial!

    ReplyDelete
  14. Thanks this was inspirational. I did things a little bit differently, but seeing that others had brought Zend into Joomla successfully encouraged me to do so. I was able to build my component much faster using Zend capabilities. I was also inspired by the anonymous guy who took it and ran with it and created http://joomlazend.rbsolutions.org/
    I didn't see that comment until after I started my own thing so my approach was a little different, but I used that project to solve a few problems with what I had done. In the future I plan to start with that project as a base.

    ReplyDelete
  15. Nice framework explanation.It really excellent and great information.i am glad to write this comments.It is also very impressive.Thanks for making it.

    ReplyDelete
  16. Such a nice and unique information sharing. thanks for that.
    - Dividend tax

    ReplyDelete
  17. Such a useful information sharing and its helpful for everyone.

    - Tend Skin Products

    ReplyDelete
  18. really amazing blog and I was thankful to you for sharing such a useful information.
    - CRM Services

    ReplyDelete
  19. it was just informative news and thanks for sharing such a useful information.
    - anna maria island rentals

    ReplyDelete
  20. We offer high end Site Design and SEO services in New York. We explore new horizons regularly to get you the edge over competitors.

    - website companies

    ReplyDelete
  21. Great…Its Working…Really Good… its very useful for topic.Keep writing some more interesting topics like a Joomla Developer

    ReplyDelete
  22. Very interesting read. I have a lot of work and learning to do. Thanks!
    Joomla Developer

    ReplyDelete
  23. Great information! Thank you for sharing your ideas. I like your post and share it on my twitter and Facebook wall Joomla Developer .

    ReplyDelete
  24. A great article explaining about website development very clearly. I have no idea how many people will read your post, because I already send your post to my friend list on twitter, facebook and stumble upon.

    ReplyDelete
  25. Non-Profit Logo Design
    Accurate results and precision is the base you select the developers to redesign your portal. You have won our satisfaction level and further we will surely work with you.

    ReplyDelete
  26. Thanks for sharing such useful information. It will helpful for those people what they really need.

    ReplyDelete
  27. Update yourself with the latest news on the trading world. If you scrutinize the daily news about equity market, you surely will come to know how various online sharks (platforms) are duping the investor in the shape of money lenders.

    ReplyDelete
  28. really amazing blog and I was thankful to you for sharing such a useful information.
    - CRM for Iphone

    ReplyDelete
  29. Hey, Absolutely abundant work, I would like to accompany your blog anyhow so amuse abide administration with us,

    Advertising agencies in pakistan

    ReplyDelete
  30. Finally a real comment: I just used this for a project of mine which had to be integrated with Joomla after the fact. All of my routing and such was dependent on the standard Zend route, but that doesn't work in Joomla. I took Rob Allen's query router and updated it a bit to include some Joomla things like the option: http://pastebin.com/A5xSVmNA Modifications are at like 91-95 to account for the option and ajax raw format controller

    ReplyDelete
  31. Are you looking for an expert, experienced and equally dedicated Joomla Developer for your new project or for the maintenance of your current project? Our Joomla Web Developer’s team of skilled professionals develops or modify modest to complex Joomla Web Application Development. Our specialized Joomla web developer delivers cost-effective solutions with accuracy and timeline in classify to develop Joomla based website for superior efficiency and usability of CMS.

    ReplyDelete
  32. The best design of website could making in only joomla tutorials.Because i designed one website in joomla tutorials.It was very nice compared to other tutorials websites Web Design Services Bangalore | Web Designing Company Bangalore

    ReplyDelete
  33. Great Work. This post is worth everyone’s attention. web design company in chennai

    ReplyDelete
  34. Informative article, just what I was looking for.seo services chennai

    ReplyDelete
  35. Can truly relate and retain this outstanding post. Very well written. web design company Chennai

    ReplyDelete
  36. This article shares great information, thanks for sharing it with us...
    Yasir Jamal

    ReplyDelete