Zend Framework QuickStart
Introduction.
This document will help Zend Frameworks newcomers to learn and understand how to start their own ZF applications. You can download this file where you will find the code used in this quickstart guide.
The following guide will show how to create a very simple blog application using a common page layout and interacting with a database.
Directories needed
The following directory structure will be used in this quickstart.
Create a new directory in your webserver which points to our public folder.
Creating the database
Execute the following code in your database:
This will create our database and one table that will be used by our quickstart application, this table will store our visitors comments.
Setting up the rewrite apache rules
You must create an .htaccess file in the public folder with the following contents:
This file will redirect all requests to the index.php file.
Creating a configuration file
Create a file with the name quickstart.ini in the /application/config folder with the following contents:
This file contains two configurations needed in our quickstart example.
- Layout: we will set the path where our layout scrips are and the layout we want to use.
- Database: please setup here your database information.
Using the Zend Framework
Download from the official Zend Framework site the lastest version and copy all the library folder to the library folder of your application.
Creating the bootstrap file
Create an index.php file in the public folder with the following contents:
This files setups the follows:
- Error reporting: this will allow you see errors (if any) in your application, please disable it in production environments.
- Setting paths: this will add the paths needed by our application (configuration, models, ZF library and public).
- Load configuration: this will load our configuration file.
- Setup database: this will use our configuration file information in order to connect to the database.
- Setup layout: this will configure our page layout.
- Setup front controller: the front controller is the ZF component that does the MVC magic in the application.
Creating the comments model file (+ creating the write comments form)
This file allows your application to interact with the database, the filename is CommentsTable.php and it must be in the models folder.
This files includes:
- A require once to Zend/Db/Table/Abstract.php, this file is the ZF component that helps us to interact with our database comments table.
- Two protected variables: $_name containing the database table name (in our case 'comments') and $_primary containing the primary key of the table.
- getAddCommentForm method: this static method returns the form which our guests will need to write their comments.
- getComments method: this static method returns all rows in the comments database table.
- createNewComments method: this static method creates a new row in the comments database table.
Setting the main layout.
The layout is the html that we want to show in every request done to our application, this will helps us to not repeat the same html code in all pages, this example will have 2 pages, the home page and an about page, but we are not going to create this files with the same boring html code, in order to not to repeat that, we create the main layout; to do this, create a file called main.phtml in the /application/layouts/scripts folder with the following content:
This will create a title for our page and a little menu with two options (home and about). The line:
will output the real content that has been requested; in the following steps we will create that content.
Creating the controllers
The following files must be in the /application/controllers folder:
IndexController.php
This controller includes two methods:
- init: this method will setup our base url (e.g. http://host/quickstart/ , this, is our base url, is the url where our application has been addressed)
- indexAction: this will render our home page, notice that we are using the getAddCommentForm() method of the CommentsTable class, as well, we are adding the information given in the form to our comments table.
AboutController.php
This controller will also use the baseUrl variable and an empty indexAction method.
ErrorController.php
And finally we setup the error controller, any error found in our application will redirect to this controller/action.
Creating the view files
The view files are the html rendered code of each controller/action in our application. This files must be in the /application/views/scripts folder.
index/index.phtml
Notice that we are using the array $this->comments, this array has been passed to the view from the controller using:
about/index.phtml
error/error.phtml
And now we are done, just point your browser to http://host/quickstart and you are ready to see our application in action!
References
- Default Project Structure http://framework.zend.com/wiki/x/6KM
- Zend_Config http://framework.zend.com/manual/en/zend.config.html
- Zend_Db http://framework.zend.com/manual/en/zend.db.html
- Zend_Controller http://framework.zend.com/manual/en/zend.controller.html
- Zend_Layout http://framework.zend.com/manual/en/zend.layout.html
- Zend_Form http://framework.zend.com/manual/en/zend.form.html
ZF Home Page
Code Browser
Wiki Dashboard
Hi,
I suggest using Zend_Loader::registerAutoload() and then drop all the require_onces
Regards,
Rob...