Archive for the ‘PHP’ Category

Symfony ezComponents Integration

Monday, August 27th, 2007

As the documentation on how to integrate and use ezComponents in Symfony is quite meager here’s a small example on that. Exemplarily I picked the Graph component for the demonstration. Further documentaion on that can be found here.
Asuming there is an already configured symfony project with one or more application and modules the first thing to do after downloading ezComponents is to prepare symfony for the usage and copy the ezComponent classes into the project folder (they can also be referenced as explained in symfony book chapter 17 but copying the files has some advantages. E.g. you don’t have to change the application’s settings.yml when swapping from Win to Linux).
Configuration is quite easy. Just copy the base folder of the ezComponent you want to use into your symfony lib folder. In our case the folders “Base”, “autoload” and “Graph” are needed. Afterwards you just have to activate the built-in bridge class in symfony. So search for the line autoloading_functions: in your settings.yml, uncomment it and add the following line:
- [sfEzComponentsBridge, autoload]
The whole block should look like that afterwards:

.settings:
.
.
.
  autoloading_functions:
    - [sfEzComponentsBridge, autoload]

After doing this we can instance the ez classes like every other custom class from the lib folder. Let’s assume you have a shop and want to illustrate how many items of each product are sold per day. The following code snippets demonstrate how to generate a simple line chart showing the amount of sold products broken down by product category for each day of the year. Please ignore the absurdity or the performance issues of this approach as it shall only demonstrate how the data passed to the graph class has to be formatted ;)
This is the actions.class.php of the module:

public function executeIndex()
{
.
.
.
(the days array could be generated with PEAR::Date)
  foreach ($days as $day) {
    $product_1[$day]++;
    $product_2[$day]++;
    $c = new Criteria();
    $c->add(SalesPeer::DAY,$day);
    $sales = SalesPeer::doSelect($c);
    foreach ($sales as $sale) {
      if($sales->getProductId() == '1') {
      $product_1[$day]++;
    } elseif($sales->getProductId() == '2') {
      $product_2[$day]++;
    }
  }

This results in an array with the days of the year as indexes and the sale count as value. Now we just have to pass those arrays to the Graph component:

$data = array('Product 1' => $product_1,'Product 2' => $product_2);
//init graph
$graph = new ezcGraphLineChart();
//set title of the chart
$graph->title = 'Sales';
//tell class to generate a jpg instead of svg
$graph->driver = new ezcGraphGdDriver();
$graph->options->font = 'fonts/tutorial_font.ttf';
$graph->driver->options->supersampling = 1;
$graph->driver->options->jpegQuality = 100;
$graph->driver->options->imageFormat = IMG_JPEG;
//assign previously generated arrays to class
foreach ( $data as $item => $count ) {
$graph->data[$item] = new ezcGraphArrayDataSet( $count );
}
//render image
$graph->render( 500, 300, 'images/charts/sales_chart.jpg' );
}

The ezComponents automatically know about the application paths so if you don’t pass any dir names (like “images/charts/”) the output would be saved to the “web” folder (same for the ttf file for the jpg generation). So all you have to do to display the generated chart on a page is the call of the “image_tag” helper:

< ?php echo image_tag('charts/sales_chart.jpg'); ?>

As you can see, quite simple and very comfortable. Have fun with it!

Zend Framework All-In-One Getting Started Package

Saturday, December 2nd, 2006

I started a new PHP project today and as everytime I was pissed that setting up a proper project structure always takes so much time. So I thought it might be useful for some of you if I present my result right here. The following zip file contains a, for my purposes quite suitable all-in-one out-of-the-box eclipse project for a Windows development environment. I basically went through some Zend Developerzone tutorials and put it alltogether.
Let’s start with the general configuration. The code base can be used on any Windows system with WAMP installed but to get things started even faster I recommend to install the following two software packages first:

  1. PHP IDE from Zend (which is basically Eclipse with some neat little plugins for PHP development)
  2. XAMPP as WAMP package.

The whole paths in my scripts are customized for this configuration so as I said, for quick quickstart get those two first. XAMPP isn’t shipped with mod_rewrite enabled, so the first thing to do is to adjust the httpd.conf in “XAMPP_HOME\apache\conf”.
Next thing to do is to remove all files and subfolders in “XAMPP_HOME\htdocs” and to extract the zip from this page into that folder.
Afterwards change DocumentRoot and attribute from “XAMPP_HOME/htdocs” to “XAMPP_HOME/htdocs/www” and (re)start the server.
The provided zip file comes with the following features:

  • Zend Framework 0.2.0
  • Smarty Template Engine 2.6.16
  • Propel 1.2.0
  • Creole 1.1.0 (needed for Propel)
  • Phing 2.2.0 (needed for Propel)

The directory structure explained in detail:

  • app – this is where all MVC classes should go. The “views” folder conatins a example.tpl.html which is just a standard Smarty template. I prefer to name my tpl files with .html ending as they can be opened more easy and are recognised as HTML by Eclipse.
  • bin – this folder just contains the batch file for model generation
  • build – this is where all propel related files should go to. The files from the archive are simply copy & pasted from this tutorial. Generation of the sample model from that tutorial should work out-of-the-box (as long as you changed the path variables the right way).
  • cfg – Here goes the config.ini file used by Zend_Config and the autogenerated propel-config.php
  • lib – self-explanatory I guess. Be sure to add the path to /www/.htaccess if you add new libs here.
  • www – this is the document root for the apache server. Only content (images, CSS, JS, etc.) needed by clients should go here as it’s public.

As a special feature there is a generate-model.bat which can be used to automatically start the phing build process for propel ORM generation.
To get started with this base project structure there are some config lines which have to be overviewed:

  1. “set XAMPP_HOME=c:\Programme\xampp” in “bin/generate-model.bat”
  2. “smarty.template_dir= C:\Programme\xampp\htdocs\app\views” in “cfg/config.ini”
  3. Paths and DB config in “build/build.properties”

Sorry for not giving more details on how to use the code base, but it’s late, I’m tired and everything one needs to know is written on Zend Developerzone… ;)
I’m planing to integrate the LiveUser package aswell the next days, maybe I’ll give you an update. Feel free to comment, ask questions or indicate errors…
Oh, almost forgot HERE is the zip.

Zend Framework in subdirs

Wednesday, October 18th, 2006

It took me some time to find this out so maybe it’s useful for you aswell…
I went through the very good tutorial written by Chris Shiflett concerning Zend Framework and was confontend with a little problem. Everything worked fine except the redirection by the Controller objects, I always was redirected to the noRouteAction. The problem is that I installed the application in a vhost subdirectory (http://devserver/subdir/) and it kept redirecting me to the server root. To “fix” this issue one have to make use of the Zend_Controller_RewriteRouter class. The following code snippet shows the usage:

< ?php
include 'Zend.php';

Zend::loadClass('Zend_Controller_Front');
Zend::loadClass('Zend_Controller_RewriteRouter');

$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory(
'/path/to/controller_classes');

Zend::register('controller', $controller);
$router = new Zend_Controller_RewriteRouter();
$controller->setRouter($router);
$controller->dispatch();

?>

Zend Framework

Wednesday, October 18th, 2006

I dealt with PHP first time since ages the last days and did some evaluation work on frameworks. The most promising candidate for future projects is the Zend Framework of which a first stable release is expected during the next weeks.
Most important feature is a MVC implementaion which looks quite similar to Struts. Other neat little features include PDF creation, XMLRPC support, RSS feed handling and many more. The most exciting thing with Zend Framework is the very flexible and “non-invasive” integration opportunity. All features are based on a “can do” basis and it seems to be very easy to integrate 3rd party modules. E.g. integration of Smarty tpl engine and Propel O/R mapping is done in no time (Howtos can be found here and here). I can’t wait for the stable release to come and I’ll definitely give it a try for some upcoming projects. Looks like this could become a Spring for PHP…

pear package installer

Sunday, July 10th, 2005

As I’m always forgetting this quite easy to remember switch a little mental note: --alldeps makes pear install command resolving all needed dependencies automatically… dumb me

LiveUser Beta 0.16.0 released

Sunday, June 26th, 2005

I just realised the guys from LiveUser released Beta 0.16.0 this week. I hope there are not that many changes as I just managed to integrate this Pear package into one of my running projects.
LiveUser is a Pear package for user management including Auth and Permission handling functionality. Unfortunately allthough being a quite a nice piece of code it is aswell one of the least well documented Pear projects I’ve ever seen. As I have to check out the new version anyway I’ll try to contribute some tips on how to get started with this package here during the next time.
Check out LiveUser at the Pear home: http://pear.php.net/package/liveuser