The Zend Framework is an incredibly comprehensive toolbox that makes the job of creating exciting and dynamic websites in php a lot simpler. For a list of all the benefits please refer to their overview
Why should you choose the Zend Framework?
You should use whatever tools make your job as a web developer as simple as possible. There are a number of frameworks available and they all have their merits and pitfalls. My reason for choosing Zend was because it was adopted at my workplace for the server application.
I am writing this tutorial primarily because I have found that there are not many comprehensive guides that take a beginner through the whole process of understanding what the framework is, how it can be used and what it can do for you. You tend to either get very simple examples or very complex ones that are out of date or missing some vital piece of the puzzle. So with that in mind, my goal is to compile a set of comprehensive tutorials and links to other tutorials that will take a beginner right through the process of learning the Zend Framework. I intend to start slow, giving you an overview of some of the powerful components you can use straight out the box, and then move onto the Model View Controller (MVC) structure that is seen as the best practice design for web development.
First things first, you must remember that the Zend Framework is a toolbox. The vast majority of tutorials on the web utilise the MVC architecture (a key part of what the Framework offers is MVC support to help you structure your sites better), hell even the 'Hello World' beginner guide on the Zend Framework website is an MVC example. However, don't let that put you off, you do not actually need to use that architecture to use the Zend framework, you can use as much or as little as you want.
Whilst the Zend Framework does have recommended ways to layout your applications it does NOT require you to follow them. This is important because it means you can start to use the Zend Framework now, in your existing applications, without having to perform a complete rewrite of your pages. It is the cause of a lot of confusion in the various supporting documents and contributes that makes the framework difficult to learn in my opinion. Really, everything should have two examples, clearly labelled to make things easier for new adopters, an MVC version, and a basic use version. The current documentation falls somewhere in between these two making it very difficult to find what you are looking for. On the plus side, there are a lot of very good tutorials on the internet that will help you to get to grips with it, I'll add links to all the really good ones on this site.
Let's get on with it then
OK, first things first, go get the framework. You can download it from the Zend framework download page.
Next, and most importantly, you need to look at the Zend Framework User Manual. This manual is your friend (even if it is sometimes slightly lacking in detail and examples). Bookmark it, you'll be needing to visit it often.
You will also need a development environment. I prefer the WAMP Server but you can use whatever implementation of PHP you wish. Note that I will be assuming you are using Apache web server for your development machine.
Now, do what I did. Have a scroll through the components list on the right of the web page [not this web page, the Zend Framework User Manual page
]. Anything grab your interest?
For me it was the Zend_Db as I was helping a friend with his website and needed to start from scratch.
My usual method of writing php sites was to create a standard include file that makes the calls to everything I need (like databases etc) which I call at the top of each php file. No reason to change things now so I did the same thing with this site.
My include file had the required zend framework loaders in it for the database access. The next part was that I didn't want to hard code any database connection settings, so again going back to the manual there is a section in the Zend_Db that links to the Zend_Config_INI and Zend_Config_Xml components. From a personal point of view, I always prefer to use XML so I went for that option. Reading the manual to see what sort of information I can place in the file, how to read it and how to pass that to the database component. Here is the extract from my include file that loads the configuration file:
//Zend Framework require_once 'Zend/Loader.php'; require_once 'Zend/Config/Xml.php'; require_once 'Zend/Db.php'; //Configuration settings $config = new Zend_Config_Xml('configs/config.xml','production'); //Database $db = Zend_Db::factory($config->database);
With an xml file structured like this:
<?xml version="1.0"?> <configdata> <production> <webhost>www.tauke.com.my</webhost> <database> <adapter>mysqli</adapter> <params> <host>localhost</host> <username>username</username> <password>password</password> <dbname>dbname</dbname> </params> </database> </production> </configdata>
Now in an example php page I can easily access the database like this:
// Always required require 'includes/tauke.php'; $sql = 'SELECT * FROM live'; $item = $db->fetchAll($sql);
Ok so I know none of that is exactly rocket science, or an actual 'type along at home' example. Don't worry I'll get to those
The real point I'm making in this introduction is that the Zend Framework is a toolbox that you can dip into at will to utilise its functionality, you don't need to use it all right away.
The Zend Framework is your coding toolbox. Use the tools you want, however you want, to help you complete the job at hand. You have the Zend Framework User Manual bookmarked so you can easily see how to use specific tools [now you did bookmark that didn't you
]
Next time I'll go into a little detail on the main components in the Zend Framework. You'll notice that I'm purposefully leaving the whole Model View Controller thing till a lot later. That's because it is not the main point of the framework and in my opinion gets far too much attention in online tutorials. I'd go so far as to say I feel it actually puts some developers off using the toolbox altogether, especially when they already have a project underway and don't have time to go re-invent the whole thing from scratch.