Zendframework tutorial : Developing a Blog using version 1.9
I have been trying to work with zendframework 1.8 and now with 1.9 . I have created a simple blog application using zend framework ( You may call it as a zend framework tutorial
. Changing name from "A simple Blog tutorial using Zend framework 1.9" to Zendframework tutorial : Developing a blog for some search engine optimization technique ) . When I say a blog application with zend framework , never expect it , something like wordpress . Its a simple application which can add posts , add comments , can fetch albums from Picasa now , which I developed to learn Zend framework .
As I am new to zendframework and MVC , I know there is errors and some are for my convienience I have placed as it is I am new to unit testing , so have not written any code for test data too . sorry . I know the drawbacks of this application and the database itself is not proper . But I am publishing it if this helps some one to understand Zend_ACL , Zend_Auth , Zend_Form etc. I have added a gallery to get the albums , photos from picasa using the Zend_Gdata API . Thinking to implement to add photos through admin side . Need to implement the feature in admin . Will do it if I get some time 
I have looked various blogs and some are upto date applications and some are not . I am using the Zend Toolkit as I love to work in command line rather than GUI . I have added the link to download the complete zend framework 1.9 blog application example . Feel free to grab the complete working example code of the zend framework 1.9 from github to which a link is given to the last of the document .
Development Environment :
1 ) LAMP or WAMP or MAMP
2 ) Zendframework 1.9 configured .
3 ) I am using eclipse , netbeans and zend studio i5 . If you are using zend studio i5 , there is no need to look the command line, many are already in it .
I hope you have configured the first two . Now lets move to command line to create project , controllers and actions . The dollar ( $ ) symbol represents the command prompt .
$ zf create project blog
This will create the zend framework directory structure > 1.8 . Change the directory . ie cd <projectname>
hari@hari-laptop:/var/www/blog$ zf show profile
ProjectDirectory
ProjectProfileFile
ApplicationDirectory
ConfigsDirectory
ApplicationConfigFile
ControllersDirectory
ControllerFile
ActionMethod
ControllerFile
ModelsDirectory
ViewsDirectory
ViewScriptsDirectory
ViewControllerScriptsDirectory
ViewScriptFile
ViewControllerScriptsDirectory
ViewScriptFile
ViewHelpersDirectory
BootstrapFile
LibraryDirectory
PublicDirectory
PublicIndexFile
HtaccessFile
TestsDirectory
TestPHPUnitConfigFile
TestApplicationDirectory
TestApplicationBootstrapFile
TestLibraryDirectory
TestLibraryBootstrapFile
This is how the directory structure will appear .
zf create controller posts
Creating a controller at //var/www/blog/application/controllers/PostsController.php
Creating an index action method in controller posts
Creating a view script for the index action method at //var/www/blog/application/views/scripts/posts/index.phtml
Creating a controller test file at //var/www/blog/tests/application/controllers/PostsControllerTest.php
Updating project profile '//var/www/blog/.zfproject.xml'
zf create action view posts
Creating an action named view inside controller at //var/www/blog/application/controllers/PostsController.php
Updating project profile '//var/www/blog/.zfproject.xml'
Creating a view script for the view action method at //var/www/blog/application/views/scripts/posts/view.phtml
Updating project profile '//var/www/blog/.zfproject.xml'
zf create action edit posts
Creating an action named edit inside controller at //var/www/blog/application/controllers/PostsController.php
Updating project profile '//var/www/blog/.zfproject.xml'
Creating a view script for the edit action method at //var/www/blog/application/views/scripts/posts/edit.phtml
Updating project profile '//var/www/blog/.zfproject.xml'
zf create action add posts
Creating an action named add inside controller at //var/www/blog/application/controllers/PostsController.php
Updating project profile '//var/www/blog/.zfproject.xml'
Creating a view script for the add action method at //var/www/blog/application/views/scripts/posts/add.phtml
Updating project profile '//var/www/blog/.zfproject.xml'
zf create action login index
Creating an action named login inside controller at //var/www/blog/application/controllers/IndexController.php
Updating project profile '//var/www/blog/.zfproject.xml'
Creating a view script for the login action method at //var/www/blog/application/views/scripts/index/login.phtml
Updating project profile '//var/www/blog/.zfproject.xml'
zf create action logout index
Creating an action named logout inside controller at //var/www/blog/application/controllers/IndexController.php
Updating project profile '//var/www/blog/.zfproject.xml'
Creating a view script for the logout action method at //var/www/blog/application/views/scripts/index/logout.phtml
Updating project profile '//var/www/blog/.zfproject.xml'
Create a database and add the tables users , comments and posts .
CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Post_id` int(11) NOT NULL, `Description` varchar(300) NOT NULL, `Name` varchar(200) NOT NULL, `Email` varchar(250) NOT NULL, `Webpage` varchar(200) NOT NULL, `Postedon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `posts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Title` varchar(250) NOT NULL, `Description` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Username` varchar(200) NOT NULL, `Password` varchar(250) NOT NULL, `Role` varchar(10) NOT NULL, `Name` varchar(200) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Add the code to Bootstrap.php so the other classes can be autoloaded . Same like __autoload() .
protected function _initAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
return $moduleLoader;
}
- blog /
- php /
- tutorial /
- zend /
- zendframework /
pretty helpful... thanks for the post...
login and logout are super good, but I can not show me the post and comments
@David Thanks for your comment . Many of the things are learned from different blog posts and zend itself .
For the post and comments can you make it clear . I didnot get you .
There are three type of users for now . user , blogger and admin . Have not added much options for admin . I want to work on it to make this some more beautiful .
The user can post comments . The blogger and admin can edit the posts . I hope this is what you was asking .
*I need to work more on it to make everything dynamic . Will update the code to github after completing it .
I can't find the login password!
Usernames : user , blogger , admin .
Password : 123456
Sorry guys I found a small bug on line 7 of view.phtml . Please change the small letter 'u' to caps 'U' so will be baseUrl .
Fixed in the repository :) .
Please help!!! I am stuck When I run the tutorial website using the ZendFramework, I am getting this error, "The mysql driver is not currently installed". There are no errors in my Apache error logs. I am using: Apache2.2 PHP5.3 latest ZendFramework version Below is the error I am getting: Error Message: The mysql driver is not currently installed Stack trace: #0 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Adapter\Pdo\Mysql.php(96): Zend_Db_Adapter_Pdo_Abstract->_connect() #1 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Adapter\Abstract.php(448): Zend_Db_Adapter_Pdo_Mysql->_connect() #2 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('DESCRIBE `album...', Array) #3 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Adapter\Pdo\Mysql.php(156): Zend_Db_Adapter_Pdo_Abstract->query('DESCRIBE `album...') #4 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Abstract.php(814): Zend_Db_Adapter_Pdo_Mysql->describeTable('albums', NULL) #5 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Abstract.php(857): Zend_Db_Table_Abstract->_setupMetadata() #6 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Abstract.php(964): Zend_Db_Table_Abstract->_setupPrimaryKey() #7 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Select.php(100): Zend_Db_Table_Abstract->info() #8 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Select.php(78): Zend_Db_Table_Select->setTable(Object(Model_DbTable_Albums)) #9 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Abstract.php(1000): Zend_Db_Table_Select->__construct(Object(Model_DbTable_Albums)) #10 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Db\Table\Abstract.php(1286): Zend_Db_Table_Abstract->select() #11 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\application\controllers\IndexController.php(16): Zend_Db_Table_Abstract->fetchAll() #12 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Controller\Action.php(513): IndexController->indexAction() #13 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('indexAction') #14 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #15 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch() #16 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\library\Zend\Application.php(346): Zend_Application_Bootstrap_Bootstrap->run() #17 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\zf-tutorial\public\index.php(26): Zend_Application->run() #18 {main} Request Parameters: array ( 'controller' => 'index', 'action' => 'index', 'module' => 'default', ) Please help!!
How did you configured the PHP , MySQL and Apache ?
I think you may have done seperately not like installing WAMP or XAMPP . If so refer the link http://in2.php.net/manual/en/book.pdo.php may need to configure it .
If you are using the WAMP you can activate the pdo driver . I don't exactly have in my machine to tell how you can do . A quick google search may help you in that.
Alternatively you can use XAMPP http://www.apachefriends.org/en/xampp.html which comes with the zend framework already in it . Hope this helps you .
I installed PHP, Mysql, and Apache separately. I am using windows. I am getting this error, "The mysql driver is not currently installed". How do I installed and configure the PDO MYSQL Driver in windows. I already enable these and other extensions in my PHP.ini file; extension=php_pdo.dll extension=php_mysql.dll extension=php_mysqli.dll extension=php_pdo_mysql.dll extension=php_pdo_sqlite.dll Do I have to do something in phpForApach.ini file?? Help!!!
If you have done the steps ( http://in2.php.net/manual/en/pdo.installation.php ) I hope you have configured it and may not have restarted the apache .
Please consider looking the php.net mannual if you are still having trouble . I am not using Windows in my system . So I am really sorry to tell how it works . Consider installing XAMPP if you still have the trouble or move on to ubuntu or some other distro of GNU/Linux which can install and configure much more easily like XAMPP :). Debain based OS is much more popular and have lots of support .
Good Tutorial but i get this Error Notice: Undefined variable: options in E:\ab\cd\ef\gh\application\forms\Login.php on line 6
I didnot get any error like that . Which version of ZF are you using ?
Consider declaring the $options in the class and passing it to the parent constructor .
I have installed your application and it works fine. Now I am trying to implement the login method and get the eroor "C:\Program Files (x86)\Zend\Apache2\htdocs\Ulferik\application\forms\Login.php on line 6".
Can you tell me where this $option is suppose to come from? I have found it in several places but not where it is declared.
Thank's for sharing this tutorial whit us.
Ulferik
Hey I am following this tutorial and was wondering what version of ZF this works with? Also, if you check line 12 in the application.ini listing, you will see that its actually the title for the list below! :) It makes it kinda difficult to know which file the listing is for!?
Hi Chris ,
I didnot understand one thing from your comment Line 12 . I think its Line 21 what you was talking about . Its always a tedious job to post a big post like this . Even though I have revised it a lot , some errors like this may happen . I am sorry that the code and the text got wrapped . Have corrected the piece of code and Thanks for your comment to notify me .
You may have jumped to the code, and may have forget to read the title of the post itself . I am using ZF 1.9 now and it will works fine in 1.8 too as from 1.8 onwards is ZF command . I don't have used the ZF components below 1.8 .
If you have any trouble understanding any part of it feel free to grab the complete code from github which I have added a link to it . I am adding new features and updating the code . So the code you see in git may be slightly different than the code I have posted . The code in git is always a working example and I hope it will works fine in every system .
Zend_Form has a „init“ method, you don't need „__construct()“ and set the options again!
Its very helpful for a beginner..Thank you so much for your post.
Hi, First, thank-you very much for your tutorial. It was very helpfull. I´m doing an implementation of your code, but when I try to submit the form, I get the error below. I believe that the problem comes from the "$result = new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null);" and "$result = new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);" in the AuthAdapter.php file. When I comment them, the error below doesn´t happen. Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in H:\SERVER_ADER\WEBARCHITECT\HTDOCS\DP_INTRANET\PRODUCAO\library\Zend\Controller\Dispatcher\Standard.php:241 Stack trace: #0 H:\SERVER_ADER\WEBARCHITECT\HTDOCS\DP_INTRANET\PRODUCAO\library\Zend\Controller\Front.php(936): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 H:\SERVER_ADER\WEBARCHITECT\HTDOCS\DP_INTRANET\PRODUCAO\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch() #2 H:\SERVER_ADER\WEBARCHITECT\HTDOCS\DP_INTRANET\PRODUCAO\library\Zend\Application.php(303): Zend_Application_Bootstrap_Bootstrap->run() #3 H:\SERVER_ADER\WEBARCHITECT\HTDOCS\DP_INTRANET\PRODUCAO\public\index.php(38): Zend_Application->run() #4 {main} thrown in H:\SERVER_ADER\WEBARCHITECT\HTDOCS\DP_INTRANET\PRODUCAO\library\Zend\Controller\Dispatcher\Standard.php on line 241 What am I doing wrong? Thanks in Advance.
Hi ,
Thank you man .
For your problem it seems you don't have an error controller . Do you have one ?
How did you start your project ? With the zf create project itself ? Or something different ?
Hey Hari,
I hope I´m not being a burden, but answering your question I did not create my project using zf.
Instead, I´ve made exactly as they explain at this tutorial http://framework.zend.com/docs/quickstart/create-your-project, but the structure itself I created by hand.
As soon as I created an Error controller, it worked perfectly. So my question is just one thing: why do I need an error controller in this case? Where in the project it would say that it is required, if there´s such parameter? application.ini ? Would it be the "phpSettings.display_startup_errors = 1" ?
Thanks a lot in advance.
hello, Thank you. It is very interesting.
Hello, first of all great tutorial, I am learning a lot from it. You have a little bug in the HEAD section. headlink() should be echoed only once. In your case the link tags for the css will be duplicated in the output. The correct version would be :
<?php $this->headLink()->appendStylesheet('/quickstart/public/css/global.css') ?> <?php $this->headLink()->prependStylesheet($this->baseUrl().'/css/main.css'); ?> <?php echo $this->headLink(); ?>Friendly regards.Hi, Just curious, why does "Change the contents of application/views/scripts/index/logout.phtml with" Appear twice with 2 source code boxes? Is it an error?
Hi, Thanks so much for the tutorial, the Auth and Acl was especially useful. However I'm confused by two mentions of: Change the contents of application/views/scripts/index/logout.phtml with is that an error?
Thanks edwardc for poiting me to it . Have corrected it now in the post.
Have a great day .
This may be just a tutorial for a simple application but the entire code is a mess. I know the Zend Framework is meant to give a modular approach to PHP development but like so many examples I see, this is openly absuive. It's nothing personal, as you are not alone - there is just so much rubbish out there and unfortunately you are only contributing to it.
Great post. I am pretty new to zend but I just wanted to know how you retrive the parameters from the ini file and instantiate the database connection. Thank you
Hi Noel Darlow ,
Thanks for your comments . I am new to ZF so there may be many errors . If you can tell me where I am wrong , I can correct it, else till you point me where I am wrong I am right :) . I published this for if there is someone who can learn something from me . I ll never tell I am completely correct as I know that some places I am wrong . So if you have developed something to learn for others please do share it , so others can learn from your great post .
Hi Arash ,
If you are looking to learn how the database connection works , the post by http://akrabat.com/zend-framework-tutorial/ is a good one . I started with quickstart then with the post by him and many other bloggers . In the file application/configs/application.ini
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = <your username>
resources.db.params.password = <your password>
resources.db.params.dbname = <your db name>
This creates a DB connection .
Hi, Nice tutorials but i cant seem to view the post, the page itself, after login. it says an error occurred! page not found.
Can you tell me more clearly . Like where you kept the files , the url , the url you got the error , the exact error thrown etc ?
If so I will try to tell where it causes the problem .
Thanks for reporting it too . But I don't find any problem at present for me .
I like how you explain and show what you did.
I have used this and helped me implement the Zend_ACL , Zend_Auth in my Sudoku site.
on my blog i also talk about zend frame work.
http://roninio.blogspot.com/
cheers and hope to see more of such tutorials.
Sudoku
Very good post, thanks a lot.
Thank you.
Yes, its really Happy to see the comments .
Some don't write anything even though it may have helped them . But its always some of the Happy moments to read and hear the comments if though it doesn't make a living .
Thank you to all who have commented and helped me to learn ZF and who have pulled me to learn ZF .
I don't see Post Controller in your post. It is on github, but it would be nice to be here
Thank you Nexik for notifying me.
Seems many have not seen the mistake ;) .
Hi,
there are few bugs.
In application/forms/Comments.php on line 13 there should be
$identity['Role']instead of$identity['role']In application/Models/Acl.php you should add
$this->allow('blogger', 'comments', 'add');But still helpfull tut.
Thank you!
Jan
Thank you for reporting it . First I just made the DB field as 'role' then later changed to 'Role' ie why the mistake . And the other I may have forget to add the role .
great stuff but i have read loads of tutorials about acl and auth but never get shown how to assign a role to a user? how to do it properly, i have found it quite hard searching for acl and auth info for cakephp and zend framework that actually shows all steps...any help or websites would be great! :D thanks
I too have spend a lot of time to understand how the Zend_Acl and Zend_Auth works .
It was really hard to understand atleast for me . But when I learned something , its a wonderful one .
You can set the roles dynamically to assign roles for users.
For eg ( from what i learned , may be wrong : if some one thinks I am wrong please let me know ) : if you are using dynamically you need a table for Roles with role_id , name .
And you can have a table for Resources : resource_id , resource_name , role_id .
Then you can assign these roles and resources . ie it .
hi thanks for ur Blog regarding zend acl perfect blog to start with but i'm new to zend framework every thing is working fine with the index controller but its not taking to other controllers such as Gallery controller as well as the postcontrollers please help me out with some tips ASAP
Its showing error like
Not Found
The requested URL /harikt-zendblog-86859be/public/gallery was not found on this server.
Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.2 with Suhosin-Patch Server at localhost Port 80
please help me out with some tips ASAP
Thanks n Best wishes
With this minimum error "Not found" I guess either you don't have the controllers or the path going from index may be to different .
Have you configured htaccess .
Hi Hari,
I, too, am learning ZF, so I want to say thanks for two things.
1. Thanks for a great sample app. A nice level, not too trivial, not too difficult. Especially the Auth/ACL stuff. Very helpful.
2. Thanks for being so open about your experience. I appreciate your candor that: you are learning; you are doing the best you can; you might make some mistakes, either in code or in design; you welcome the feedback about mistakes and ways to do things better. Your app might not be the most polished out there, but I find your manner to be quite refreshing. It gives me a bit more confidence to simply put myself out there like you are doing.
It would be interesting perhaps to add Routing to the blog app. Perhaps not out of necessity - there is nothing wrong with the current urls of the form "controller/action/k1/v1/k2/v2". But it might be instructive to see an example of how to define routes, some ideas about where to define them, conventions for naming the plugin class and for placing the plugin file in the file-system. All that good stuff.
Again, thanks for an excellent contribution to the ZF community, especially for us noobs.
Cheers!
Hi,
Firstly thanks for all the open minded posts you wrote. I read most of them. I still could not figure out how to run this application. I can run the public/index page which lists the items from the database but when i click on an item for detail - on its title it gives me these errors
--->
An error occurred
message ?>
Exception information:
Message: exception->getMessage() ?>
Stack trace:
exception->getTraceAsString() ?>
Request Parameters:
request->getParams()) ?>
<----
I can see the pagination on the first page of public but when i click on anything else its gone. Can you help me out. Thanks. Can it be that your application here is a bit different than on github code.
Thanks,
Mahmood
Hi Mahmood,
Actually I was not able to figure your problem . ie why not replied to it . Have you tried quickstart or any such applications before this ?( Hope you have done it and has no issues :) )
Have you tried the code in github and still facing the same issue ?
Some of the things I can suggest is looking the url and look the controller, action and get request parameters . May be some errors in it .
Thanks
Hi, it is really useful stuff for beginners, thanks.
I found a little bug in your code:
application/controllers/IndexController in loginAction you have line
$this->view->errors = 'user credentials not found';and in login view there is
<?php echo $this->error; ?>so you should change 'error' to 'errors' in login.phtml file
Thanks dude .
Hi Hari,
I am not able to download code snippet from the url
http://github.com/harikt/zendblog
Thanks,
Hardik
I have tried it , and there is no such problems for me . Don't know the problem you are facing :( .
Have you tried with git too ? Else use git clone .
Hi,
With all due respect can u answer the question i put first above with the displaying errors,
Thanx,
Mahmood
Post new comment