Head First Zend Framework - Part 1 Project & Application
1. Head First Zend Framework
Part 1 - Project & Application
Jace Ju
2. Install
Get Zend Framework from svn repository.
# cd /path/to/library/php
# svn co http://framework.zend.com/svn/framework/standard/tags/release-
1.10.x/library/Zend
(add ZF_HOME = /path/to/ZF, example: /usr/local/ZF)
# cd $ZF_HOME
# svn co http://framework.zend.com/svn/framework/standard/tags/release-1.10.x/bin
# cd bin
# ln –s $ZF_HOME/bin/zf.sh $ZF_HOME/bin/zf
3. Setup
Create config of zf script
# zf create config
Successfully written Zend Tool config.
It is located at: /usr/local/ZF/.zf.ini
(Bug in ZF 1.10.6.)
# vi /usr/local/ZF/.zf.ini
php.includepath change to php.include_path
# zf show config
User Configuration: /usr/local/ZF/.zf.ini
`-- php
`-- include_path: /usr/local/lib/php:/var/lib/php:.
4. What is zf script
Help info
# zf ?
Usage:
zf [--global-opts] action-name [--action-opts] provider-name [--provider-opts]
[provider parameters ...]
Note: You may use "?" in any place of the above usage string to ask for more
specific help information.
Example: "zf ? version" will list all available actions for the version provider.
... (ignore) ...
DbTable
zf create db-table name actual-table-name module force-overwrite
Note: There are specialties, use zf create db-table.? to get specific help on
them.
ProjectProvider
zf create project-provider name actions
5. Create Project
Build a project skeleton
# cd /path/to/wwwroot
# zf create project zf_test
Creating project at /path/to/wwwroot/zf_test
Note: This command created a web project, for more information setting up your VHOST,
please see docs/README
# cd /path/to/wwwroot/zf_test
# zf show project.info
Working with project located at: /path/to/wwwroot/zf_test
# zf show profile
ProjectDirectory
ProjectProfileFile
... (ignore) ...
TestsDirectory
TestPHPUnitConfigFile
TestApplicationDirectory
TestApplicationBootstrapFile
TestLibraryDirectory
TestLibraryBootstrapFile
6. Setup Project
Setup VirtualHost in Apache config file
<VirtualHost *:80>
DocumentRoot "/path/to/wwwroot/zf_test/public"
ServerName zf_test
# This should be omitted in the production environment
SetEnv APPLICATION_ENV development
<Directory "/path/to/wwwroot/zf_test/public">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
http://zf_test/
7. Create Protable Project
Change position of index.php and .htaccess
# cd /path/to/wwwroot/zf_test
# cp public/.htaccess .
# cp public/index.php .
# vi public/.htaccess
RewriteEngine Off
# vi public/index.php
<?php
header('HTTP/1.1 403 Forbidden');
# vi .htaccess
SetEnv APPLICATION_ENV development
# vi index.php
'/../application' change to '/application'
http://localhost/zf_test/
19. Zend_Application_Resource
Common Resources
Front Controller
Router
Database
View
Session
Layout
Log
...
More flexible then Bootstrap::_init<Resource>
method.
20. Example: Database Resource
Initialize Custom Resources for Project
# cd /path/to/wwwroot/zf_test
# zf configure db-adapter "adapter=mysqli&username=uname&password=mypass&dbname=mydb"
A db configuration for the production section has been written to the application
config file.
# vi application/configs/application.ini
[production]
... (ignore) ...
resources.db.adapter = "mysqli"
resources.db.params.username = "uname"
resources.db.params.password = "mypass"
resources.db.params.dbname = "mydb"
... (ignore) ...
Will initialize Zend_Application_Resource_Db
22. pluginPaths in application.ini
Register the prefix and path of plugin classes.
Resolve loading order of plugin.
pluginPaths.My_Application_Resource_ = "My/Application/Resource"
23. Summary
Zend_Application
Load configuration.
Set bootstrap and run.
Zend_Application_Bootstrap
Initialize resources.
Zend_Application_Resource
Get options from configuration.
Initialize controller, view, database, etc.