Yii 1.1: Include Yii framework in a Joomla module

16 followers

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.

Total 7 comments

#15394 report it
Fábio Sales at 2013/11/05 02:08pm
Zii Components Conflict

I solved this problem using the jQuery Easy Plugin from Joomla Plugin's Page.

#9285 report it
dannythebestguy at 2012/08/01 03:42pm
Getting Joomla user info in Yii

Hello,

I could get the yii working in Joomla successfully.

Now I am trying to get the logged in user from Joomla to yii.

When I use Login in Joomla and try to access it through JFactory::getUser in Yii the user does not show. When the "Remember Me" option is clicked and logged in the JFactory::getUser works fine.

How can I use JFactory::getUser without Remember Me clicked.

Thnx

#8518 report it
fennellgb at 2012/06/10 12:38am
Fix for Joomla v2.5

To make this work in Joomla v2.5 you need to replace

Yii::registerAutoloader('__autoload');

with

Yii::registerAutoloader(array('JLoader', 'load'), true);

#8505 report it
fennellgb at 2012/06/07 11:19pm
Doesn't work with Joomla v.2.5

It looks like with Joomla v2.5 they have deprecated __autoload and are now using JLoader. Doesn't anyone have the new way to make this work? I'm still trying to figure out the changes, but nothing seems to be working properly.

#2761 report it
zaccaria at 2011/02/07 08:06am
zii widget

I didn't tested zii widget, so far as I know they can work.

What I mean is that I can assure you that will work urlManager, model controllers and all standard part of the framework, about zii widget I have no information.

If anyone will try zii widget in joomla, please update the article!

#2760 report it
Maurizio Domba Cerin at 2011/02/07 04:00am
zii conflict

Hi zaccaria

I presume you have tried the zii components... can you explain which one and how the zii components make conflict... as all the jQuery code that uses $ is wrapped in a function called by using "jQuery" instead of $... it should be isolated from other javascript libraries...

#2684 report it
zaccaria at 2011/01/28 04:53am
Call for help

This article is a result of a joomla module developed with success using yii.

In this case we avoided at all zii widget, we included custom script wrote in mootools.

Anyway the possibility of use url manager, CHtml class, modules and view separed is priceless: developing a module for joomla from scratch is like fall back to the stone age of php programming.

If anyone will use zii widget with success, please update this article adding the methods overridden in AssetsManager!

I hope you enjoy this tutorial!

Leave a comment

Please to leave your comment.

Write new article