Пример использования Zend Framework в компоненте Joomla

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

//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.

zend_dir_structure

 

 

 

 

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.

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.

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

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

//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

//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.

5 1 Голосовать
Оцените статью =)

Вам может также понравиться...

Подписаться
Сообщать о
guest
0 Комментарий
Inline Feedbacks
Смотреть все комментарии
0
Есть вопросы? Задавайте =)x