Include Yii framework in a Joomla module

  1. What for
  2. Configure Joomla
  3. Modifing the .htaccess
  4. How to change index.php
  5. Changes in configuration
  6. Custom class for http request
  7. Client script
  8. Conclusion

What for

Delploying anything using joomla's framework is quite difficult. The framework don't provide a good MVC structure and the documentation is very poor.

This article will drive you in the creation of a Yii based module for Joomla.

Configure Joomla

From the administration panel create a new Jumi apps, specifying the path of the entry script of your yii application (let say, as example, 'modules/yiiapplication/index.php').

Save the result and check what Id joomla gave to this compoment. Let's say, as example, we have id number 13.

Modifing the .htaccess

In the joomla .htacces we add the followin rules:

RewriteRule yiimodule\.shtml$	/index.php?option=com_jumi&fileid=13&yiiPath=$1   [L]

RewriteRule ^yiimodule/([A-z_0-9/]+)\.shtml$ /index.php?option=com_jumi&fileid=13&yiiPath=$1   [L]

This will explain Joomla that for the path yiimodule he has to use the module with id 13, that means our application.

It will also take the path and passing to the application as the get paramter 'yiiPath'

How to change index.php

Modify your index.php as follows:

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );


require_once($yii);

Yii::registerAutoloader('__autoload');

Yii::createWebApplication($config)->run();
spl_autoload_unregister(array('YiiBase','autoload'));


This will register Yii autoload function and preserve joomla's one, so both frameworks will work at a time (but we will use only our beloved Yii, of corse!).

Changes in configuration

Now is time to update config/main.php:

'assetManager'=>array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'../../assets',
	'baseUrl'=>'modules/yiiapplication/assets',
),
'request'=>array(
	'class'=>'JHttpRequest',
        'baseUrl'=>'yiimodule'

),
'clientScript'=>array(
	'class'=>'JClientScript',
),

The changes to assets manager are necessary, in order to explain in wich directory put the files ans how to explain browser to reach. We also explicitly assign the base url in CHttpRequest, wich will make all generated url to be correct.

The other 2 options (request and clientScript) are optional, but they using some customized class provider great advantages.

Custom class for http request

Now our application can work using routing with get format, but it would be very nice to use path format.

For achive it we have to use a custon CHttpRequest class:

Create a file in components named JHttpRequest with this content:

<?php 

class JHttpRequest extends CHttpRequest
{

	public function getPathInfo()
	{
		return $_GET['yiiPath'];
	}
	
}

In the .htaccss we instructed modRewrite to take the path after yiiApp and to pass it as a get parameter. Rewriting this function will make url manager to work correcly, and the magic is done: we have all the power of urlManager rules!

Client script

For publish our script and css file we cannot rely on the standard assets manager, so let's create one ad hoc.

Create another file in components named JClientScript with this content:

<?php 

class JClientScript extends CClientScript
{
   public function registerCssFile($url, $media='')
   {
      JHTML::_('stylesheet', $filename = '', $path = $url , $attribs = array() ); 
   }

   public function registerScriptFile($url, $media='')
   {
      JHTML::_('script', $filename = '', $path = $url , $attribs = array() ); 
   }
}

As you see, this function will invoke Joomla's methods for publishing css and script file.

Conclusion

Now you can use the power of yii in joomla, but pay attention to compatibility. Joomla uses mootools instead of jquery, so many zii widget can create conflicts, also for styling the page require more attention for not override some joomla class.

8 2
17 followers
Viewed: 29 502 times
Version: 1.1
Category: How-tos
Written by: zaccaria
Last updated by: Anupam
Created on: Jan 28, 2011
Last updated: 7 years ago
Update Article

Revisions

View all history