Tutorial: Getting Started with Zend Framework 1.10

This tutorial is intended to give an introduction to using Zend Framework to write a simple database driven application. It has been updated for version 1.10 of the framework and takes full advantage of the Zend_Tool command line script and Zend_Application for bootstrapping. Other components used include Zend_Controller, Zend_View, Zend_Db_Table and Zend_Form.
This tutorial has been tested on version 1.10 of Zend Framework. It will not work on any version prior to version 1.10.
If you are deploying to a "user" site (e.g. http://localhost/~rob), then you need a RewriteBase line in your .htaccess file that looks something like: RewriteBase /~rob/zf-tutorial/public/.
English version: Dowload the PDF (v1.7.1).
Code
The associated code is available:
Further reading
Once you have finished this tutorial, consider buying my book, Zend Framework in Action. Also, the Quick Start is worth reading.
Chris Kirk has kindly provided a Q&A PDF for the 1.8 version of the tutorial which may help if you are having problems.
Previous versions of this tutorial
There have been three previous versions of this tutorial:
- tutorial for Zend Framework 1.8 and 1.9
- tutorial for Zend Framework 1.5, 1.6 and 1.7
- tutorial for Zend Framework 1.0
Changelog
- 1.7.1 (11/Feb/2010)
- Fix code in deleteAction to use the correct model name
- 1.7.0 (7/Feb/2010)
- Significantly revised for Zend Framework 1.10

February 8th, 2010 at 09:37 #
[...] Akrabat's blog: As a result, I have updated my Zend Framework tutorial so that it is completely current. The main change I made was to remove the _init methods in the [...]
February 9th, 2010 at 10:51 #
Thank you very much, great stuff, again.
One minor problem in the zip files though:
IndexController.php line 91 (deleteAction)
$albums = new Model_DbTable_Albums();
generates an error
forgot the 'Application_'?
greets,
el mich
February 9th, 2010 at 11:09 #
Thanks for letting me know. I've updated the zip files.
Regards,
Rob...
February 9th, 2010 at 14:33 #
thanks ... ever helping tutorial :)
February 10th, 2010 at 15:26 #
You'll better correct the pdf version number and the download link from 1.7.0 to 1.10.0 .
Regards,
Italo A.
February 11th, 2010 at 17:15 #
I'm getting a 'Class 'Application_Model_DbTable_Albums' not found in C:\wamp\www\zf-tutorial\application\controllers\IndexController.php'
after following up to page 12 of this tutorial!
Can you help?
February 12th, 2010 at 09:48 #
I'm new to object-oriented php and Zend Framwork and I found this tutorial very useful, thanks a lot.
I'm running this on the newest Zend Server CE as a virtual host and it works like a charm. :-)
February 13th, 2010 at 12:35 #
Error is happeing because class name is wrong
Solution
Rename Model_DbTable_Albums to Application_Model_DbTable_Albums in controllers/IndexController.php
February 13th, 2010 at 18:45 #
I believe the correct location for the layout.phtml file shown on pages 10 and 11 of the tutorial should be:
zf-tutorial/application/layouts/scripts/layout.phtml
February 13th, 2010 at 20:38 #
This is the tutorial Zend Framework newbies have been seeking! A complete working project interspersed with useful explanations.
Very well done, thanks!
iMac 10.6.2 Snow Leopard
MacPorts install of Apache, MySQL, PHP and phpMyAdmin.
February 14th, 2010 at 19:57 #
In your tutorial, the Application_Model_DbTable_Albums class could be created with the zf command line tool:
'zf create dbtable Albums albums'
February 15th, 2010 at 21:39 #
Thanks a lot for this tutorial, I was looking for some good and straight to the point explanations to start with Zend and this is pretty good stuff!
February 17th, 2010 at 12:24 #
Hi again,
It's been a few days I try to augment and modify this simple application to make a real (but still relatively simple) administration console.
This is a was also to get more familiar with Zend.
Sincerely it is a PAIN, when something does not work I just have "Application Error" displayed, or worse, most of the time, a white page!
Even if I code pure PHP from scratch will I have better indication of what is wrong when there is an error!
The question would be: where the hell do the error messages go?
(I am trying to simply upload a picture, rename it and move it to a folder I create on the fly, and resize it. But moving and renaming the file just does not work, and I tried many things I saw in tutorials etc... I am a bit disappointed as I expected this to be basic stuff for zend)
February 17th, 2010 at 23:57 #
Hi David,
I guess you are using the "production" setting, which has display_errors = 0. I suggest using the "development" enviroment setting instead.
check your index.php code
on line 8.
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
February 18th, 2010 at 13:23 #
Thanks MayorBrain! ok I changed to development, I guess it should be better like this.
Anyway when I do a echo "string" in the IndexController.php code I never see my "string" displayed when executing the code.. any idea where it goes?
Also a problem with form is that sometimes I press the submit button and it stays blocked on the page, like if it was loading something forever. I need to press the submit button a second time to have the form processed and go back to the index page. I think it is specific to the file upload I added in the form, and seems to happen when I upload 2 times the same file..
Has anyone ever seen experienced this?
As there is absolutely no message displayed (even in development) it is hard to know what is wrong..
And finally (and I promise I am going to stop here:),
-What development environnement would you recommand to develop with zend? Is there any way to have autocompletion of some sort, or do we have to look everytime at the manual for each parameters etc.. ?
I am on mac os, using dreamweaver to edit my php but I am thinking on moving to eclipse with php.
A tutorial to setup a good working environment for zend development would be awesome!
-What forum who be the best to adress those kinds of issues specifically with zend?
Thanks !
February 20th, 2010 at 13:11 #
I've met the same problem as David. In project created with Zend_Tool only Application Error message is shown without any details. I am sure I've set my APPLICATION_ENV to development. Can this be in case of custom .htaccess file in project directory? It just redirect all queries to /public/ since the baseurl part is not empty.
February 23rd, 2010 at 11:42 #
After 2 weeks with Zend, I found the learning curve pretty steep, especially because the supposedely simplest things won't ever work the first time.
After a bit of a struggle my zend back-end works fine locally. Then I want to upload it to the REAL web server because the client asks for it this week.
So I do it and bam! New errors, open_dir restriction. I replaced the following line in index.php:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
with simply:
set_include_path(realpath(APPLICATION_PATH . '/../library'));
it works!
At least I got rid of the open_dir restriction warnings.
But now when I try to use my back end, the first call to an action in any controller brings me a 404 file not found page!!
Why is that??
Every forum I find seems to speak of local configurations, where we should edit httpd conf to allow override all etc..
But is there any real case of people using Zend for a public website?
Any help would be greatly appreciated!
Thanks again
David
February 24th, 2010 at 21:01 #
Good tutorial. Better than the guide on the Zend site. Only noticed one minor error on page 8. "Count not find row $id" should probably be "Could not find row $id".
February 25th, 2010 at 10:53 #
Fatal error: Class 'Application_Model_DbTable_Albums' not found in C:\Program Files\Zend\Apache2\htdocs\my\application\controllers\IndexController.php on line 16
This is the error, though i have the 'Application_Model_DbTable_Albums' class written in application\models\DbTable\Albums.php and using same in IndexController.php
March 2nd, 2010 at 19:34 #
Thanks for this great tutorial, it was really usefull, I can now create a basic app with Zend Framework.
Is there some slightly more advanced tutorial that you could advise me?
March 4th, 2010 at 12:14 #
Can somebody please tell me how to implement modules in Zend 1.10 ?
I need a detailed tutorial because I have tried many on the web buty seem to be outdated.
March 7th, 2010 at 10:41 #
HiRob,
I'd reccomend you to tell something about setting up a virtual host in this tutorial. I've got the example application up and running quite easy but when I'd like to add more controllers and functionallity I ran into a lot of troubles and it was a heavy search to get things solved. After I've found the information about setting up a virtual host thing got easy.
But thanks, your tutorial was very helpfull to me!
Cheers!
March 8th, 2010 at 14:26 #
Crispijn,
Why? A virtual host is not required for ZF. I know this as I don't use them for local development :)
Regards,
Rob...
March 8th, 2010 at 20:15 #
Hi Rob,
I ran into trouble when I added custom controllers to my application. When I had the virtual host up and running these problems were solved... Don't know why, I'm not a ZF guru, but it was.
Could you tell me why I ran into trouble? The troubles where that all the request I've made at http://localhost/~myname/controllername/actionname I got a 404 error but your tutorial app was up and running so I guess that the overwrite setting was just as you suggested.
Regards,
Crispijn
March 9th, 2010 at 08:40 #
Hi,
beautiful guide, but I've this error:
Fatal error: Class 'Application_Model_DbTable_Albums' not found. But the class name is correct (Application_Model_DbTable_Albums). What's the problem? I've used the downloadable example..
I've a lot of problem with a modules.. with a framework 1.10 the project's structure is significantly changed..
Thank you!
March 11th, 2010 at 20:06 #
Great! Does anyone know how to extend this tutorial with active record validation?
I am trying to inplemend Zend_Validate_Db_NoRecordExists so that it won't be possible to insert a new record while a record with an identical name field already exists. This works fine in case of inserts, but When the form is used to edit a record it will find itself and fail validation.
I know I need to set an exception for th active record. The problem is that I simply don't know how to pass a variable (id) from the controller into the form.
It should be pretty basic OOP I guess but I tried everything I could think off for more than a few nights now. I just don't see it. The manual describes this validator but somehow I can't manage to set it up. All I want is to pass a id from the controller to the form and use it.
My form currently uses the init() method. I also tried overriding the constructor __construct($id) as well but that's no good practice as far as I am aware and it did broke the 'save' method. The init() function is for convenience according to the documentation. But I have no idea what they mean. Then there's is this configuration object which can be passed to the constructor. And there is a setAttrib method. I am completely lost.
Any suggestions on using a constructor in stead of init() functions as in this tutorial? Could I use setAttrib for this purpose? Or implementing getter setter methods? Any help would be greatly appreciated!
March 13th, 2010 at 12:03 #
SELECT * FROM emp WHERE ename='smith' AND id=520
please tell me how to implement this qury in zend frame work
March 14th, 2010 at 17:51 #
Thank you for the tutorial.
When I'm trying to delete a record I get following
Fatal error: Class 'Model_DbTable_Albums' not found in \application\controllers\IndexController.php on line 91
(File from source downloaded)
This line should read:
$albums = new Application_Model_DbTable_Albums();
Regards.