Archive for 'PHP'

Disabling PHP in a specific directory

To disable the PHP processor in a given directory, put the following in an .htaccess file. If one doesn’t exist, create it.

# Disable PHP
AddHandler default-handler php
RemoveType application/x-httpd-php php

# Make .php files display as plain text
AddType text/plain php

This assumes an Apache server. PHP on IIS may involve different steps.

Files starting with a . are hidden by default on *nix OSes. To see them in listings, use ls -a.

Migrating from Smarty to Flexy

Templates are a great way to separate business logic from page layout in web applications. Smarty (LGPL) and Flexy (PHP License) are two great, mature templating libraries for PHP. Flexy is the younger and faster of two, so there is a tendency to migrate installed Smarty implementations onto it.

Migrating the PHP

Engine Declaration

Smarty:

$smarty = new Smarty();

Flexy:

$smarty = new HTML_Template_Flexy_SmartyAPI();

This API is largely limited to emulating the assign() method.

Engine Configuration

Smarty:

$smarty->template_dir = './smarty/templates';
$smarty->compile_dir = './smarty/templates_c';
$smarty->cache_dir = './smarty/cache';
$smarty->config_dir = './smarty/config';

Flexy:

$options = array();
$options['templateDir'][] = dirname(__FILE__) . '\\flexy\templates';
$options['compileDir'] = dirname(__FILE__) . '\\flexy\templates_c';

Your templates go into the templateDir. Flexy will compile them to optimized PHP code and cache it in compileDir.

Page Display

Smarty:

$smarty->display($template);

Flexy:

$object = new StdClass;
foreach($smarty->vars as $k=>$v) {
	$object->$k = $v;
}

$flexy = new HTML_Template_Flexy($options);
$flexy->compile($template);
$flexy->outputObject($object, array());

Needless to say, this code would be much simpler if you were writing a web app from scratch. The goal during migration is to minimize points of change, which is achieved by slightly baroque markup at selected points rather than elegant markup across the board.

Migrating the Templates

Unfortunately, Flexy’s syntax differs from that of Smarty. I would recommend doing a search-and-replace with regular expressions across all your templates. On Windows, UltraEdit offers this across multiple files. On Unix, a script could be readily written.

Variables

Smarty:

{$blurb}

Flexy:

{blurb:h}
{blurb}	// escaped HTML
{blurb:u}	// URL-encoded

Unlike Smarty, Flexy escapes HTML tags by default. It’s arguably more secure, but I find that I need to insert raw HTML into a template fairly often.

Array Loops

Smarty:

{section name=t loop=$tables}
  • {$tables[t]}
  • {/section}

    Flexy:

    {foreach:tables,key,value}
    
  • {value:h}
  • {end:}

    Lower-case t should be avoided as a variable name in Flexy.

    Javascript Variables

    Smarty:

    Flexy:

    This is a useful way to transfer a variable from PHP to your client-side Javascript. It comes especially handy with Javascript strings.

    Includes

    Smarty:

    {include file='0top.tpl'}

    Flexy:

    Documentation

    Flexy Home
    Flexy Documentation

    This covers the minimum needed to migrate from Smarty to Flexy. I highly recommend abstracting and encapsulating some of these operations for ease of maintenance. It’s best to avoid repeating yourself.

    Installing Pear and PECL on Zend Core for IBM

    Links

    Atomized | PHP Performance Best Practices – Informative.

    Installing PEAR and PECL on Zend Core for IBM

    1. Save http://go-pear.org/ to go-pear.php (in, say, C:\Program Files\Zend\Core for IBM\pear)
    2. Open Command Prompt
    3. % cd “C:\Program Files\Zend\Core for IBM\pear”
    4. % ../bin/php go-pear.php
    5. Follow the steps and let it modify your php.ini
    6. Restart your Apache

    Installing the profiling packages mentioned in Best Practices

    1. % pear install Benchmark
    2. % pecl install apd