In this second part (click here to check the previous part) of this series we will create a new Zend Framework project, which will be used as basis of all the other topics in the series. So, first of all, let’s describe what we will have.
Our project will be a system that will have the following features:
- User registration – a user will be able to enter their details and register
- List registered users – display a list of registered users (requires users to be logged in)
- Edit user – edit a user details (requires users to be logged in)
- Delete user – delete a specific user (requires users to be logged in)
- Login – log in a user
- Logout – logout a user (requires users to be logged in)
After defining what we are going to have at the end, let’s start !
Creating the project
To create our project, let’s make use of a useful tool called “zf”. So, open a terminal, go to your project base folder (i.e: /Users/myuser/Workspaces/eclipse/php/) and type:
By running this command we have created all the basic structure required to start using zend framework, including a default controller (IndexController.php) and auxiliary files. We recommend you to explore those files and see how they look like. For further details about them, have a look at Zend Framework Documentation (http://framework.zend.com/manual/en/).
Now, open Eclipse, click on File -> New -> PHP Project. Give a name to the project (Preferably the same name as your gave to zf tool). And finish the wizard to create the project.
As the Zend Framework library is out of our project folders, we will also have to edit the project properties to indicate where the Zend Framework library is. This is important to allow code completion features and a better Debugging. So, in the project tree (left sidebar), right click on the project’s root folder (i.e. “zfguide”) and select properties. At the properties Window, click on PHP Include Path then select the libraries tab. Now, click on “Add External Source Folder …” and select the Zend Framework Library Folder (“/Applications/Mamp/bin/php5.3/lib/php/Zend”). And Finally click in OK. After finishing this process, the autocompletion features will be include the Zend Framework classes.
Now open the file application/config/application.ini and add the following line into the [production] section:
1 | phpSettings.date.timezone = "Europe/London" |
This is going to be used by Zend_Date to handle dates in your application.
Now the basic part of your application is up and running. So, open your web browser and type http://zfguide (if you are using the same virtual host as defined in the part 1 of this tutorial). You should see something like this:
Creating a layout
Layouts are a very good feature from Zend Framework to keep your code organized and easy to manage. To enable the use of Zend_Layout, first we need to inform our bootstrap to use the Layout resource. This can be easily done by using the zf tool to enable the layout.
After that, your config file will contain a layout entry, and a layout file will be created at application/layouts/scripts/layout.phtml
Open the configuration file application.ini and you will see that a new line has been added. Now, lets add another line to this configuration file, in order to initialize the view object which we will need to use in the Bootstrap.php file.
1 2 | ; Add to [production] section: resources.view[] = |
The final version of the application.ini file should look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" resources.view[] = [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.params.displayExceptions = 1 |
Now let’s edit the Bootstrap.php file and make use of some interesting view helpers to improve the HTML generated in the layout. We first create a new method called _initViewHelpers() (in the Bootstrap file, everytime you create a method using the prefix “_init”, this method will the called during the Bootstrap) and define the attributes we want for the helpers:
1 2 3 4 5 6 7 8 9 10 11 12 | protected function _initViewHelpers() { // bootstrap the layout and get the view object $this->bootstrap('layout'); $layout = $this->getResource('layout'); $view = $layout->getView(); // setup helpers $view->headMeta()->appendHttpEquiv('Content-Type','text/html; charset=utf-8'); $view->headTitle('.:: ZFGuide Application ::.'); $view->doctype('XHTML1_STRICT'); $view->headLink()->appendStylesheet(Zend_Controller_Front::getInstance()->getBaseUrl() . '/styles/default.css'); } |
And now, let’s edit application/layouts/scripts/layout.phtml and define the layout if the website:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php echo $this->doctype(); ?> <html> <head> <?php echo $this->headTitle(); ?> <?php echo $this->headMeta(); ?> <?php echo $this->headLink(); ?> <?php echo $this->headLink()->appendStylesheet($this->baseUrl() . '/styles/default.css'); ?> <?php echo $this->headScript(); ?> </head> <body> <div id="header"> <h1>ZF Basics</h1> </div> <div id="content"> <?php echo $this->layout()->content; ?> </div> </body> </html> |
Now we have a functional website. Go to your browser and refresh it, the source now will included all we defined in the layout.
The last step is to clean the index.phtml file, since there is an example html generated by the zf tool. Open application/scripts/index/index.phtml and replace the code for this one:
1 | View script for controller <strong>Index</strong> and script/action name <strong>index</strong> |
Enabling Debug
Click in the “bug” icon, and select “Debug Configurations” . Select PHP Web Page in the right menu, and click in “New”. Now give a name to this Debug Configuration (i.e. “zfguide-debug”), select Zend Debugger as Server Debugger. Select Default PHP Web Server in the PHP Server option and click in Configure. In the Name Field, insert “ZF Guide Web Server” and in the URL, insert the URL you have configured in your apache (i.e. http://zfguide).
Click Ok and click browse in the File field. Select the index.php in the “public” folder. Check Break at First Line option, uncheck the “Auto Generate” URL and erase the content of the second text field. The final configuration should look like this:
Now, (don’t click close or debug yet) go to ZendDebugger folder where you have extracted the files (as we described in the Part 1) and you will see a file called dummy.php. Copy that file and paste in the same folder where the index.php is (“/zfguide/public/”). After doing that, go back to eclipse and click in Test Debugger. After some time, you will see a success message:
If you see this message, it means that you are ready to start debugging. Click on Debug and Eclipse will change to Debug perspective and will break in the First Line, as defined in the configuration.
And that’s it. Now we have a project created and we are ready to add new features to it.
In the next part (Part 3) of this series, we will create the model, the database and describe how to retrieve values from them.
Popularity: 28% [?]









