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

12 Responses to “Finally a practical solution: Joomla with CakePHP together - JAKE”

Subscribe to comments with RSS Feed or TrackBack from your own site.


  1. on Wed, 14 February, 2007 at 4:44 am

    WOW!

    Max, thanks a lot!! My next CakePHP project - an Open source Conference/Event Management software - needed precisely this

  2. Tom said,

    on Wed, 14 February, 2007 at 5:32 am

    You are my hero!
    This really marries two of the biggest things I use right now. I never really got into making components/modules for Joomla, I just hacked core files, setup sites, modified other components/modules, etc. but never really had a situation where I had to…but I wanted to…but couldn’t justify taking the time to learn without a cause.

    On the other hand, I have done a good bit with Cake and can see a LOT of circumstances where I can now use Joomla as a base and add powerful functionality with Cake….this makes a lot possible.

    This will undoubtedly save a TON of time too.
    Two questions though:

    1, What about Jake and Joomla 1.5 ?? Since Joomla 1.5 will be on an MVC model…

    2. Can you use current Joomla database tables with Jake? Or is Jake just wrapped in Joomla? In other words, can a model be setup for one of Joomla’s tables? (…then a controller) so you pass around Joomla data using Cake/Jake?

    Thanks again - AMAZING work.

  3. admin said,

    on Wed, 14 February, 2007 at 8:38 am

    @Tarique Sani: Dont forget to post your experience with this. I have a feeling that we can extend this to a great solution.

    @Tom: Thanks Tom, I have already tested it with Joomla 1.5 and it works great.
    Also, It uses same database, its not wrapped, its fully integrated with Joomla like any other Joomla component.

  4. Daniel said,

    on Thu, 15 February, 2007 at 3:58 am

    Thanks a heap for this.

    I’m in the same boat as Tom. I run a site on Joomla, and have created some minor modules and done some hacks. I never got around to creating components. Part of the reason for that was my lack of knowledge about MVC.

    Now that i’ve started using Cake i’m learning more about MVC, but would love to leverage the power of Cake to create components for Joomla. I’ve got Cake up and running nicely on my IIS box (the live site unfortunately uses IIS with no possibility of changing root directories, creating virtual directories, or adding asapi_rewrite).

    I’ll see how this goes under IIS.

    Thanks once again.


  5. on Thu, 15 February, 2007 at 8:47 pm

    Max - you rock! I am hoping there will be Cheesecake in your Joomla! soon now that you have Jake! Am watching you all work!

  6. admin said,

    on Thu, 15 February, 2007 at 8:53 pm

    Keep watching Amy…. You will see more cool exciting stuff :) I’m waiting for more updated version of Jake by Mariano, then I will start with some cool integrations of the components.

    Combining com_users, com_banners and other standard components with CakePHP’s controllers will save huge development time.

  7. Richard said,

    on Thu, 15 February, 2007 at 8:57 pm

    Awesome, I am just coming from Amy’s blog
    http://community.nebraska.edu/amyblog/index.php/2007/02/14/cakephp-as-a-joomla-component-jake/

    This is really a cool idea. I was always thinking to start my own CMS with Cake but was very lazy to do it… Now I dont see any use of creating it. You already joined it with a big power - Joomla!. Thanks, Max…

    Richard


  8. on Thu, 15 February, 2007 at 9:57 pm

    Very happy … smiling! Yes, the more you can integrate into the Joomla! base, the more powerful this becomes. GOOD!

    I don’t suppose you or any of the geeky Google PHPCake friends you are working with live close to Sunnyvale California?

    I sure would *love* to have this presented as an hour session at the Open Source Content Management System Conference in March. Very important advancements you are introducing - basically - you are marrying two very powerful systems with enormous potential.

    The conference is free and there are only a couple of Joomla! sessions so far. Have to register sessions by TOMORROW! If someone wanted to present this - wow! that would be awesome!

    Keep on doing what you do! It’s good stuff!
    Amy :)


  9. on Fri, 16 February, 2007 at 1:26 am

    The revamped Jake is near… In the meantime, I’ve set up its homepage at:

    http://dev.sypad.com/projects/jake

    Check soon as there will be lots of updates, including first beta release.

    [ Max can you send me an email or add me to your MSN (my account is the email address I’ve used to post this comment)? Need to talk to you but my email don’t seem to be getting through to your account.]

  10. admin said,

    on Fri, 16 February, 2007 at 9:42 am

    @Amy Stephen: I would love to join the confrence but I’m in India.. I’m sure it will be good for other CakePHP guys…

    @Mariano: I have added you now.


  11. on Fri, 16 February, 2007 at 9:34 pm

    I suppose India is a bit of a distance! Too bad.


  12. on Tue, 5 February, 2008 at 10:12 am

    Great! I tried to do a joomla cake integration since weeks, but didn’t write correctly reform_url.
    Thanks for your help.

Leave A Reply