13 Feb

Finally a practical solution: Joomla with CakePHP together - JAKE

Articles, CakePHP, Latest Developments

For all those who liked my previous post where we integrated Joomla and CakePHP together, will surely love this one. There were some problems and limitations with the previous system, the major one was - The users who are already running Joomla couldn't take advantage of Cake as the setup involved in changing paths and all. Thanks to Dr. Tarique Sani for suggesting me that.

Ok, so lets start with the setup. Here the concept is, to install CakePHP as any other Joomla component and lets not modify anything with the present setup of Joomla. This is how we will start writing our component, com_cake [You can skip this part if you are interested in getting ready to use component] :

1. Lets install CakePHP in '\components\com_cake' of Joomla directory.

2. As we did before, lets create triggers for Cake... which are actual part of the components too: cake.php and cake.html.php

Here is cake.php

PHP:
  1. <?php
  2. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  3.  
  4. require_once($mainframe->getPath('front_html'));
  5. $mainframe->setPageTitle("com_cake: Ultimate Joomla Component");
  6.  
  7. $joomla_path=dirname(dirname(dirname(__FILE__)));
  8.  
  9. // As this component (cakephp) will need database access, lets include Joomla's config file
  10. require_once($joomla_path.'/configuration.php');
  11.  
  12. // Constants to be used later in com_cake
  13. define(JOOMLA_PATH,$mosConfig_live_site);
  14. define(DB_SERVER,$mosConfig_host);
  15. define(DB_USER,$mosConfig_user);
  16. define(DB_PASSWORD,$mosConfig_password);
  17. define(DB_NAME,$mosConfig_db);
  18.  
  19. $controller=mosGetParam( $_REQUEST ,'module'); //option passed is treated as a controller in cake
  20. $action=mosGetParam( $_REQUEST ,'task'); //task passed is treated as a controller in cake
  21. $param=mosGetParam( $_REQUEST ,'id');
  22.  
  23. HTML_cake::requestCakePHP('/'.$controller.'/'.$action.'/'.$param);
  24.  
  25. ?>

cake.html.php

PHP:
  1. <?php
  2. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  3.  
  4. class HTML_cake {
  5.  
  6. function requestCakePHP($url)
  7. {
  8.     $_GET['url']=$url;
  9.     require_once 'app\webroot\index.php';
  10. }
  11.  
  12. }
  13. ?>

Edit Cake's database.php

PHP:
  1. <?php
  2.  
  3. class DATABASE_CONFIG
  4. {
  5.     var $default = array(
  6.         'driver' => 'mysql',
  7.         'connect' => 'mysql_connect',
  8.         'host' => DB_SERVER,
  9.         'login' => DB_USER,
  10.         'password' => DB_PASSWORD,
  11.         'database' => DB_NAME
  12.     );
  13. }
  14. ?>

Ok, now we can see that URL like:
http://localhost/joomla/index.php?option=com_cake
will open Cake's homepage in Joomla layout. Make sure you have edited your default.thtml file so that CSS and HTML tags do not mess up.

http://localhost/joomla/index.php?option=com_cake&module=names&task=add
will call CakePHP's 'names' controller and 'add' function.
(Please dont confuse 'module' variable in URL with Joomla's module !!)
It will work great but we need even more. We also need scafollding/bake and more things which Cake can give... In my case, I loved baking than any other thing, so as to support bake process. Lets do next step.

3. Since Cake baked code uses helpers to output links, images or any other URL. So, lets hack Cake helper to output Joomla's URL in this formats:

index.php?option=com_cake&module=names&task=add
index.php?option=com_cake&module=names&task=index
index.php?option=com_cake&module=names&task=view&id=12

So, I wrote a small function for this, as we all love our HTML helper in Cake:
Write this function on bootstrap.php

PHP:
  1. <?php
  2.   function reform_url($url)
  3.     {
  4.         $temp=explode('/',$url);
  5.         $controller=$temp[1];
  6.         $action=$temp[2];
  7.         $param=$temp[3];
  8.         $url=JOOMLA_PATH.'/index.php?option=com_cake&module='.$controller.'&task='.$action;
  9.         if($param)
  10.             $url=$url.'&id='.$param;
  11.         return $url;
  12.     }
  13. ?>

Now we need to integrate this function with the helper functions so that it takes input in same format but output Joomla URL.

Copy HTML Helper (html.php) to your views/helpers folder and edit the following functions:

PHP:
  1. function url($url = null, $return = false) {
  2.  
  3.         if (isset($this->plugin)) {
  4.             $base = strip_plugin($this->base, $this->plugin);
  5.         } else {
  6.             $base = $this->base;
  7.         }
  8.  
  9.         if (empty($url)) {
  10.             return $this->here;
  11.         } elseif($url{0} == '/') {
  12.             $url=reform_url($url)//Lets change the URL
  13.             $output = $base . $url;
  14.         } else {
  15.             $url=reform_url($url)//Lets change the URL
  16.             $output = $base . '/' . strtolower($this->params['controller']) . '/' . $url;
  17.         }
  18.  
  19.         return $this->output($output, $return);
  20.     }

Similar changes were done in HTML::link(), HTML::image() and Controller:flash()

PHP:
  1. function flash($message, $url, $pause = 1) {
  2.         $this->autoRender = false;
  3.         $this->autoLayout = false;
  4.         $this->set('url', reform_url($this->base . $url));
  5.         $this->set('message', $message);
  6.         $this->set('pause', $pause);
  7.         $this->set('page_title', $message);
  8.  
  9.         if (file_exists(VIEWS . 'layouts' . DS . 'flash.thtml')) {
  10.             $flash = VIEWS . 'layouts' . DS . 'flash.thtml';
  11.         } elseif ($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.thtml')) {
  12.         }
  13.         $this->render(null, false, $flash);
  14. }

4. We are all done, and our Cake is waiting for us....!!!
I did one more mod so that you people can still bake on using bake.php... Edit com_cake\cake\scripts\bake.php and on starting lines add this:

PHP:
  1. $joomla_path=dirname(dirname(dirname(dirname(dirname(__FILE__)))));
  2. require_once($joomla_path.'/configuration.php');

Most of your small applications developed with Cake will still work with Joomla using this component, as we have modified the helpers too. You dont even have to change Cake's conding conventions to use this plus you can still use bake.php anytime.

You can download the component here.

See it in action:
CakePHP Homepage in Joomla
Names::index()
Names::add()

Please post your feedback if you like this, good feedback on my previous article inspired me to do this.. I will post more interesting stuff soon.

UPDATE - 16, Feburary: Mariano Iglesias is extending this project further. Here's the Jake Homepage

Thanks
- Max