rulururu

post Building a Modular Application in Zend Framework – Part 1

June 17th, 2009

Filed under: Code, PHP, Zend Framework — Brenton Alker @ 09:45

This is part one of a series exploring modular application development in Zend Framework. In this entry we’ll look at downloading and installing Zend Framework, especially Zend_Tool, on a Linux environment. We’ll start from the beginning so that future posts can build on a known environment. Throughout this post, the code snippets are copy/paste ready, so following along should be easy, just start in a new working directory.

Install Zend Framework

The first step is to actually get ZF, so start by downloading the package (about 40MB in total) into our working directory and extracting it.

wget http://framework.zend.com/releases/ZendFramework-1.8.4/ZendFramework-1.8.4.tar.gz
tar zxf ZendFramework-1.8.4.tar.gz

We’ll then create a symlink to provide an easy upgrade path (extract the new version and move the symlink), and an easier to remember directory name.

ln -s ZendFramework-1.8.4 ZendFramework

Install Zend_Tool

Creating an alias allows command “zf” to always point to the Zend_Tool shell script, so we can use the command line tool from wherever we need it.

alias zf=`pwd`/ZendFramework/bin/zf.sh

Now the installation is complete, we should be able to check what version of the framework we have just installed.

zf show version
# Zend Framework Version: 1.8.4

If this works, we’re ready to start creating our project. The documentation provides some alternate methods of setting up Zend_Tool, including setting it up in a windows environment.

Create the project

Once Zend_Tool is working, we can begin creating our project. For the exercise, we’ll call our project “aza” (A Zend Application). Using Zend_Tool, we create the basic structure for the project.

zf create project aza

This should produce a project structure that looks like this

Directory Listing of new project

Directory Listing of new project

Setup the web server

Finally, we can tell the Apache2 web server about our application by adding a VirtualHost to the server configuration. You will need to replace “/WORKING/PATH/” with the absolute path to the directory in which you are working (run pwd if you’re not sure).

<VirtualHost *:80>
    ServerName aza
    DocumentRoot /WORKING/PATH/aza/public
    <Directory /WORKING/PATH/aza/public>
        php_value include_path "/WORKING/PATH/ZendFramework/library"
        php_value magic_quotes_gpc 0
        php_value short_open_tag "on"
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Don’t forget to restart the web server to enable the site.

Test our page!

We should now be able to navigate to our site and be warmly welcomed to our new Zend Framework application!
We’ll stop here for now. In the next post, we’ll start looking at creating our first module.

Updates

2009-06-26
Updated for ZF Version 1.8.4

post PHP Quebec Wrap-up from a Conference Newb

March 11th, 2009

Filed under: PHP, Zend Framework, phpc — Brenton Alker @ 11:02

As I am in Canada for only 3 more weeks, and we don’t get too many PHP conferences in Brisbane, I took the opportunity to attended my first ever PHP conference. I thought I would share my thoughts, and maybe those more experienced can help me do it “better” next time.

After flying across the country we checked into the hotel, the Hilton Bonaventure in Montreal. The front desk apparently didn’t know we were there for the conference because we didn’t “book with the others”. I’m not sure what that means, or what difference it would have made? The hotel offered free wireless in the foyer and conference halls, but not in the rooms. Luckily, we were close enough to the foyer to access their wireless from our room (once I sorted out some driver issues on my laptop).

Day 1

After missing most of the openning keynote on the first day in favour of eating (I didn’t know breakfast would be available) I jumped into the sessions. Starting with Matthew Weier O’Phinney’s Practical Zend Framework Jutsu with Dojo, which provided a practical overview to an area of the Zend Framework I have been planning on investigating, but haven’t yet got round to. This was followed up by John Coggeshall’s explaining their process of Building RIA Application in PHP. This wasn’t a talk I intended to attend (Sara Goleman’s talk was scheduled for this time, but was unable to attend due to illness) but it was interesting to see the differences in building a PHP application without a HTML front end.

After lunch, Derick Rethan’s looked into search and indexing. Of Haystacks and Needles, introduced MySQL full text, Selenium and Solr. While I have used Selenium, Solr seems like a useful step up for systems with higher requirements. The afternoon sessions were A tour of MySQL High Availability by Morgan Tocker, which talked about the difference between scaling for performance and scaling for HA, and techniques for the latter, for me there wasn’t much I hadn’t used before, but some of the monitoring tools warrant further research. Stupid Browser Tricks by Sean Coates, was in a similar boat. It was a good introduction to some useful browser side tools (Firebug, YSlow! and Selenium IDE) but I had hoped for a deeper look into Selenium. Isn’t everyone using firebug by now? FirePHP is a nice addition though.

Day 2

Day 2 started with a quick breakfast (I’m a fast learner ;) ), then the PHP Code Review Part #1, with Sebastian Bergmann and Stefan Priebsch delving into some not particularly pretty examples of code from well known PHP applications such as Wordpress. It’s reassuring to know other people write and release bad code too :) I didn’t attend Part #2 of the session, where they took code samples from the audience and critiqued them. Instead I opted to drop in on Chris Hartjes expaining why Deployment Is Not A 4 Letter Word and that with some planning and appropriate tools, in your absence even the sysadmin should be able to deploy your application with confidence.

The pre-lunch keynote was John Coggeshall again discussing RIA in Beyond the Browser. Is the browser dead? Not yet, but it certainly has some growing competition. After a break, I listened to Ilia Alshanetsky talk about Premature Optimization Mistakes, focusing on optimising the server stack itself before delving into application level optimisation. Arguing it usually provides more results without the risk of breaking the application. PHP for the Enterprise then examined how PHP has reached a level where it is suitable for projects that were once considered the realm of “real” programming languages. Most of the talk discussed more technical details of scaling PHP to an enterprise level, such as database buffer sizes, performance monitoring and caching at various levels.

The day ended with the career fair which saw a number of, primarily local, employers (including the armed forces?) set up booths and discuss their work and employment with the potential candidates at the conference. While I wasn’t actively seeking employment, I did have a chat with some of the representatives. Given the location of the conference, it wasn’t too big a surprise that the majority were bilingual and in some cases French only offices.

Day 3

My final day of the conference started with Owen Byrne discussing Growing a Development Team While Building a Huge App at 500 miles/hour; which I attended, hoping to garner an insight into building a team and managing agile development on a large project. While the project was an interesting one, Owen seemed to be more interested in giving out t-shirts and I didn’t feel we got very deep into the whole process. Being a fairly heavy user and fan of Zend Framework, I joined Matthew Weier O’Phinney in his search for some of Zend Framework’s Little Known Gems. The talk was targeted at using the components in isolation and I discovered a number of components that may come in handy in future projects, with or without the MVC stack.

Morgan Tocker then talked further about MySQL, this time focusing on performance as opposed to high availability. There were a few points about the inner working of the InnoDB storage engine that got my attention, including some builds available from Percona we may need to look at.

The round-table Framework Comparison, featuring Fabien Potencier (Symfony) Derick Rethans (ezComponents) and Matthew Weier O’Phinney (Zend Framework) seemed to indicate that all 3 frameworks solve much the same problem, they even went as far as agreeing you should use components from the other frameworks when your primary framework doesn’t include one. Much different to the “my framework is better” “discussions” we too often see.

Finally, Chris Shiflett addressed Security-Centered Design: Don’t Just Plan for Security; Design For It provided an alternative look at some interesting security topics. Instead of focusing on technical details, he primarly focused on security from a user perspective as “user perception is as important as reality”. Giving examples of various recent attacks on high profile sites that, while not actually the fault of the site, would be perceived as such by most users. He also put forward ideas about using “ambient signifiers” to assist in the fight against phishing, and how the normal (request-reload) web model can mean important information is missed due to “change blindness” including a live demonstration. He suggesting AJAX might be a suitable solution in this case (as long as it’s still accessible of course).

Conclusions

All-in-all the conference was a great experience, the hotel was really nice (especially compared to some of the hostels where I usually slum on my travels) and the talks were wide ranging and generally well presented.

Some of the material I found too “introductory”, but I think that may be because I primarily attended sessions on topics I am familiar with hoping to learn more, whereas using the talks as introductions to new topics might have been a better idea. How do people usually select talks?

I also didn’t attend much in the way of “extra-conference” activities; social events outside of the conference schedule. I am a little disappointed about this, as it would have been good to chat to some members of the community in a less formal setting, so this is something I think I would do much more of next time. And there will be a next time.

post Environment specific bootstrapping for Zend Framework

February 13th, 2009

Filed under: Code, PHP, Zend Framework — Brenton Alker @ 09:40

When you build an application, it is often deployed to a number of servers each with a different set of configurations. The development and production servers, with different debug, logging, and database details is an obvious example. Many approaches I have seen to dealing with this, including Anis uddin Ahmad’s post that inspired this one, involve determining the  domain the application is running on, and loading a pre-determined configuration for that host.

There are 2 downfalls to that solution from my point of view;

  1. Each new environment involves editing the source of the application
  2. If the application involves command line components, there is no way to determine the domain, requiring a special case

My solution may look familiar if you have ever looked at python’s django. It involves using environment variables, which can be set through the web server, or on the command line – even in crontab. Basically, I set an environment variable specifying the location of the configuration file to load into the application. I actually allow a list of configuration files,so you can have a “base” and only override specific configuration as required per location.

To add and environment variable in apache, you use the mod_env module’s SetEnv directive.

SetEnv APPLICATION_CONFIG /path/to/config/basic.php:/path/to/config/development.php

on the command line, you can use export.

export APPLICATION_CONFIG=/path/to/config/basic.php:/path/to/config/development.php

and very similarly in crontab (above the code that requires it)

APPLICATION_CONFIG=/path/to/config/basic.php:/path/to/config/development.php

To read the configuration files, from php we can simple get the environment variable with getenv(), then merge each configuration into the applications configuration;


$configFiles = getenv('APPLICATION_CONFIG');

$configArray = explode(PATH_SEPARATOR, $configFiles);
$config = new Zend_Config(array(), true);
foreach ($configArray as $newConfig) {
&nbsp;&nbsp;&nbsp; $config-&gt;merge(new Zend_Config(require $newConfig));
}
$config-&gt;setReadOnly();

In this example I am using simple PHP files to store the configuration arrays, you could also use any other format Zend_Config can read. You could also use a simple array and array_merge() instead on Zend_Config if you are not using the framework.

ruldrurd
« Previous PageNext Page »
Powered by WordPress, Web Design by Laurentiu Piron Monitored by SiteUpTime
Entries (RSS) and Comments (RSS)