Join over 55,891 Subscribers Today! FREE UPDATES!
Get The Only Freelancer crash course you will ever need to read!
You’ve been still tormenting and write-ins of the same type in the n-times for the site? You do what has been done for you and try to divide the project design from the code? Looking for realization of the necessary functions in questionable forums? If you answered “yes” one of these questions, so this article is definitely for you. There I will tell you about the powerful free framework (Content Managment Framework), if you learn it you will not only save time for the design, but also rtake the quality of your work to a much higher level. So, let’s sit comfortably and read my first article of the series “Learning to work with Zend Framework”.
The major aim for any CMF – reduce time for a project’s working up. Using this product, you can achieve following:
Someone says that ZF is too complicated for understanding, heavy, requires a lot of server resources. It’s not like this in reality. If you can to learn php, so moreover you can deal with ZF, and as for heaviness, a good diet will eliminate this shortcoming with no problems.
There are no ideal solutions and ZF is not the exception. It has its both positive and negative sides, which we’ll discuss in this part. Let’s start with the negative:
That’s all the negative sides. Now the positive:
So, it’s enough, no more theory and let’s go straight to practice. We estimate the possibility of the giant, so to speak:) Inorder to work with ZF, we need ZendFramework (you can download the latest version from here, HTTP server with the mod_rewrite support, PHP at least version 5.2, and MySQL 5. Download the latest stable version. By the way I almost forgot, you can download ZF in two different assemblies:
Full Package contains the Dojo Toolkit, and the demos to work with a skeleton. Because you are just beginning to study this framework, I recommend to download this version.
Minimal Package contains only ZendFramework library.
Extract the folder with the core (/ library / Zend), ZF is better to keep a few levels higher, so that don’t produce the files for each project, in my example, it turned out in this folder D: \ library \ ZF \ 1.7.8 \ Zend \. (i’m on PC)
Let’s organize the file structure for our future project. Create two directories in the root of the application:
Also create index.php and .htaccess in the root, in which immediately add rules of redirects:
RewriteEngine on
RewriteRule .* index.php
It’s necessary to add similar .htaccess file, in the folder “public”.
RewriteEngine off
Now lets create 3 folders in “application” directory: configs, library, modules.
After all these simple manipulations I had such a structure:
The root:
If the structure is ready, so we can move on to the coding ;-)
Open our index file (index.php). Let the interpreter know that php code wiil start soon and determine 4 constants:
PATH_TO_ZF - the path to ZF
PATH_TO_APPLICATION - the path to the folder “application”
PATH_TO_LIBRARY - the path to our libraries
PATH_TO_MODULES - the path to our modules.
The code:
define('PATH_TO_ZF', '../../../ZF/1.7.7/');
define('PATH_TO_APPLICATION', './application/');
define('PATH_TO_LIBRARY', PATH_TO_APPLICATION . 'library/');
define('PATH_TO_MODULES', PATH_TO_APPLICATION . 'modules/');
Now we should tell our intepriter the path to load all our stuff from:
include_path(PATH_TO_ZF . PATH_SEPARATOR . PATH_TO_APPLICATION . PATH_SEPARATOR . PATH_TO_LIBRARY);
The next step is to download Zend_Loader (later we will return to it) and register classes’ autoload.
require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload();
So, Zend_Loader is loaded, now let’s activate Zend_Controller_Front (about it, too, later) and point the location of our modules to the dispatcher. Then start the process of dispatching.
$controller = Zend_Controller_Front::getInstance(); $controller->addModuleDirectory(PATH_TO_MODULES)->dispatch();
The result should be something like this:
define('PATH_TO_ZF', '../../../ZF/1.7.7/');
define('PATH_TO_APPLICATION', './application/');
define('PATH_TO_LIBRARY', PATH_TO_APPLICATION . 'library/');
define('PATH_TO_MODULES', PATH_TO_APPLICATION . 'modules/');
set_include_path(PATH_TO_ZF . PATH_SEPARATOR . PATH_TO_APPLICATION . PATH_SEPARATOR . PATH_TO_LIBRARY);
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$controller = Zend_Controller_Front::getInstance();
$controller->addModuleDirectory(PATH_TO_MODULES)->dispatch();
As you can notice Zend_Controller_Front never loaded because Zend_Loader loadsController automatically. Zend_Loader recognizes location of the controller on its name:
Zend_Controller_Front is in Zend/Controller/Front.php
Zend_Controller_Front implements the Singleton pattern, that means it can be initialized in the project only once. When you call a method dispatch(), the manager goes into a loop of dispatching passing three events:
Create the folder “default” in our modules folder, it will be our first module. If we turn to our website using the link http://localhost/site.ru (you may have another location), it will be carried our default module. Name of default module can be changed, for example, to the “index”. It is done using the method – setDefaultModule (), object Zend_Controller_Front. It’s necessary to call the method before dispatching. As a parameter the method should get the module name, which will be used by default.
$controller->setDefaultModule('index');
Come on. We will create now two more folders in the module’s folder:
Create a new controller (IndexController.php), and put this code:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
return;
}
}
Now you need to create a script for our controller. For it, create folders “scripts/index” in a folder “views”. It should be something like this:
default
controllers
IndexController.php
views
scripts
index
Create a file index.phtml in the folder views/scripts/index/. Try to test it! If there are no errors, it means you are good to listen to me :) Now add the event:
$this->view->var = ‘ZendFramework’;
General view of the controller looks like this:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->var = ‘ZendFramework’;
}
}
Lets add into the view file the following code:
echo $this->var;
After that, update the page.
Create another controller in our module, ErrorController.php:
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
return;
}
}
Now lets create a submission for the error: /default/views/scripts/error/error.phtml:
404
In order to test it, type in your browser’s address bar: site/qwerty. As we do not have this page, so the result will be appropriate. In order that would include the output error due to which the script is stopped before dispatching we need to call the throwExceptions() method and passing the appropriate parameters, if we want to see a mistake, true, and false, if we want to differentiate a mistake when we created this page.
First, I think that’s enough. For our first meeting we discussed a lot of interesting things. It is not all clear while, but do not worry. Skill comes with time. The main thing is not to be lazy and try to do something. In the article I mentioned that ZF is based on the MVC architecture. If you’re scared or have entered a dead end, the words “view”, “controller”, then launch Google and read a basis MVC theory.
That’s all; I hope you enjoyed this tutorial, and thanks for reading! See you in next chapter.
Get The Only Freelancer crash course you will ever need to read!
Hi there! My name is Nick Plekhanov. I'm a passionate web developer and user interface designer from Russia, experienced in PHP, and MySQL along with the beautiful XHTML and CSS under jQuery stuff. I've been doing web development for 3 years. Here, I'll be your PHP coder, sometimes designer, and SEO expert. My own web development blog is coming really soon. If you’d like to connect with me, follow me on Twitter.
Sunday, April 8th, 2012 20:25
i wish this article had better grammar. It was really hard to follow along; but thanks for posting
Thursday, July 28th, 2011 16:11
Good stuff
Once you get going it can be worth to stop for a moment and have a think where do things go? how to separate concerns? how to decouple the layers and identify rules of what should be done and in which place.
Friday, July 22nd, 2011 09:59
Hi ,
i have followed your steps,
I got this error:
Fatal error: Uncaught exception ‘Zend_Controller_Exception’ with message ‘Invalid controller specified (error)0′ in C:\xampp\htdocs\zendraj\application\library\Zend\Controller\Plugin\Broker.php:337 Stack trace: #0 C:\xampp\htdocs\zendraj\application\library\Zend\Controller\Front.php(965): Zend_Controller_Plugin_Broker->postDispatch(Object(Zend_Controller_Request_Http)) #1 C:\xampp\htdocs\zendraj\index.php(15): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\xampp\htdocs\zendraj\application\library\Zend\Controller\Plugin\Broker.php on line 337
can u please help me in it?
Friday, September 23rd, 2011 21:07
Felt so hopeless loiknog for answers to my questions…until now.
Tuesday, March 8th, 2011 15:50
now i got the idea how thing goes under the hood.. very well explained.. thumbs up for this.. now i think zend should come with the smarty.. hell it will make things lot easier.. regards. :-)
Tuesday, October 26th, 2010 13:52
Any reason you are not using 1.10? 1.11 is right around the corner. Also after 1.8, ZF started using Zend_Application_Bootstrap to help get your application loaded, which would be much easier for noobs, instead of writing all this custom code in your index.php.
Tuesday, October 26th, 2010 07:41
oops, I cannot type the signs it ends with. Anyway, looks like there is an error in your html.
Tuesday, October 26th, 2010 07:39
So, does this mean your manually generated structure looks the same like the auto-generated structure by zf?
btw: by the way: Your directory structure looks like it is one level only, and it ends with </li … so there might be a coding error in your html.
Monday, October 25th, 2010 06:35
Hi, Excellent tutorial (i will read the 2nd part in a few momements :).
Im currently trying to learn Symfony. What do you think of it? I would appreciate your guidance!
Friday, October 22nd, 2010 15:42
good article to learn more things
Wednesday, October 20th, 2010 20:13
i would hire a professional to do all that to me , haha , i didn’t actually have time recently to learn these kind of things , i know it make sense and it really helps , but may be soon i will !! :)) thank You
Wednesday, October 20th, 2010 18:21
mfk, Dan, thank you. Your comments is much appreciated.
Tuesday, October 19th, 2010 14:05
I like PHP frameworks but ZEND looks for me much more complicated until now, thanks.
Wednesday, October 20th, 2010 18:14
What are your most favorite frameworks? As for me, time by time i use CodeIgniter, ZF, and CakePHP. They are awesome.
Thursday, May 19th, 2011 13:02
i think zend is not
Tuesday, October 19th, 2010 18:25
I don’t use to do these stuffs manually . We have the zf command line from 1.8 onwards . So why do we want to mess with all :) .
The things that you have done can be done via
$zf create project
The default structure and the error controller and all will be done :) .
Need to create another controller ?
$zf create controller
Need to create another action ?
$zf create action
Like so and so :) .
Wednesday, October 20th, 2010 18:09
Thanks. Good addition Hari K T – I totally agree!
Just wanted to show how to do things without them ;)
Friday, May 4th, 2012 04:00
Actually Nick, I think the tutorial would have been better for beginners if we used the command line tool. And this manual process could have been something of an extra, for the motivated reader to understand in more detail.
Tuesday, October 19th, 2010 17:59
Good tutorial, but you need to improve your English :)
Wednesday, October 20th, 2010 18:11
I know. I do all my best to improve it, every day btw …..
Tuesday, October 19th, 2010 17:35
“Resource-intensive. (…) I have (…) seen more than one hosting provider that would kick the bucket from ZF” – well, that makes it even more odd that provider 1&1 is -to my knowledge- offering Zend framework within their shared hosting environment. Has anyone experience with ZF on 1&1 shared hosting servers? Last time i looked, they themselves didn’t offer much/any documentation on the subject. So if anyone could point to a decent “Hello World using Zend framework on 1&1 servers” tutorial that would be much appreciated.
Tuesday, October 19th, 2010 17:07
Looking forward to your feedback, comments, and opinions on what should i do to improve my article’s usefulness for you my friends.
Thanks
Tuesday, October 19th, 2010 15:21
thanks for the tutorial ! i`m w8ing for step 2 ! keep up the great job man :D
Tuesday, October 19th, 2010 12:45
Thank you so much for this.
I was looking for an article just like this about 3 weeks ago, and now I’ve found it!!
Better late than never :-)
If not, then it's time to learn how to:
You can trust 1stWebDesigner to help you become a better web designer!
- Jacob Cass | Just Creative
Just enter your name and email below and click Get Updates!
Mia Lazar
Tuesday, October 19th, 2010 14:05
I like PHP frameworks but ZEND looks for me much more complicated until now, thanks.
Sumit
Thursday, May 19th, 2011 13:02
i think zend is not
Nick Plekhanov
Wednesday, October 20th, 2010 18:14
What are your most favorite frameworks? As for me, time by time i use CodeIgniter, ZF, and CakePHP. They are awesome.
Paul Bouzakis
Tuesday, October 26th, 2010 13:52
Any reason you are not using 1.10? 1.11 is right around the corner. Also after 1.8, ZF started using Zend_Application_Bootstrap to help get your application loaded, which would be much easier for noobs, instead of writing all this custom code in your index.php.
Rahul
Wednesday, October 20th, 2010 20:13
i would hire a professional to do all that to me , haha , i didn’t actually have time recently to learn these kind of things , i know it make sense and it really helps , but may be soon i will !! :)) thank You
Nick Plekhanov
Wednesday, October 20th, 2010 18:21
mfk, Dan, thank you. Your comments is much appreciated.
Hari K T
Tuesday, October 19th, 2010 18:25
I don’t use to do these stuffs manually . We have the zf command line from 1.8 onwards . So why do we want to mess with all :) .
The things that you have done can be done via
$zf create project
The default structure and the error controller and all will be done :) .
Need to create another controller ?
$zf create controller
Need to create another action ?
$zf create action
Like so and so :) .
Nick Plekhanov
Wednesday, October 20th, 2010 18:09
Thanks. Good addition Hari K T – I totally agree!
Just wanted to show how to do things without them ;)
Gopal Aggarwal
Friday, May 4th, 2012 04:00
Actually Nick, I think the tutorial would have been better for beginners if we used the command line tool. And this manual process could have been something of an extra, for the motivated reader to understand in more detail.
mfk
Tuesday, October 19th, 2010 15:21
thanks for the tutorial ! i`m w8ing for step 2 ! keep up the great job man :D
Nick Plekhanov
Tuesday, October 19th, 2010 17:07
Looking forward to your feedback, comments, and opinions on what should i do to improve my article’s usefulness for you my friends.
Thanks
Oliver
Tuesday, October 19th, 2010 17:35
“Resource-intensive. (…) I have (…) seen more than one hosting provider that would kick the bucket from ZF” – well, that makes it even more odd that provider 1&1 is -to my knowledge- offering Zend framework within their shared hosting environment. Has anyone experience with ZF on 1&1 shared hosting servers? Last time i looked, they themselves didn’t offer much/any documentation on the subject. So if anyone could point to a decent “Hello World using Zend framework on 1&1 servers” tutorial that would be much appreciated.
SkullTraill
Tuesday, October 19th, 2010 17:59
Good tutorial, but you need to improve your English :)
Nick Plekhanov
Wednesday, October 20th, 2010 18:11
I know. I do all my best to improve it, every day btw …..
Veerankimuni
Friday, October 22nd, 2010 15:42
good article to learn more things
Dan
Tuesday, October 19th, 2010 12:45
Thank you so much for this.
I was looking for an article just like this about 3 weeks ago, and now I’ve found it!!
Better late than never :-)
Artur Ejsmont
Thursday, July 28th, 2011 16:11
Good stuff
Once you get going it can be worth to stop for a moment and have a think where do things go? how to separate concerns? how to decouple the layers and identify rules of what should be done and in which place.
sarmen
Sunday, April 8th, 2012 20:25
i wish this article had better grammar. It was really hard to follow along; but thanks for posting
Rajeev
Friday, July 22nd, 2011 09:59
Hi ,
i have followed your steps,
I got this error:
Fatal error: Uncaught exception ‘Zend_Controller_Exception’ with message ‘Invalid controller specified (error)0′ in C:\xampp\htdocs\zendraj\application\library\Zend\Controller\Plugin\Broker.php:337 Stack trace: #0 C:\xampp\htdocs\zendraj\application\library\Zend\Controller\Front.php(965): Zend_Controller_Plugin_Broker->postDispatch(Object(Zend_Controller_Request_Http)) #1 C:\xampp\htdocs\zendraj\index.php(15): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\xampp\htdocs\zendraj\application\library\Zend\Controller\Plugin\Broker.php on line 337
can u please help me in it?
Rean John Uehara
Friday, July 22nd, 2011 12:37
Hi, I’m not sure the author of this guide can answer your question since we’re no longer in contact with him. I suggest you post this problem on Stackoverflow and other QnA sites for webdevelopers. I’m sure you’ll get fast feedback.
Kaylyn
Friday, September 23rd, 2011 21:07
Felt so hopeless loiknog for answers to my questions…until now.
oscar B.
Monday, October 25th, 2010 06:35
Hi, Excellent tutorial (i will read the 2nd part in a few momements :).
Im currently trying to learn Symfony. What do you think of it? I would appreciate your guidance!
Dave
Tuesday, October 26th, 2010 07:39
So, does this mean your manually generated structure looks the same like the auto-generated structure by zf?
btw: by the way: Your directory structure looks like it is one level only, and it ends with </li … so there might be a coding error in your html.
r-benTahir
Tuesday, March 8th, 2011 15:50
now i got the idea how thing goes under the hood.. very well explained.. thumbs up for this.. now i think zend should come with the smarty.. hell it will make things lot easier.. regards. :-)
Dave
Tuesday, October 26th, 2010 07:41
oops, I cannot type the signs it ends with. Anyway, looks like there is an error in your html.