четверг, 6 ноября 2008 г.

CakePHP: static pages in admin area

Sometimes you need to have some static pages in administrative area, e.g.
Router::connect('admin/settings', array('controller'=>'pages', 'action'=>'display', 'admin', 'settings', 'admin' => true));

Cake's PagesController does not handle these by default, because admin_display() function is not defined. To add it without duplicating the display() function, you can do this:
function admin_display() {
$args = func_get_args();
call_user_func_array(array(&$this, 'display'), $args);
}

суббота, 27 сентября 2008 г.

Removing extra characters in generated images

Couple of times when developing with CakePHP (this post may be applied to any PHP app) I faced the following problem: GD (or ImageMagick)-generated images did not show properly in a browser. The reason for this was the presence of extra symbols in the generated image (usually the linefeed - 0x0A). This happens if you have empty line breaks after the closing (or before the opening) PHP tag in your source files. So, what needs to be done is to remove those line breaks.
In CakePHP case it's not very easy because the controller file which usually generates an image by some action is not the only file loaded by PHP interpreter. But there is a function in PHP that can tell you what files have been included and therefore could influence the output. The function is get_included_files(). Here is an example of debug() output of function results:

app\controllers\users_controller.php (line 13)

Array
(
[0] => Y:\home\my_project\www\index.php
[1] => Y:\home\cakes\latest\cake\bootstrap.php
[2] => Y:\home\cakes\latest\cake\basics.php
[3] => Y:\home\cakes\latest\cake\config\paths.php
[4] => Y:\home\cakes\latest\cake\libs\object.php
[5] => Y:\home\cakes\latest\cake\libs\inflector.php
[6] => Y:\home\cakes\latest\cake\libs\set.php
[7] => Y:\home\cakes\latest\cake\libs\configure.php
[8] => Y:\home\cakes\latest\cake\libs\file.php
[9] => Y:\home\cakes\latest\cake\libs\folder.php
[10] => Y:\home\cakes\latest\cake\libs\cache.php
[11] => Y:\home\my_project\cake_files\app\config\core.php
[12] => Y:\home\cakes\latest\cake\libs\debugger.php
[13] => Y:\home\cakes\latest\cake\libs\cake_log.php
[14] => Y:\home\cakes\latest\cake\libs\cache\file.php
[15] => Y:\home\my_project\cake_files\app\config\bootstrap.php
[16] => Y:\home\cakes\latest\cake\libs\string.php
[17] => Y:\home\cakes\latest\cake\libs\session.php
[18] => Y:\home\cakes\latest\cake\libs\security.php
[19] => Y:\home\cakes\latest\cake\dispatcher.php
[20] => Y:\home\cakes\latest\cake\libs\router.php
[21] => Y:\home\cakes\latest\cake\libs\controller\controller.php
[22] => Y:\home\cakes\latest\cake\libs\controller\component.php
[23] => Y:\home\cakes\latest\cake\libs\view\view.php
[24] => Y:\home\cakes\latest\cake\libs\view\helper.php
[25] => Y:\home\cakes\latest\cake\libs\overloadable.php
[26] => Y:\home\cakes\latest\cake\libs\overloadable_php5.php
[27] => Y:\home\cakes\latest\cake\libs\class_registry.php
[28] => Y:\home\my_project\cake_files\app\config\routes.php
[29] => Y:\home\my_project\cake_files\app\config\inflections.php
[30] => Y:\home\my_project\cake_files\app\app_controller.php
[31] => Y:\home\my_project\cake_files\app\controllers\users_controller.php
[32] => Y:\home\cakes\latest\cake\libs\controller\components\session.php
[33] => Y:\home\my_project\cake_files\app\controllers\components\helper_functions.php
[34] => Y:\home\my_project\cake_files\app\controllers\components\sd_auth.php
[35] => Y:\home\cakes\latest\cake\libs\controller\components\request_handler.php
)

A pretty long list, but it's unlikely that core CakePHP files have extra line breaks inside, so we should only check the files that belong to the project and clean the extra characters out.

P.S. This could probably be automated by a bash/php script.