head first zend framework - part 1 project & application

24
Head First Zend Framework Part 1 - Project & Application Jace Ju

Upload: jace-ju

Post on 11-May-2015

5.049 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Head First Zend Framework - Part 1 Project & Application

Head First Zend FrameworkPart 1 - Project & Application

Jace Ju

Page 2: Head First Zend Framework - Part 1 Project & Application

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

Page 3: Head First Zend Framework - Part 1 Project & Application

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:.

Page 4: Head First Zend Framework - Part 1 Project & Application

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

Page 5: Head First Zend Framework - Part 1 Project & Application

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

Page 6: Head First Zend Framework - Part 1 Project & Application

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>

Page 7: Head First Zend Framework - Part 1 Project & Application

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'

Page 8: Head First Zend Framework - Part 1 Project & Application

Project Structure

application

configs

controllers

models

views

helpers

scripts

docs

library

public

tests

Page 9: Head First Zend Framework - Part 1 Project & Application

Application

Zend_Application

Zend_Application_Bootstrap

Zend_Application_Resource

Page 10: Head First Zend Framework - Part 1 Project & Application

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();

Page 11: Head First Zend Framework - Part 1 Project & Application

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

Page 12: Head First Zend Framework - Part 1 Project & Application

phpSettings in application.ini

Use "ini_set" function.

See http://www.php.net/manual/en/ini.list.php.

Page 13: Head First Zend Framework - Part 1 Project & Application

includePaths in application.ini

Use "set_include_path" function.

Prepend to "include_path".

"library" is identity for configuration.

Page 14: Head First Zend Framework - Part 1 Project & Application

bootstrap in application.ini

To load Bootstrap class.

Page 15: Head First Zend Framework - Part 1 Project & Application

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 ";

}

}

Page 16: Head First Zend Framework - Part 1 Project & Application

appnamespace in application.ini

Define the autoloader prefix of models, form, etc.

Page 17: Head First Zend Framework - Part 1 Project & Application

resources in application.ini

Load common resources.

Define resource options.

Page 18: Head First Zend Framework - Part 1 Project & Application

FrontController Resource

Initialize Front Controller.

frontController.controllerdirectory

call Zend_Controller_Front::setControllerDirectory()

frontController.params

call Zend_Controller_Front::setParams()

Page 19: Head First Zend Framework - Part 1 Project & Application

Zend_Application_Resource

Common Resources

Front Controller

Router

Database

View

Session

Layout

Log

...

More flexible then Bootstrap::_init<Resource> method.

Page 20: Head First Zend Framework - Part 1 Project & Application

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) ...

Page 21: Head First Zend Framework - Part 1 Project & Application

Autoloadernamespaces in application.ini

Register the prefix of library. Autoloadernamespaces[] = "My_"

Page 22: Head First Zend Framework - Part 1 Project & Application

pluginPaths in application.ini

Register the prefix and path of plugin classes.

Resolve loading order of plugin.pluginPaths.My_Application_Resource_ = "My/Application/Resource"

Page 23: Head First Zend Framework - Part 1 Project & Application

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.

Page 24: Head First Zend Framework - Part 1 Project & Application

See you next part.