About the Author

Chris Shiflett

Hi, I’m Chris, a web craftsman making things like Mapalong & Brooklyn Beta with my friends at Analog.


Zend Framework Tutorial

I'm a little late announcing this, but I wrote a quick Zend Framework Tutorial for php|architect over the weekend. It was written in haste and likely contains some errors as a result, but I've arranged to have the tutorial updated frequently. Therefore, it should gradually improve as well as continue to remain relevant as the framework evolves.

The demo app from the tutorial is also available:

I hope you enjoy it.

About this post

Zend Framework Tutorial was posted on Tue, 07 Mar 2006 at 21:51:03 GMT. Follow me on Twitter.

25 comments

1.Rob Allen said:

I like it!

It's clear that you are a "writer", if that's what you consider to be "written in haste"! My efforts have taken blood and sweat to do the writing bit - the coding investigation was easy :)

Rob...

Tue, 07 Mar 2006 at 23:12:59 GMT Link


2.Richard Thomas said:

Thanks, I used your tutorial to take another look at the Framework and then added my own twist by extending the Framework to include gettext support

http://www.cyberlot.net/zendframeworkSecond

Tue, 07 Mar 2006 at 23:38:01 GMT Link


3.David Rodger said:

>It's clear that you are a "writer"

I think we just found one of the key reasons more people listen to Chris than to Stefan Esser!

Wed, 08 Mar 2006 at 05:08:22 GMT Link


4.Ian said:

Very nice tutorial :)

I'm suprised that someone actually said there was no MVC functionality in the new framwork which you so clearly proved wrong :)

I just wish it was more stable. I've got a project to use the Zend Framework on but I don't think it can wait until it is more stable :(

Wed, 08 Mar 2006 at 05:58:34 GMT Link


5.Jaydee said:

By far, this is the easiest MVC approach i've tried for PHP. but is it the framework or was it Chris' writing? Either way, can't wait for updates on the framework and will definitely start playing with it. Thanks for the great guide!

Wed, 08 Mar 2006 at 06:07:09 GMT Link


6.Eddie A. Tejeda said:

I wrote a very similar controller, which seems to be getting some traction.

http://users.visudo.com/eddie/PHP-Controller/

In many ways, it handles pages a lot more elegantly. Class is managed by an XML file, which controls the flow of the application, and does not require hard coding "header(Location:)". It also creates and ACL for the accessible page.

It supports aliases for URLs

Classes are reuseable in context which are not web related. For example, since output is managed externally, you can include a class that was a web accessible, in another class that is not.

The classes do not need to be extended and are more self contained.

GET/POST are handled much more elegantly (passes as function params) and make the class, again, more reusable.

- The next version (due out within the next 2 weeks) will allow for regular expression for URL names (to be handled by a single class)

And a few other things which I wont get into now.But I was not too impressed with this version. They implemented issues, which when I was designing my PHP-Controller, I made a concious effort not to implement. But maybe that will change in later versions.

Wed, 08 Mar 2006 at 17:22:31 GMT Link


7.Eddie A. Tejeda said:

Oh yea. And a debugger.

:)

Wed, 08 Mar 2006 at 17:47:34 GMT Link


8.Nate Klaiber said:

Greetings Chris (I feel like I am typing your name alot - but that should help rid some of the spam!)

Thanks for the example, I havent gotten around to completely testing it because I am using MySQL at the moment. But I do have one question. The zend framework is very similar to the CAKE PHP (in some aspects). One thing that I like about CAKE is that you have a layouts section for your views, and this allows you to have several layouts without using them in every template. Is this something that will come with future releases? Did I miss something?

Cake doesnt have PDO support, and I am in need of finding a framework with that support (in comes Zend) - but I also would like to control the layouts separately.

Thoughts?

Thanks,

Nate

Wed, 08 Mar 2006 at 20:10:50 GMT Link


9.Nate Klaiber said:

Chris,

I am using the framework for a small project right now - just to get a feel for everything. I was wanting to create a URL like the following:

/the-collection/member-benefits/

And then redirect it to /member-benefits/ (as some already will have this URL bookmarked).

However, I am unable to set a method for the member-benefits to make the appropriate redirect (Even tried MemberBenefits and memberBenefits). Does this make sense?

Just thought I would see if you had some insight...

Thanks,

Nate

Wed, 08 Mar 2006 at 20:21:30 GMT Link


10.Chris Shiflett said:

Thanks for the positive feedback. I'll be working on the second version of the tutorial this weekend.

Nate, unless I'm misunderstanding your question, it seems like you'll need to create a controller for the-collection whose only purpose is to issue the redirect or use a method in IndexController to handle that. Your controller for member-benefits should not be called.

(I'm not sure how the hyphens affect the names of the controllers, but a bit of experimentation should answer that.)

Fri, 10 Mar 2006 at 22:19:18 GMT Link


11.andre flitsch said:

Hi Chris,

Just thought that i would point out that there is an error in the tutorial on the phparch.com web site.

The class FooController should contain the methods:

indexAction() & barAction

instead of

index() & bar()

Stumped me for a while, maybe you can update tutorial?

Anyway, thanks for the great work!!

Andre

Sat, 11 Mar 2006 at 16:30:11 GMT Link


12.Peter Pistorius said:

Has anyone managed to rewrite this rule for lighttpd?

Lighttpd uses the PCRE engine.

RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

Syntax looks like this:

url.rewrite-once = ( "<regex>" => "<relative-uri>" )

Sat, 11 Mar 2006 at 22:12:26 GMT Link


13.Chris Shiflett said:

Hi Andre,

I've just sent an update to Marco (of php|architect), so hopefully he will publish the update very soon. It fixes a few mistakes, updates everything to work with 0.1.2, and hopefully does a slightly better job of explaining a few things.

Tue, 14 Mar 2006 at 05:43:13 GMT Link


14.Chris Shiflett said:

Hi Peter,

I've never used lighttpd, but do you know exactly what it matches against the PCRE? For example, is it just the request URI exactly as it exists in the HTTP request?

Tue, 14 Mar 2006 at 05:52:26 GMT Link


15.Peter Pistorius said:

For those of you that use lighttpd:

url.rewrite-once = ( "\.(gif|jpg|png|css|js)$" => "$0", "" => "/index.php")

Thu, 16 Mar 2006 at 14:56:42 GMT Link


16.Chris Shiflett said:

Thanks, Peter!

Thu, 16 Mar 2006 at 14:59:45 GMT Link


17.Peter Pistorius said:

Please excuse me, the above rewrite rule has an error, it should be written as such:

url.rewrite-once = ( ".*\.(js|gif|jpg|png|css)$" => "$0",

"" => "/index.php")

Mon, 20 Mar 2006 at 11:58:07 GMT Link


18.Rick Petersen said:

Hey, I'm most likely just missing something, and I know I'm commenting on a topic that hasn't had a comment in almost a month, but I've just finally really started looking into the Zend Framework.

Someone referred to Chris's tutorial as (documentation of) an easy implementation of the MVC pattern. I see a Zend_View and a Zend_Controller, but no Zend::Model. And as I said, I'm fully willing to accept that this is merely me missing something or perhaps my lack of knowledge regarding the MVC pattern, but this would seem to be an incomplete implementation of MVC. I have looked at Symphony, Cake, Mojave and SEVERAL other PHP-based frameworks, and they all seem to have the Model, the View and the Controller... Is there some reason the Zend framework does not need the Model part of this pattern?

From what I've read of the MVC, the models are the set of classes that model and support the underlying problem you're trying to solve. They should have only the business logic for the objects of your system, and should know (arguably) nothing about anything about the views or the controllers. Granted, you can still create a bunch of classes and take care (as you should) to keep display and data information abstracted as much as possible, and then use a database class as you designed along with views to handle those aspects... I guess I just feel uneasy since there is specifically a Zend_View and a Zend_Controller but not a Zend_Model which makes me feel as though there would be a disconnect.

Am I totally off here? I know that the MVC pattern has been modified in various ways throughout the years of use (introduction of the concept of Domain Models versus Application Models, the combining of the functionalities within the View and the Controller, etc) but I'm not really able to reconcile the current Zend Framework with a true MVC pattern.

Help?

Tue, 11 Apr 2006 at 16:18:51 GMT Link


19.Chris Shiflett said:

Hi Rick,

There's nothing to prevent you from using MVC without a framework at all. Frameworks can help make it easy and natural, but writing your own class for the model instead of using something provided by the framework doesn't mean you're no longer using the MVC pattern.

I'm not very impressed with the Zend Framework's current database components, and that's why I chose to ignore that part when writing the tutorial. Don't let the fact that the tutorial uses only a handful of components lead you to believe that those are the only ones available. Also keep in mind that this is a preview release of the framework with many components yet to be developed. See the roadmap for more:

http://framework.zend.com/roadmap

In summary, the Zend Framework provides all of the components for MVC. As stated in the tutorial, I chose not to highlight the database (model) components.

Hope that helps!

Tue, 11 Apr 2006 at 16:44:58 GMT Link


20.Rick Petersen said:

That does indeed help. Thanks again Chris.

btw, I was at ZendCon 2005 in SanFran and caught your talks on Security (both the pre-con one and the official session)... and have been reading here since. I enjoy your work quite a bit; keep it coming. </fanboi> :)

Tue, 11 Apr 2006 at 16:50:54 GMT Link


21.Chris Shiflett said:

Thanks, Rick. :-)

Tue, 11 Apr 2006 at 16:53:07 GMT Link


22.Philipp said:

Hello Chris,

I am wondering why does the tutorial is not including a model directory and classes inside ? Thanks

Thu, 06 Jul 2006 at 12:55:33 GMT Link


23.zibin said:

Chris,

I've dabbled with ZF for a weeks now. I actually have a question which I have always wanted to ask. Here it goes.

When we want to reference methods in other class, we first load the specific class with loadClass() and then simply call

ExampleController::ExampleMehod();

But why can't we do the below instead?

$exampleControllerObject = new ExampleController();

$exampleControllerObject->ExampleMethod();

Thanks again. I appreciate all the help.

Wed, 21 Feb 2007 at 10:07:43 GMT Link


24.anonymous said:

static is the keyword you are looking for

Thu, 04 Oct 2007 at 13:50:00 GMT Link


25.Joseph Crawford said:

404 not found :(

What's with this OpenID thing, you know how long it took me to figure out I had to register HAHA

Thu, 15 May 2008 at 00:59:15 GMT Link


Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.


Work and Books

Analog Essential PHP Security HTTP Developer's Handbook