Tutorial Q&A PDF

1st September 2009

Chris Kirk has kindly provided a Q&A PDF which summarises a number of problems that have been raised and answered in the comments on the tutorial page. If you are having problems, download it and see if it helps.

Thanks Chris!

Three years of my Zend Framework Tutorial

16th August 2009

Three years ago today, I published my first Getting Started with Zend Framework tutorial. This was the announcement. Back then, Zend Framework was at version 0.1.5 and a considerably smaller download than now :)

Three years later and I haven't lost my enthusiasm for Zend Framework as you can tell since the latest version of the tutorial supports ZF 1.8 and 1.9 and uses the new features like Zend_Application and the command line Zend_Tool scripts. And I wrote a Zend Framework book!

I wonder what will happen in the next three years?!

My tutorial is compatible with Zend Framework 1.9

5th August 2009

I've just updated my tutorial to version 1.6.3 after checking that it is still compatible with version 1.9 of Zend Framework.

The only changes I had to make were:

  • ZF 1.9 comes with its own BaseUrl view helper, so there's no need to write our own.
  • ZF 1.9.0's command line tool doesn't work on Windows. I've created patches on issues ZF-7464 and ZF-7465. I'm sure this will be sorted with 1.9.1 though.

ZF 1.9 looks like a really solid release too.

DNS Transfer

3rd August 2009

Just a heads up, I am currently transferring the registrar for akrabat.com, so there may be an interruption of service here if I mess up :)

The watch Linux command line tool

24th July 2009

I'm sure everyone else already knows about watch, but it's new to me. This little utility executes a program repeatedly at a set interval and displays its output.

I've been using it with mysqladmin's processlist command like this:


watch -n 1 /usr/bin/mysqladmin -uroot -pMYPASSWORD  processlist

Note that this does put your password on display at the top of the command window whilst watch is running. If you don't want that, you could write a little bash script instead like this one from a friend of mine:


#!/bin/sh

while :
do
sleep 1
clear
mysqladmin -uroot -pMYPASSWORD processlist 
done

Either way, we get a display of the MySQL process list every second in a Terminal window and it becomes very easy to see which processes are causing trouble.

Bootstrapping modules in ZF 1.8 and up

8th July 2009

I've started to play with modules in a Zend Framework 1.8 application as the new autoloader means that all your model directories no long have to be on the include_path for autoloading to work. What I'm specifically interested in is being able to instantiate a model that is within a module from within another module.

Setting it all up isn't that hard, but I couldn't find a concise description, so these are my notes on it.

Start by creating a ZF application using the zf command line tool:

$ zf create project myproject

Don't forget to put a copy of ZF 1.8 into the library directory or ensure that it is on the include_path.

We now need a module:

$ cd myproject
$ zf create module blog

This will create all the relevant directories in myproject/application/modules/blog. We create a simple model within the blog module:

File: myproject/application/modules/blog/models/Info.php

<?php

class Blog_Model_Info
{
    public function getInfo()
    {
        return "This is the info about the Blog module";
    }
}

The naming is important. First we have the module name, then we have the word "Model" then we have the name of the model itself. It is important that this model's name matches the filename too.

We want to use this model within the index action of the Index controller like this:

File: myproject/application/controller/IndexController.php

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $info = new Blog_Model_Info();
        $this->view->blogInfo $info->getInfo();
    }
}

I've included the entire class here; most of it is auto-generated, you just need to add the two new lines under the // action body comment. Having assigned something to the view, we should display it so we can prove it worked:

File: myproject/application/views/scripts/index/index.phtml

<?php echo $this->blogInfo?>

(Note that we replace the pretty ZF welcome page.)

At this point we get an error:
blog_model_module_failure.png

This is because we haven't told the autoloader about our module's model's directory. This is done using Zend_Application's bootstrapping. There are two parts:

Firstly we have to add a line to application.ini enable modules at the end of the [production] section:

File: myproject/application/configs/application.ini

resources.frontController.moduleDirectory APPLICATION_PATH "/modules"
resources.modules[] = ""

Secondly, we need to add a Bootstrap class to our module:

File: myproject/application/modules/blog/Bootstrap.php

<?php

class Blog_Bootstrap extends Zend_Application_Module_Bootstrap
{

}

Again, the naming is important; the class name must be {module name}_Bootstrap and it must extend Zend_Application_Module_Bootstrap. It must be stored in a file called Bootstrap.php within the root of the module.

That's it. If you refresh the page, you'll get the data from the Blog module's Info model within the default module:

blog_model_module_success.png

All in all, it's not difficult at all, but if you don't have those two lines in application.ini and define a module bootstrap class, then it doesn't work.

I'm speaking at ZenCon 2009!

1st July 2009

I'm speaking at ZendCon 2009 this year!

I'm doing a tutorial session called Zend Framework Certification Bootcamp where I'll be highlighting key sections of Zend Framework that you'll need to know in order to pass the the ZFCE exam.

I'm also presenting a standard session, Getting a website out of the door (aka Managing a website project) which will be a non-code talk about the realities of project management in a small web development company where the PM overhead has to be minimal, but effective.

ZendCon last year was a great conference and this year's is shaping up to be equally as good, if not better. I recommend that you persuade your employer to send you. It'll be money well spent - especially if you register before 28th August and get the discount.

Some notes on Zend Server CE for Mac OS X

22nd June 2009

I've installed Zend Server CE on my Mac to see where it's got to and it's looking quite usable. The installation puts everything into the usr/local/zend directory which is fairly well laid out so that you can find what you are looking for. There's also a a nice admin system at http://localhost:10081 which allows you to restart PHP, view phpinfo(), configure extensions and php.ini. There's also a phpMyAdmin to help administer the bundled MySQL server.

For Mac, this is now one of the better one stop shops for easy PHP & MySQL installation.

Obviously, some things need configuration:

Set up paths

You need access to the command line zendctl.sh and mysql tools:

  • Edit ~/.bash_profile and add:
        PATH=$PATH:/usr/local/zend/bin:/usr/local/zend/mysql/bin
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/zend/lib
    
  • Close Terminal and restart it so that your change takes effect.

Change to port 80

The Apache in Zend Server is configured for 10088 to avoid conflicting with Apple's Web Sharing I suppose. The choice of using port 80 would have been nice as an installation option though.

To use port 80 is easy enough:

  • Stop Apache: sudo zendctl.sh stop-apache
  • Edit /usr/local/zend/apache2/conf/httpd.conf and replace Listen 10088 with Listen 80
  • Edit /usr/local/zend/apache2/bin/apachectl and change STATUSURL="http://localhost:10088/server-status" to STATUSURL="http://localhost:80/server-status"
  • If you are using vhosts, then edit /usr/local/zend/apaches/conf/httpd.conf and replace all instances 10088 with 80
  • Restart Apache:sudo zendctl.sh start-apache

Installing PHPUnit

Update PEAR first:

    sudo pear channel-update pear.php.net
    sudo pear upgrade-all

Install PHPUnit:

    sudo pear channel-discover pear.phpunit.de
    sudo pear install phpunit/PHPUnit

Installing Xdebug

  • Install Xcode so you have a compiler!
  • Stop Apache: sudo zendctl.sh stop-apache
  • Go to http://localhost:10081/ and pick the Server Setup tab. Turn off the Zend Debugger and Zend Data Cache and restart PHP
  • sudo pecl install xdebug
  • Edit /usr/local/zend/etc/php.ini and add above the [zend] section near the bottom:
    zend_extension="/usr/local/zend/lib/php_extensions/xdebug.so" 
    
    [xdebug]
    xdebug.remote_enable=1
    xdebug.remote_host="localhost"
    xdebug.remote_port=9000
    xdebug.show_local_vars=On
    xdebug.var_display_max_data=10000
    xdebug.var_display_max_depth=20
    

    (you should set up your xdebug settings as you require!)

  • Restart Apache:sudo zendctl.sh start-apache
  • The Server Setup->Extensions section of the admin interface should now show xdebug.

All in all, it's remarkably easy to set up Zend Server using PEAR and PECL is which how it should be.

The only other gotcha I noticed is that my.cnf is in /usr/local/zend/mysql/data whereas I would have thought that /usr/local/zend/etc would have been more logical.

DPC '09

15th June 2009

The Dutch PHP Conference is over and so it's time to write a short wrap-up.

Day 1

The conference was opened by a cool animated video and then Cal Evans welcomed us, with a nice short speech. The keynote was given by Andrei Zmievski of Digg. Andrei is a core devloper and gave an interesting overview of what we can expect to see next in PHP.

Cal opens DPC 09

I then listened to Paul Reinheimer talk about some problems you can encounter in conceptually easy situations. I found the section about handling account login issues very interesting and it's an area that I now intend to improve in my code. Ben Ramsey followed with a talk on the theory of REST architectures which was interesting, though not directly relevant to anything that we're doing at the moment. Making sure that I understand it when we come to make web service APIs is important though.

After lunch Matthew Weier O'Phinney talked about contributing to open source projects. Matthew gave a great talk with useful information in it. As a contributor to Zend Framework already, I mainly used this talk to learn how to evangelise the concept of contributing to other people.

I stepped out of the next session into the hallway track where I caught up with some people and then checked my email. Then Jan Lehnardt was on to talk about CouchDB. CouchDB fascinates me as it's so different from the relational databases I'm used to. I don't see that we'll be using it soon though - the paradigm shift is significant.

The day ended with the speakers dinner followed by drinking :)

Day 2

Day 2 dawned bright and far too early and I managed to leave my power adapter at the hotel, so I had to go back for it, missing the opening keynote. I got back to see most of Eli White's talk on scaling. Eli is a good speaker and the talk was well researched. I know that if I ever need to scale a website to 20 database servers and too many web servers, then I'm going to try and head hunt him...

I was intending to see Juliette Reinders Folmer's talk on UTF-8, but Paul suffered from a video adapter failure and so I lent him my laptop and stayed to watch and make sure he didn't break it. Paul gave another interesting talk whilst looking good in his suit.

After lunch, it was my turn to talk. Although I was nervous at the beginning, I think that I got into my stride and the presentation went well with intelligent questions asked by the audience. I think it helped that I advised the people who knew more than I did to leave before we started :) If you were there and haven't yet rated it, then please leave feedback!

The final breakout session that I attended was another by Ben Ramsey about HTTP. There was more here that was directly relevant to work, but I'd have preferred more on the codes side with less emphasis on the methods.

The session was closed with a conversation between Cal Evans, Ivo Jansch, Andrei Zmievski, Lorna Mitchell and Paul Reinheimer. This took the form of an interview by Cal and Ivo with their guests. It worked quite well, but I felt that the questions for Andrei and Paul were not as well structured as the ones for Lorna. There was a slide show running above their heads with Twitter and Flickr photos from the conference playing. This was very funny :)

In the evening, I went for a meal at a Pancake house that was very enjoyable and I got to see a little bit of Central Amsterdam!

Conclusion

Overall, The 2009 version of the Dutch PHP Conference was very successful. It's clear that it is being positioned to become a major conference, not just for Europe, but for the world with a significant focus on the advanced developer.

I will certainly be submitting to talk in 2010 - assuming I can think up some advanced topics to talk about!

Cheers!

WinPHP wrap-up thoughts

1st June 2009

This post is part of a series about my experiences building a PHP app for Windows Server 2008 and IIS 7 for the European WinPHP Challenge 2009 which is sponsored by iBuildings, Microsoft and Leaseweb.

The WinPHP challenge come come to an end and I'm fairly happy with how SuccesSQL has come. I wish I had realised exactly how little time I had in May though. Somehow I managed to forget that we had three family birthdays, a Christening and a weekend holiday to fit in during the month...

I already knew that PHP worked great on Windows and SuccesSQL again proves that PHP is truly cross-platform. The areas that I wanted to explore and learn a little about were running PHP on IIS and using the SqlSrv database connector.

I learnt a lot about setting up PHP in Windows. It's amazing how much easier it was the second time when I installed it on the Leaseweb server. The Web Platform Installer is very good as long as you remember to stop the IIS service before running it! It's also very odd that the installer doesn't install PEAR or PDO_ODBC. I'm very glad that it uses the exact same installer as available at windows.php.net, as it makes correcting these oversights trivial.

Fortunately, Juozas Kaziukenas also wanted to connect to SQL Server, so we collaborated on a Zend Framework Database adapter for SqlSrv on codepex at http://zfmssql.codeplex.com/. Please download and exercise that too as all bug reports (and patches) will be gratefully received!

Like Juokaz, I also really enjoyed the community aspect of this competition too. Twitter was alive with #winphp tweets and it felt as co-operative rather than competitive as we helped each other overcome obstacles.