head first zend framework - part 1 project & application
TRANSCRIPT
Head First Zend FrameworkPart 1 - Project & Application
Jace Ju
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
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:.
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
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
Setup Project
Setup VirtualHost in Apache config file
http://zf_test/
<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>
Create Protable Project
Change position of index.php and .htaccess
http://localhost/zf_test/
# 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'
Project Structure
application
configs
controllers
models
views
helpers
scripts
docs
library
public
tests
Application
Zend_Application
Zend_Application_Bootstrap
Zend_Application_Resource
Zend_Application
Load Configuration.
Initialize bootstrap.
Run.
# cd /path/to/wwwroot/zf_test
# vi index.php
... (ignore) ...
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
application.ini
Configuration section.# cd /path/to/wwwroot/zf_test
# vi application/configs/application.ini
[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
[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
phpSettings in application.ini
Use "ini_set" function.
See http://www.php.net/manual/en/ini.list.php.
includePaths in application.ini
Use "set_include_path" function.
Prepend to "include_path".
"library" is identity for configuration.
bootstrap in application.ini
To load Bootstrap class.
Zend_Application_Bootstrap
Initialize custom resources for project.# cd /path/to/wwwroot/zf_test
# vi application/Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initResource1()
{
$this->bootstrap('resource2'); // Initialize Resource 2 first.
echo "Initialize Resource 1\n";
}
protected function _initResource2()
{
echo "Initialize Resource 2\n ";
}
}
appnamespace in application.ini
Define the autoloader prefix of models, form, etc.
resources in application.ini
Load common resources.
Define resource options.
FrontController Resource
Initialize Front Controller.
frontController.controllerdirectory
call Zend_Controller_Front::setControllerDirectory()
frontController.params
call Zend_Controller_Front::setParams()
Zend_Application_Resource
Common Resources
Front Controller
Router
Database
View
Session
Layout
Log
...
More flexible then Bootstrap::_init<Resource> method.
Example: Database Resource
Initialize Custom Resources for Project
Will initialize Zend_Application_Resource_Db
# 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) ...
Autoloadernamespaces in application.ini
Register the prefix of library. Autoloadernamespaces[] = "My_"
pluginPaths in application.ini
Register the prefix and path of plugin classes.
Resolve loading order of plugin.pluginPaths.My_Application_Resource_ = "My/Application/Resource"
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.
See you next part.