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 '
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
-
-
require_once($mainframe->getPath('front_html'));
-
$mainframe->setPageTitle("com_cake: Ultimate Joomla Component");
-
-
-
// As this component (cakephp) will need database access, lets include Joomla's config file
-
require_once($joomla_path.'/configuration.php');
-
-
// Constants to be used later in com_cake
-
-
$controller=mosGetParam( $_REQUEST ,'module'); //option passed is treated as a controller in cake
-
$action=mosGetParam( $_REQUEST ,'task'); //task passed is treated as a controller in cake
-
$param=mosGetParam( $_REQUEST ,'id');
-
-
HTML_cake::requestCakePHP('/'.$controller.'/'.$action.'/'.$param);
-
-
?>
cake.html.php
Edit Cake's database.php
-
<?php
-
-
class DATABASE_CONFIG
-
{
-
'driver' => 'mysql',
-
'connect' => 'mysql_connect',
-
'host' => DB_SERVER,
-
'login' => DB_USER,
-
'password' => DB_PASSWORD,
-
'database' => DB_NAME
-
);
-
}
-
?>
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
-
function reform_url($url)
-
{
-
$controller=$temp[1];
-
$action=$temp[2];
-
$param=$temp[3];
-
$url=JOOMLA_PATH.'/index.php?option=com_cake&module='.$controller.'&task='.$action;
-
if($param)
-
$url=$url.'&id='.$param;
-
return $url;
-
}
-
?>
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:
-
function url($url = null, $return = false) {
-
-
$base = strip_plugin($this->base, $this->plugin);
-
} else {
-
$base = $this->base;
-
}
-
-
return $this->here;
-
} elseif($url{0} == '/') {
-
$url=reform_url($url); //Lets change the URL
-
$output = $base . $url;
-
} else {
-
$url=reform_url($url); //Lets change the URL
-
}
-
-
return $this->output($output, $return);
-
}
Similar changes were done in HTML::link(), HTML::image() and Controller:flash()
-
function flash($message, $url, $pause = 1) {
-
$this->autoRender = false;
-
$this->autoLayout = false;
-
$this->set('url', reform_url($this->base . $url));
-
$this->set('message', $message);
-
$this->set('pause', $pause);
-
$this->set('page_title', $message);
-
-
$flash = VIEWS . 'layouts' . DS . 'flash.thtml';
-
} elseif ($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.thtml')) {
-
}
-
$this->render(null, false, $flash);
-
}
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:
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
