2009
08.18

For my CakePHP project I wanted to use the Javascript helper in a element. This element needs a javascript include (which i only use for this element) so I thought to it would be a good place to add it using the Javascript helper in the element template. To my surprise the javascript did not end up in the $scripts_for_layout variable. After hours of searching and debugging I found issue #6323 in CakePHP trac :

scripts_for_layout is generated _before_ the layout is rendered. Therefore it cannot contain scripts from elements that are included in the layout as that would require bending of space time. As templates are just plain PHP, and are parsed by the PHP interpreter only.

There are alternative solutions for this in the works, but at this time rebuilding space time is a too large of a task. :)

I think the CakePHP team will find some solution for this. Before that you can use the solution therma_lobsterdore provided:

You can get around this problem by manually recreating scripts_for_layout. Remove $scripts_for_layout from your view and replace it with this…

echo join("\n\t", $this->__scripts);

I hope this helps some people experiencing the same problem :)

2009
08.03

For a new project I started using the CakePHP framework. I work on this project on different servers. My laptop and desktop act as development servers and of course a production server. To manage my code I use subversion. Because I also like to check-in all my config files it would be nice to keep them the same in each environment.

To accomplish this I use a environment variable in my httpd.conf. This variable describes which config to use (development/test/production/etc). For now i have only used it on my database config but it would be easy to use for other config parameters.

other/domain.com.conf virtualhost section or httpd.conf
(I like to keep all my domains in separate configs)

<VirtualHost *:80>

….

SetEnv APPLICATION_ENV development
</VirtualHost>

SetEnv sets the APPLICATION_ENV envrionment variable to development for this virtual host. On your production environment you would set this to “production”.

Next change your config/database.php to something like this:

<?php
class DATABASE_CONFIG {

var $default = array(
‘driver’ => ‘mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘login’ => ‘DEFAULT_LOGIN’,
‘password’ => ‘DEFAULT_PASSWORD’,
‘database’ => ‘DEFAULT_DATABASE’,
);

var $production = array(
‘driver’ => ‘mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘login’ => ‘PRODUCTION_LOGIN’,
‘password’ => ‘PRODUCTION_PASSWORD’,
‘database’ => ‘PRODUCTION_DATABASE’,
);

var $development = array(
‘driver’ => ‘mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘login’ => ‘DEVELOPMENT_LOGIN’,
‘password’ => ‘DEVELOPMENT_PASSWORD’,
‘database’ => ‘DEVELOPMENT_DATABASE’,
);

function __construct() {
$env = getenv(“APPLICATION_ENV”);
if( ($env) && isset($this->{$env})) {
$this->default = array_merge($this->default, $this->{$env});
}
}
}
?>

Notice the __construct function. This function merges the default database parameters with the current environment parameters.

When I have some time I will also change the core.php config to work like this so you can for example enable debugging on your development environment and disable it on production.

2009
08.03

Google gives some tips on PHP performance. Most things we all know but it’s nice to read why.