Tutorial: Getting Started with Zend Framework 1.5

This tutorial is intended to give a very basic introduction to using the Zend Framework to write a simple database driven application.
English version: Dowload the PDF (v1.5.2).
Code
The associated code is available:
- Zip file of tutorial (~9KB)
- Zip file of tutorial including ZF 1.5.0 (~2.3MB)
- My Subversion repository
Further reading
Once you have finished this tutorial, Pádraic Brady is in the process of writing a very comprehensive blog tutorial that is well worth going through. Or just buy the book!
Previous versions of this tutorial
The original tutorial for Zend Framework 1.0 is still available, along with the translations for it.
Changelog
- 1.5.2
- Use a screenshot for the directory layout.
- 1.5.1
- Fixed minor mistakes found by Jason Qi and Źmicier.
- 1.5.0
- Significant revision to take advantage of Zend Framework 1.5
- 1.4.0
- Updates to support version 1.0.0. RC1 of the Zend Framework.
- 1.3.0
- Significant reworking to support version 0.9 of the Zend Framework.
- 1.2.0
- Updated tutorial for version 0.6 of the Zend Framework.
- 1.1.0
- Updated tutorial for version 0.2 of the Zend Framework.
- 1.0
- Original version

March 17th, 2008 at 19:38 #
Hallo Rob,
I use your Tutorial: Getting Started with Zend Framework 1.5 on my local server works very good and if i load it on the server of my provider i get this error:
———–
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, support@one.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
—————
http://www.dynascript.nl/zf-tutorial/public/index.php
Regards,
Wil
March 17th, 2008 at 20:12 #
Wil,
I think you are going to have to see the logs to work out what's going on.
Regards,
Rob…
March 17th, 2008 at 21:15 #
I have sent mail to support.one.com and they say their server don't support zend framework. but their server support php5.2.5.
March 17th, 2008 at 22:41 #
Hello Rob…
just yesterday began with the old tutorial and i was thinking that it's been some time since 2006 :P. Anyway I'm new to zf and you're giving me a big hand with your work. Thanks.
p.s. i need to integrate ext JS with zend can you please give me a start point?
pp.s. thanks again
March 17th, 2008 at 22:48 #
Hi Raz,
The old tutorial had been pretty well debugged by version 1.4.5, so didn't need much in the way of updating regularly.
However Zend Framework 1.5 brings some very useful new features with Zend_Layout and Zend_Form that a new version was most certainly required in order to understand how useful ZF is.
Regards,
Rob..
March 18th, 2008 at 04:06 #
WIL,
I had the same problem, its due to the .htaccess file. Here is the response that worked for me. The log file reads: "Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration."
The solution:
"It sounds like you don't have mod_rewrite loaded.
If it's your server, open the httpd.conf file and look for these lines:
#LoadModule rewrite_module modules/mod_rewrite.so
#AddModule mod_rewrite.c
If you see them, simply remove the # from the beginning and then save the file. If you don't see them, put them in there, without the #, of course.
You will need to restart Apache for the change to take place."
You should read what it says in the log files.
"
March 18th, 2008 at 04:07 #
Also, this tutorial was great. Got me started on the right direction and understanding how the MVC system works. I love it how you don't need to name any files, you see no file extensions when using the site, it's mad.
March 18th, 2008 at 20:01 #
Hallo Rob, could you help me with this issue. If i go to this url http://www.dynascript.nl/public/index.php then everything works good but i want if the user go to this http://www.dynascript.nl then the site works too.
Regards,
Wil
March 18th, 2008 at 20:27 #
O sorry i mean if the user go to this http://www.dynascript.nl then i get http error 403.
March 18th, 2008 at 21:52 #
Thanks Rob, hope you'll upgrade the Zend_Auth tutorial ;)
March 19th, 2008 at 21:26 #
Wil,
You'll need to move index.php to the root.
Regards,
Rob…
March 19th, 2008 at 21:26 #
Mark,
Glad you found it useful :)
Regards,
Rob…
March 19th, 2008 at 21:27 #
Turist,
All in good time… I'm a bit snowed under at the moment though!
Regards,
Rob…
March 20th, 2008 at 09:11 #
After install tutorial and open in browser i have err, what is it? help me pls, mysql in other php applications good work.
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'The mysql driver is not currently installed' in C:\www\rasprodagafront\library\Zend\Db\Adapter\Pdo\Abstract.php:104 Stack trace: #0 C:\www\rasprodagafront\library\Zend\Db\Adapter\Abstract.php(390): Zend_Db_Adapter_Pdo_Abstract->_connect() #1 C:\www\rasprodagafront\library\Zend\Db\Adapter\Pdo\Abstract.php(206): Zend_Db_Adapter_Abstract->query('DESCRIBE 'album…', Array) #2 C:\www\rasprodagafront\library\Zend\Db\Adapter\Pdo\Mysql.php(137): Zend_Db_Adapter_Pdo_Abstract->query('DESCRIBE 'album…') #3 C:\www\rasprodagafront\library\Zend\Db\Table\Abstract.php(605): Zend_Db_Adapter_Pdo_Mysql->describeTable('albums', NULL) #4 C:\www\rasprodagafront\library\Zend\Db\Table\Abstract.php(534): Zend_Db_Table_Abstract->_setupMetadata() #5 C:\www\rasprodagafront\library\Zend\Db\Table\Abstract.php(269): Zend_Db_Table_Abstract->_setup() #6 C:\www\rasprodagafront\application\controllers\IndexController.php(8): Zend_Db_Table_Abstract->__construct() #7 C:\www\rasproda in C:\www\rasprodagafront\library\Zend\Db\Adapter\Pdo\Abstract.php on line 104
March 20th, 2008 at 17:20 #
ОтÐ'ых,
Try installing the PDO and PDO_MYSQL PHP extensions.
Regards,
Rob…
March 20th, 2008 at 18:53 #
Rob thank you.
May be you know, why if i click at any link in that script(tutorial), browser open that page "Object not found Error 404″.
March 20th, 2008 at 19:40 #
Mister Olympia,
Two choices:
1. Your .htaccess isn't being read
2. mod_rewrite needs enabling in httpd.conf
Regards,
Rob…
March 20th, 2008 at 20:54 #
Wow! Someone else named Wil who spells it with one 'l'. So this is what it sounds like. . . when doves cry. (see 'Lemons of Troy' episode of the Simpons).
,Wil
March 20th, 2008 at 21:00 #
Hallo rob thanks for you reaction, I have a problem, your tutorial works very good.Now i want to load an other controllerclass (categoryController) with the indexcontroller thus both at the same time. I want to display categoryController (indexAction) as a menu on the top en the indexcontroller (indexAction) on the body of the website.
How can i solve that
Regards,
Wil
March 20th, 2008 at 21:06 #
Wil,
You need to use the ActionStack. Have a look at my Simple Zend_Layout Example post for ideas.
Regards,
Rob…
March 20th, 2008 at 21:07 #
Wil Sinclair,
It's not that uncommon over this side of the pond. At least, I know a couple of guys who use the single l version :)
Regards,
Rob…
March 20th, 2008 at 21:08 #
Wil,
Hmm… not that entry!
I was thinking of chapter 4 of Zend Framework in Action!
Sorry,
Rob…
March 20th, 2008 at 21:14 #
I have bought jesterday your book, I have received pdf and in September 2008 (est.) i wil receive print book, Ok i wil read right now chapter 4.
Thanks
March 24th, 2008 at 13:23 #
Hai Rob,
Could you tell me
How i can execute an action of classcontroller within an other controller.
For examle i want to execute this function messagesAction($num) in other controller.
Note: messageAction have a parameter.
Thanks
March 24th, 2008 at 13:41 #
noury,
_forward().
Regards,
Rob…
March 24th, 2008 at 14:03 #
I have an action with a parameter. how can i pass that parameter to my acion with _forward.for example: messagesAction($num);
thanks
March 24th, 2008 at 16:00 #
noury,
$this->setParam(num, $num);
Regards,
Rob…
March 25th, 2008 at 23:58 #
Hi, I'm a new to Zend and PHP, and trying to learn it from your tutorial. Currently I'm using XAMPP and downloaded your tutorial. I have configured the application/config file to access MySQL and created the table and entries with the exact same names. But when running the webapp I got the following error:
Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'zftest.albums' doesn't exist'
I see the albums coming from the model/Albums.php. Why it can not map it?
I have no idea what else needs to be modified to make it run… Any suggestion would be really appreciated.
Thank you.
Monika
March 26th, 2008 at 12:35 #
Monika,
You haven't created the database table within MySQL.
Regards,
Rob…
March 28th, 2008 at 11:11 #
Hi there, first of all, your tutorial is great. I have a little problem though: when I submit the changes, the _redirec('/') part does not work. I get this error message:
Cannot send headers; headers already sent…
Now what? :)
Thanks
March 28th, 2008 at 11:24 #
Falcon,
Have you googled?
Regards,
Rob…
March 28th, 2008 at 11:54 #
Rob, sure I did, didn't want to look totally noob :) Despite my efforts, I look. So, all I have found is that nothing should be prior to the header information that is redirecting, and this I have already known. What I don't know is how this all works if I use Zend. Am a bit confused, though I'm trying to figure it out.
Respect
Falcon
March 29th, 2008 at 07:42 #
Please see "Naming Recommendation for Controllers, Actions, and URLs using zend_controller" at http://www.nabble.com/Re%3A-Naming-Recommendation-for-Controllers%2C-Actions%2C-and-URLs-using-zend_controller-p16366498.html
where the zend developer says:
Your controllers and actions can have camelCasing to accomodate multiple words
This is a clarification on some of the advice that you have in your tutorial.
March 29th, 2008 at 09:04 #
Dennis,
Whilst you are correct that it is possible to use camel casing in action names, make sure you have read and understood the note on http://framework.zend.com/manual/en/zend.controller.basics.html:
Also, camel cased action names were the biggest cause of upgrade problems from 1.0 to 1.5. So much so that it is mentioned in the migration notes here: http://framework.zend.com/manual/en/zend.controller.migration.html#zend.controller.migration.fromoneohtoonefive.
The upshot is that camel casing action names is fine as long as you use hyphens in your URLs and view script filenames.
Regards,
Rob…
March 29th, 2008 at 19:22 #
Rob,
Thanks for the pointer to the docs on camelCase. Casing is discussing in multiple places and I missed that one.
BTW, your link has an extra character at the end (:) so the link should be http://framework.zend.com/manual/en/zend.controller.basics.html
March 29th, 2008 at 19:55 #
Thanks Dennis,
Don't you just hate auto-linking at times?!
Rob…
March 31st, 2008 at 08:55 #
Hi,Rob
The index page is work properly, but
the url: http://http://localhost/zf_tutorial-150-with-zf/public/index/add shows:
The requested URL /zf_tutorial-150-with-zf/public/index/add was not found on this server.
April 1st, 2008 at 07:45 #
Pretty much the same problem as kyia it gives me the classic err that it cannot find the add controller though the controller is good and index works. Im a bit confused on what i should put in add.php and edit.php
April 1st, 2008 at 08:13 #
Oh and by the way stuff never works when i was your version of .htaccess but if i use the default zend router from the doc i get index working help a brother out
April 1st, 2008 at 10:50 #
Scratch that i got it working but i'd still like to know why .htaccess doesnt work
April 3rd, 2008 at 17:44 #
ATTENTION ALL:
v.1.5.1 of this tutorial has a serious typo!
The directory structure should have application, public, & library all within the same level. The should all be in zf-tutorial.
public and library DO NOT GO INSIDE THE APPLICATION FOLDER!
It should be:
zf-tutorial
-application
–controllers
–models
–views
-library
-public
–css
–images
–js
April 3rd, 2008 at 17:48 #
To restate for better visibility:
The directory structure should be such that you have:
zf-tutorial/application/controllers/IndexController.php
zf-tutorial/application/models
zf-tutorial/application/views
zf-tutorial/application/views/scripts/index/add.phtml
zf-tutorial/library/Zend
zf-tutorial/public/css
zf-tutorial/public/images
zf-tutorial/public/js
zf-tutorial/public/index.php
zf-tutorial/public/.htaccess
April 3rd, 2008 at 19:20 #
I've been struggling with the indentation of the folders. I should probably do a screenshot!
Rob…
April 4th, 2008 at 07:10 #
dear sir,
i'm is student from indonesia and i'm is beginner in php. i have project from my school, how to connect zf with 2 different db. for master data i'm using ms. sql server and save data to mysql.
thx's for your help.
eko
April 5th, 2008 at 21:57 #
Hi Rob, thanks for this upgraded tutorial.
It's a great help for me as a ZF Newbie.
I however have a problem with the inclusion of the css stylesheet file in my own application, which I chaged to use layout as of today.
The Baseurl helper seems to return nothing.
It looks as it is called, but the getBaseUrl returns nothing.
Here is the code of BaseUrl.php
getBaseUrl();
}
}
?>
What went wrong ?
April 8th, 2008 at 21:28 #
Rob, I open up my IE, I am getting:
set('config', $config); // setup database $db = Zend_Db::factory($config->db); Zend_Db_Table::setDefaultAdapter($db); // setup controller $frontController = Zend_Controller_Front::getInstance(); $frontController->throwExceptions(true); $frontController->setControllerDirectory('../application/controllers'); Zend_Layout::startMvc(array('layoutPath'=>'../application/layouts')); // run! $frontController->dispatch();
New to PHP, please help!
April 9th, 2008 at 14:12 #
Hi Rob.
Thank you very much for this great tutorial. It was very helpful for me as a beginner in developing with the Zend Framework. Big THX. Keep it up!
April 10th, 2008 at 15:27 #
Hi Rob,
First of all, thanks for all this great tutorials!
I was wondering why you don't put the layout directory in /application/views? Maybe you have a good reason, hope I'm not been imprudent lol.
April 11th, 2008 at 01:53 #
Wow, i'm impressed. This is the best introduction tutorial to a MVC framework i've ever seen. Most framework tutorials don't give fully information to get started like how to use layouts (or partials) to prevent duplicate coding. Thanks and keep up the good work, i'll buy your book.
April 11th, 2008 at 13:33 #
Thanks. Quick, easy to understand, just the best ZF newbie tutorial I've seen so far!
April 13th, 2008 at 15:44 #
I've gotten cake, symphony, and ap3 skeleton to "just work". Can't get zend to "just work". Can't get your tutorial files to work (1.0,1.3, nor 1.41)…. I had a list of the problems I've been having … but I'll try 1.5.2 just to be sure….
…do you think I'm mentally retarded? I mean, I've been trying to get zfw to work on my localhost since…gosh, since framework.zend.com went live! Haven't gotten it to work yet…haven't gotten your tutorial files to work either. Am I stupid. Somebody fill me in. wtf?
April 13th, 2008 at 16:27 #
my setup and my problems
http://localhost/zfw/ is where I put your files
When I go to this address I get:
Forbidden
You don't have permission to access /zfw/public/ on this server.
So I comment out the rewrite lines of the .htaccess file in zfw/public
Now I get a fatal error and stack trace because I don't have PDO.
(I don't have pdo because I try to match our live server which also doesn't have PDO and won't be upgraded for another year or 2)
So I edit the config file and change the db adapter to MYSQLI.
I recreate the zftest database, album table, user rob with password 123456.
I'm pretty sure with version 1.41 of your tutorial my problem was with the Album model class or with the database connection itself. But I couldn't figure out how to troubleshoot the album object. I'd usually start by var_dump-ing or print_r-ing the object but somewhere in the creation of the "new Album" the app would die without generating any kind of error (display errors was on and error reporting was set to E_ALL)
Now I get a different fatal error:
Fatal error: Uncaught exception 'Zend_Db_Adapter_Mysqli_Exception' with message 'The Mysqli extension is required for this adapter but the extension is not loaded' in W:\www\zfw\library\Zend\Db\Adapter\Mysqli.php:280
So I try changing the adapter to MYSQL:
Fatal error: Uncaught exception 'Zend_Exception' with message 'File "Zend\Db\Adapter\Mysql.php" was loaded but class "Zend_Db_Adapter_Mysql" was not found in the file' in W:\www\zfw\library\Zend\Loader.php:88
Took a look in Zend\Db\Adapter\ and there actually is not a file named Mysql.php
So I don't have pdo or mysqli and zfw doesn't want to use mysql_ …and now I don't know where to go from here.
April 14th, 2008 at 15:13 #
ptoom,
You have to have either the mysqli or pdo extensions installed.
Regards,
Rob…
April 15th, 2008 at 03:56 #
Hey Buddy, this tutorial is great. Thank you, terima kasih, gracias, whatever lah it's the same point.
April 15th, 2008 at 03:56 #
xHTML results…
Your wonderful example does not create xHTML. I found this bit …
$viewRenderer->view->doctype('XHTML1_STRICT');
But i have no idea where to place in with the structure of your example code.
Can you give guidance?
Thx
April 15th, 2008 at 04:43 #
on "$frontController->setBaseUrl ();"
why can't I place a full URL [http://mydomain.com/path/] in this property?
When I do, none of the links within this demo works. They look right when I view source, but they don't go anywhere.
Walter
April 15th, 2008 at 07:17 #
Walter,
The BaseUrl is the bit after the server name. In this case, "/path"
Regards,
Rob…
April 15th, 2008 at 15:35 #
Rob, yes, I gathered that; then how do I get the "server name" into the paths on the generated links?
April 15th, 2008 at 20:45 #
Why do are my menu links set to visited while using this tutorial?
above the content div i have:
<a href="url(array('controller'=>'index','action'=>'index'));?>"> Hem
<a href="url(array('controller'=>'index','action'=>'items'));?>"> Varor
<a href="url(array('controller'=>'index','action'=>'info'));?>"> Info
April 15th, 2008 at 20:48 #
that ctrl v was not so successfull :(
<a href="url(array('controller'
=>'index','action'=>'index'));?>"> Hem
<a href="url(array('controller'
=>'index','action'=>'items'));?>"> Varor
<a href="url(array('controller'
=>'index','action'=>'info'));?>"> Info
April 15th, 2008 at 20:50 #
omg!
ok i describe the setup with words-
one headerwrapper div with an header div inside.
the above mentioned links all have a menu div each.
April 16th, 2008 at 17:20 #
Hi Rob,
I will translating your tutorial to german and publishing in my blog. Is it okay?
April 16th, 2008 at 20:06 #
guenti,
Sure!
Regards,
Rob…
April 16th, 2008 at 20:31 #
Rob,
fine! This tutorial is one of the best that I've found in the www, and it's up to date. If I have published the german version, I will communicate it to you.
April 22nd, 2008 at 17:57 #
If you're having problems with the Zend_View_Helper_BaseUrl Class because it returns nothing and you're running this Tutorial in a VHost or Rootdirectory of your Apache try it that way:
setBaseUrl('.');
return $zcf->getBaseUrl();
}
}
?>
April 23rd, 2008 at 08:06 #
Excellent tutorial! I'm new to ZF and been playing around with it for the last week or so. Found a bug in your BaseUrl.php, though. If you include "index.php" in your browser's address bar, e.g., http://example.com/zf_tutorial/index.php, then baseUrl() returns "/zf_tutorial/index.php" which breaks the usage of it, notably in:
<link rel="stylesheet" type="text/css" media="screen" href="baseUrl();?>/css/site.css" />
which returns
Here's my fix:
function baseUrl()
{
$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
return str_replace('/index.php', ", $baseUrl);
}
April 23rd, 2008 at 08:11 #
Whoops…that got broken. Lemme try again:
Excellent tutorial! I'm new to ZF and been playing around with it for the last week or so. Found a bug in your BaseUrl.php, though. If you include "index.php" in your browser's address bar, e.g., http://example.com/zf_tutorial/index.php, then baseUrl() returns "/zf_tutorial/index.php" which breaks the usage of it, notably in:
<link rel="stylesheet" type="text/css" media="screen" href="baseUrl();?>/css/site.css" />
which returns
Here's my fix:
function baseUrl()
{
$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
return str_replace('/index.php', ", $baseUrl);
}
April 29th, 2008 at 22:14 #
I'm having the same problem as Kyia, localhost/zf-tutorial/ works just fine, but localhost/zf-tutorial/index/add doesn't, all I get is a 404 not found error.
I've checked and rechecked the .htaccess file, checked the httpd.conf (mod_rewrite is installed), there's nothing in the logs that indicates why it's not working. Any suggestions?
April 30th, 2008 at 06:52 #
mdrisser,
AllowOverride is not set to All
Regards,
Rob…
April 30th, 2008 at 07:03 #
Hi ,
I have followed the ZF tutorial and implemented it ..
Iam able to see the index page but unable to see other pages ..
Like I able to see http://localhost/gen gives me the index page
but http://localhost/gen/index/add is showing me 404 error page not found .. though I have the .phtml file and addAction() in IndexController.php file
please help me in this regard ..
-
Trinath
May 2nd, 2008 at 17:50 #
Thanks
But how can i install pdo extensions
May 7th, 2008 at 09:46 #
@Trinath, did you check your apache config?
Search for something like this:
Options FollowSymLinks
AllowOverride None
Make it:
AllowOverride All
May 8th, 2008 at 14:05 #
Pretty nice :)