Tải bản đầy đủ (.pdf) (244 trang)

Zend Framework Web Services doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.15 MB, 244 trang )

Zend Framework Web Services
A php|architect Guide
by Jonas Mariën
Zend F r a m e w o r k W e b Services
Co nte nts C opy r i gh t ©2010–2011 J o n a s M ar iën – All Rights R e s e r v e d
Book and cover layout, design and text Copyright ©2004-2011 Blue P a r a b o l a , LLC. and its predecessors – All Rights
R e s e r v e d
First Edition: March 2011
ISBN: 978-0-98-103455-3
Pr odu ced in Ca nad a
Pr in ted in the U n i t e d S t a t e s
N o part of this book may be r e p r o d uc e d , stored in a r e t ri e v al system, or transmitted in any form or
b y means without the prior written permission of the publisher, except in the case of brief quotations
embedded in critical r e v ie w s or articles.
D i s c l ai me r
Although every effort has been made in the preparation of this book to ensure the accuracy of the
information contained therein, this book is provided ”as-is” and the publisher, the author(s), their
distributors and re ta i l e r s , as well as all affiliated, r e l a t ed or subsidiary parties take no r e s p o n s ib il i t y
for any inaccuracy and any and all damages caused, either directly or indirectly, b y the use of such
information. W e have endeavoured to properly provide trademark information on all companies and
products mentioned in the book b y the appropriate use of capitals. H o w e v e r , we cannot guarantee the
accuracy of such information.
Bl ue P a r a b o l a , The Bl u e P a r a b o l a logo, php|architect, the php|architect logo, N a n o B o o k and the
N a n o B o o k logo are trademarks or r e g is te re d trademarks of Bl ue P a r a b o l a , LLC, its assigns, partners,
predecessors and successors.
W r i t t e n b y J o n a s Mariën
Pu b l i s h e d b y Blue P a r a b o l a , LLC.
28 Bombay A v e .
T o r o n t o , ON M3H 1B7
Canada
(416) 630-6202 / (877) 630-6202


/ www.phparch.com
Pu b l i s h e r Marco T a b i n i
T e c h n i c a l Re v i e w e r Si m o n H a r r i s
Copy Editor S t a n T y m o r e k and Lori Ann P a n n i e r
Layout and Design Arbi Arzoumani
M a n a g i n g Editor Elizabeth T u c k e r Long
F i n an ce and R e s o u r c e M a n a g e m e n t Emanuela C orso
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
C o n t e n t s
Chapter 1 — Zend F r a m e w o r k 3
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Why Zend F r a m e w o r k ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Development Environment S e t up . . . . . . . . . . . . . . . . . . . . . . . . . 5
Bootstrap the P r o j e c t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Chapter 2 — Our D a t a 15
The C u s t o m e r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Their C a s e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Database S t r u c t u r e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The M e t h o d s for the W e b S e r v i c e . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Class C o d e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
C u s t o m e r R e q u i r e m e n t s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 3 — SOAP 25
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
SOAP and P H P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
First C o n t a c t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Plenty of Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

U s i n g Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
S o a p P a r a m , S o a p V a r and Classmaps . . . . . . . . . . . . . . . . . . . . . . . 31
S o a p P a r a m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Download from Wow! eBook <www.wowebook.com>
vi ” CONTENTS
S o a p Va r and V a r i a b l e T y p e s . . . . . . . . . . . . . . . . . . . . . . . . . . 32
P e r s i s t e n c e and C o o k i e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Enter WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Classmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Error H a n d l i n g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
SOAP and Zend F r a m e w o r k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Zend_Soap_Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Zend_Soap_Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Zend_Soap_Wsdl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Zend_Soap_AutoDiscover . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Example C o d e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Option 1: S e p ar a t e Bootstrap C o d e in a S e p a r a t e P H P File . . . . . . . . 49
Option 2: W r a p it I n s i d e a C o n t r o l l e r . . . . . . . . . . . . . . . . . . . . . 50
Extending Zend_Soap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
P u b l i c and P r i v a t e W e b Se r v i c e s . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Logging, V e r s i o n i n g , R e s p o n s e and Error C o d e s . . . . . . . . . . . . . . 56
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
V e r s i o n i n g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Error C o d e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Chapter 4 — C u s t o m i z i n g Our SOAP Service 61
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
S o l u t i o n 1: A d d an API K e y P a r a m e t e r to Each M e t h o d . . . . . . . . . . . . . 61
P r o and C o n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

S o l u t i o n 2: P r e - p a r s e the SOAP M e s s a g e . . . . . . . . . . . . . . . . . . . . . 63
P r o s and C o n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
S o l u t i o n 3: U s e M a g i c M e t h o d s . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
P r o s and C o n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Chapter 5 — REST 73
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
S o m e B a c k g r o u n d on REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Download from Wow! eBook <www.wowebook.com>
CONTENTS ” vii
REST versus POX versus RPC versus P u r e REST . . . . . . . . . . . . . . . . . 77
REST and P H P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Creating the REST S e r v i c e U s i n g Zend F r a m e w o r k . . . . . . . . . . . . . . . 86
Zend_Rest_Route . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Zend_Rest_Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Zend_Http_Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
S o m e Example C o d e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
C u s t o m i z e the REST Se r v i c e . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
API V e r s i o n in the URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
API K e y and P r e p r o c e s s i n g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Chapter 6 — XML-RPC 113
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
system.listMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
system.methodSignature . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
system.methodHelp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
system.multicall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
XML-RPC and P H P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Creating the XML-RPC S e r v i c e U s i n g Zend F r a m e w o r k . . . . . . . . . . 124
Zend_XmlRpc_Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

Zend_XmlRpc_Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
M o r e Zend_XmlRpc_Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
M u l t i p l e N a m e s p a c e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
F a u l t c o d e s and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 129
C a c h i n g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
XML Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
C u s t o m i z i n g the XML-RPC S e r v i c e . . . . . . . . . . . . . . . . . . . . . 131
API version in the URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
API K e y and P r e - p r o c e s s i n g . . . . . . . . . . . . . . . . . . . . . . . . . . 132
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Download from Wow! eBook <www.wowebook.com>
viii ” CONTENTS
Chapter 7 — JSON-RPC 137
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
S t r e a m s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
R e q u e s t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
R e s p o n s e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Error Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
B a t c h e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
JSON-RPC and P H P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Zend_Json_Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Zend_Json_Server_Smd . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
C o n n e c t i n g from J a v a S c r i p t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
What is M i s s i n g ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
C u s t o m i z e the JSON-RPC S e r v i c e . . . . . . . . . . . . . . . . . . . . . . . . . 148
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Chapter 8 — Limiting A cc e s s using Zend_Acl 151
R e s o u r c e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

R o l e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
R u l e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
P r i v i l e g e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Where to S t o r e the R u l e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
H o w to U s e Zend_Acl in our W e b S e r v i c e Co d e . . . . . . . . . . . . . . . . . . 162
A d d Checks to Existing Se r v i c e s . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
XML-RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
JSON-RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Chapter 9 — P e r f o r m a n c e and Scaling 173
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Where to S e a rc h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
N e t w o r k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
S e r v e r H a r d w a r e and T i e r e d S et u p . . . . . . . . . . . . . . . . . . . . . . 174
Download from Wow! eBook <www.wowebook.com>
CONTENTS ” ix
Operating S y s t e m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
W e b S e r v e r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
C o d e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
H o w to Detect Bottlenecks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
M o n i t o r i n g T o o l s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
M e a s u r e m e n t and Benchmarks . . . . . . . . . . . . . . . . . . . . . . . . 180
Detecting C o d e I s s u e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
A d d S o m e C a c h i n g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Chapter 10 — U n i t T e s t i n g 187
I n t r o d u c t i o n and B a c k g r o u n d . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

Enter P H P Un i t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
T e s t Fixt ures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
T e s t C a s e s and T e s t S u i t e s . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
M o c k s and S t u b s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
M o c k i n g W e b S e r v i c e s U s i n g P H P U n i t . . . . . . . . . . . . . . . . . . . . . . . 196
T e s t i n g the SOAP S e r v i c e S e t u p and a B a s i c C a l l . . . . . . . . . . . . . . . . . 196
T e s t i n g the REST S e r v e r R e s p o n s e . . . . . . . . . . . . . . . . . . . . . . . . . 197
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Chapter 11 — Security 203
I n t r o d u c t i o n and B a c k g r o u n d . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
C o u n t e r m e a s u r e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
U s e a Fire wal l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
U s e A d d i t i o n a l A p a c h e M o d u l e s . . . . . . . . . . . . . . . . . . . . . . . 205
Check your W e b S e r v e r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Check and A d a p t Y o u r C o d e . . . . . . . . . . . . . . . . . . . . . . . . . . 206
S e c u r i n g C o mm u n i c a ti o n . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
One S t e p F u r t h e r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Download from Wow! eBook <www.wowebook.com>
0 ” CONTENTS
Chapter 12 — End-User Documentation 211
I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
S u m m a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Chapter 13 — C o n c l u s i o n 219
Chapter 14 — A p p e n d i c e s 221
A p p e n d i x A: Development Environment S e t u p . . . . . . . . . . . . . . . . . 221
Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

Debian/Ubuntu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
F e d o r a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
C e n t O S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
M a c OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Wi nd ows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
P H P Un i t and F r i e n d s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
M e m c a c h e d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
A p p e n d i x B: S p e c i fi c T o o l s U s e d . . . . . . . . . . . . . . . . . . . . . . . . . . 226
A p a c h e mod_rewrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
S t r e a m W r a p p e r s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
GET, POST, PUT, DELETE using cURL . . . . . . . . . . . . . . . . . . . . 228
S o c k e t s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
A p p e n d i x C: P o t e n t i a l I m p a c t of ZF M o v i n g to 2.0 . . . . . . . . . . . . . . . . 229
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
Chapter 1
Z e n d F r a m e w o r k
I n t r o d u c t i o n
I n this first chapter we will try to get up and running as fast as possible b y showing
how to set up and configure your web server, and how a basic Zend F r a m e w o r k ap-
plication is constructed, which will stand us in good stead for the following chapters.
This book is conceived as a hands-on guide, exploring actual code and technical de-
tails where possible, so if you are interested in tinkering and playing around with
the code accompanying this book, you will need a working environment similar to
the one described. If you already have some r e a l life Zend F r a m e w o r k development
experience, you will be able to skim o v e r this chapter very quickly. If not, r e a d it
carefully and make sure you have everything working as it should before moving on.
Do not expect anything ground-breaking here. This book will get to r e a l life exam-
ples as quickly as possible, and we’ll keep this chapter short and clean.

WhyZend F r a m e w o r k ?
Zend F r a m e w o r k is a PH P framework consisting of a growing collection of compo-
nents. These components can be combined into a feature-complete MVC stack.
Alternatively, many of them may be used separately or in conjunction with other
frameworks and libraries.
Download from Wow! eBook <www.wowebook.com>
4 ” Zend F r a m e w o r k
Launched in 2006 b y Zend T e c h n o l o g i e s , I n c - the company founded b y the two
original authors of the Zend Engine - Zend F r a m e w o r k quickly gained traction to
become one of the better-known P H P frameworks. A t the time of writing, it has been
adopted b y a large number of companies and considerable parts of the community.
S o m e consider Zend F r a m e w o r k , and frameworks in general, to be bloated and
prefer their code custom-crafted for each task. Y e t whenever development teams
grow in size and customer demands grow even faster, the need for a clear applica-
tion structure and code r e u s a b i l i t y becomes more urgent. This is where frameworks
come into play, along with coding guidelines, unit testing, automatic API documen-
tation and C o n t i n u o u s I n t e g r a t i o n systems. All of these techniques and their rise in
popularity are part of a tendency in the P H P world towards more mature develop-
ment practices, more mature teams and enterprise r e a d i n e s s .
Growing numbers of professional P H P developers use these techniques, taking
their code to the next level using frameworks like Zend F r a m e w o r k , S y m f o n y , S o l a r,
K o h a n a , C a k e P H P and many, many others. S o m e of these frameworks are here to
stay, while some will eventually disappear, and yet others will grow to an even more
dominant position. Zend F r a m e w o r k , given its pedigree and the quality of the code,
is most likely here to stay.
All good and well, but is this the only r e a s o n that Zend F r a m e w o r k is chosen for
a book on web services? N o t at all. Firstl y, the components provided b y the frame-
work for web service creation are mature and well-designed. They offer good inte-
gration with the r e s t of the framework and are comprehensively documented. S e c -
ondly, the entire code is unit-tested and peer-reviewed. Thirdly, there is no licensing

fuss around Zend F r a m e w o r k : the code is created from scratch and all contributors
must sign an agreement which clearly r e g u l a t e s the licensing. Fina lly, I just like it.
I had to pick one of the available frameworks for this book and Zend F r a m e w o r k
seemed an obvious choice, at least to me.
F o r more information on Zend F r a m e w o r k , such as a detailed history and its posi-
tioning compared to other frameworks and solutions, there are plenty of online r e -
sources available. M a n y of these are centered around the Zend F r a m e w o r k website
1
.
Among other things, you will find the Zend F r a m e w o r k r e f e r e n c e manual there, as
well as the option to file a bug r e p o r t , and to browse the S u b v e r s i o n r e p o s i t o r i e s and
the development wiki.
1

Download from Wow! eBook <www.wowebook.com>
Zend F r a m e w o r k ” 5
If you are a Zend F r a m e w o r k novice, going through the quick-start tutorial will be
of great help and will introduce you to some key concepts in a very steady pace and
comprehensive way.
Also, do note that a major new version of Zend F r a m e w o r k will be available in the
coming months. On the website accompanying this book, the example code will
be updated for this new version, along with some detailed notes where necessary.
A p p e n d i x C has an o v e r v i e w of the potential impact of version 2.0 on our code.
Development Environment Setup
F o r your environment to be usable, you will need a web server and database server.
I chose A p a c h e and M y S Q L for this, since they are widely-used and well-known to
most P H P developers. As for the operating system, I am an U bu n t u and Debian Linux
user myself, but everything which follows should be possible on RPM-based distri-
butions, on Win dows, M a c OSX or even F r e e B S D . A p p e n d i x A has some pointers on
installing all the necessary tools on these other platforms.

I n s t a l l a t i o n of A p a c h e , P H P and accompanying services like M y S Q L can be done
using apt-get on Debian systems and distributions derived from it:
apt-get ins tall apache2 lib apache 2-mod- php5 php5 php5-common php5-cli php5-mysql
php5-xmlrpc mysql-client-5.1 mysql-common mysql-server
If you are prompted for input on some configuration choices, just accept the de-
faults, as they should be sufficient for now. Y o u should also enable the Apache
mod
_
rewrite module, using the command a2enmod rewrite.
Throughout this book the latest version of Zend F r a m e w o r k available at the time
of writing will be used, which is currently version 1.10.2. Zend provides a choice of
the full or minimal versions, with the latter containing only the library’s standard
components, and this version will serve us well. Download the framework from the
Zend F r a m e w o r k website
2
and unpack:
mkdi r -p /web/zfw s/ && cd /web/zfws
2

Download from Wow! eBook <www.wowebook.com>
6 ” Zend F r a m e w o r k
wget http://fr amework.zend. com/re leases /ZendF ramework-1.10 .2/Zen dFramework
-1.1 0.2-mi nimal. tar.gz
tar zxvf ZendF ramewo rk-1.1 0.2-mi nimal. tar.gz
mv Zend Framew ork-1. 10.2-m inimal /libra ry .
mv Zend Framew ork-1. 10.2-m inimal /bin .
rm -rf ZendF ramework-1.10 .2-min imal
The library/ directory contains all of the Zend F r a m e w o r k components. The bin/
directory contains the Zend_Tool scripts for bootstrapping our project code.
N o w , for ease of use, you could let the user and group running A p a c h e o w n the

entire source tree. On Debian Linux, you could do something like this:
chow n -R www-d ata.ww w-data /web/zfws
chmo d -R g+w /w eb/zfws
This makes the tree writable for the www-data group. Y o u can add your o w n user to
the www-data group b y adding www-data to your group memberships. R e p l a c e “jonas”
with your o w n username in the following command:
user mod -a -G www -data jonas
Do note that the above is not considered good practice for a production environ-
ment, since an A p a c h e user should not be able to write files all o v e r the place. M o r e
on this and security in general can be found in Chapter 11.
N o w let’s define a virtual host for A p a c h e , so that our web server knows where
to find the files for our site. The contents of the file you should create in
/etc/apache2/sites-available/zfws are as follows:
<Vir tualHo st
*
:80>
Serv erAdmi n
Serv erName zfws
Docu mentRo ot /we b/zfws/p ublic
<Dir ectory />
Allo wOverr ide None
</Directory>
<Dir ectory /w eb/zfws/public>
Opti ons Indexes Foll owSymL inks Mul tiViews
Allo wOverr ide All
Orde r allow,deny
Download from Wow! eBook <www.wowebook.com>
Zend F r a m e w o r k ” 7
allo w from all
</Directory>

sete nv APPLI CATION
_
ENV dev elopme nt
LogL evel warn
Erro rLog /var/log/apache2/zfws-error.log
Cust omLog /var/log/apache2/zf ws-acces s.log combined
</VirtualHost>
N o w enable the virtual host b y runninga2ensite zfws as root, and r e s t a r t the A p a c h e
process b y running /etc/init.d/apache2 reload. The only thing left to do to make
the site r e s p o n d to r e q u e s t s from our local development environment is point the
“ z f w s ” hostname to the 127.0.0.1 loopback address. S i m p l y add this to the end of
the /etc/hosts file:
127. 0.0.1 zfws
S i n c e nothing r e s i d e s under the public/ directory yet, there is nothing to see in a
browser, but we will soon change that. Also note that no library or application code
will be placed inside our public directory, ra th er it will be placed outside our docu-
ment root.
I n Chapter 2, our database, which will contain the data to be exposed through our
web services, will be created. Y o u can install M y S Q L 5+ using your package man-
ager, as we did when we executed the installation commands above, or use an install
package from the M y S Q L website
3
. A M y S Q L management tool such as M y S Q L A d -
ministrator (check under GUI T o o l s at the M y S Q L site) or good old phpMyAdmin
can help you to manage your database. M y S Q L also has a number of command-line
tools such as mysql and mysqladmin which allow you to manage your database using
low-level yet powerful commands. W e use the U b u n t u supplied M y S Q L 5.1 version,
but the code was also tested on M y S Q L 5.0. Y o u can check for your installed version
b y running this query:
SELE CT VERSION();

3

Download from Wow! eBook <www.wowebook.com>
8 ” Zend F r a m e w o r k
Final ly, you need a decent code editor or I n t e g r a t e d Development Environment
(IDE). N u m e r o u s alternatives are available these days, from the free N e t b e a n s and
Eclipse PDT packages to paid-for software such as P H P E d , K o m o d o IDE, Zend S t u -
dio and phpStorm. J u s t pick one you like and feel comfortable working in. A good
IDE is a r e a l asset and speeds up your development cycle. R e m e m b e r to use UTF-8
file encoding for all your P H P files to avoid for character encoding issues later on.
Bo otst ra p the P r o j e c t
Zend_Tool provides several useful scripts to help get a project started. This com-
mand will create most of the application directory layout:
cd /web /zfws
./bin/zf.sh creat e proje ct .
That command uses the zf.sh command-line tool, which in turn uses Zend_Tool to
generate the necessary directories containing basic Zend_Application-based MVC
code. The inner workings of Zend_Tool are beyond the scope of this work, and the
online Zend F r a m e w o r k r e f e r e n c e manual has in-depth information on the tool and
its use. There is a Win dows variant of the command line tool called zf.bat.
The three directories Zend_Tool created that are of most interest to us right now,
are as follows:
• application: our application-specific code will be found here
• library: this contains the Zend/ directory which holds all Zend F r a m e w o r k
components
• public: contains index.php, the default index file which will load the necessary
libraries from library/ and runsZend_Application, which in turn will load ev-
erything it needs from application/.
I n s i d e application you will find several more directories and a file named
Bootstrap.php. An o v e r v i e w of the folder layout is shown in the following example:

Download from Wow! eBook <www.wowebook.com>
Zend F r a m e w o r k ” 9
appl icatio n
| Bo otstrap.php
| co nfigs
| ‘ app licati on.ini
| co ntrollers
| | ErrorController.php
| ‘ Ind exCont roller .php
| models
‘ views
| he lpers
‘ scripts
| error
| ‘ error.phtml
‘ index
‘ index.pht ml
libr ary
publ ic
| .htaccess
‘ index.php
test s
The main application configuration file application.ini will look similar to the fol-
lowing:
[production]
phpS etting s.disp lay
_
star tup
_
erro rs = 0

phpS etting s.disp lay
_
erro rs = 0
incl udePat hs.lib rary = APP LICATI ON
_
PATH "/ /lib rary"
boot strap. path = APP LICATI ON
_
PATH "/Bootstrap.php"
boot strap. class = "Bootstrap"
appn amespa ce = "Applica tion"
reso urces. frontC ontrol ler.co ntroll erDire ctory = APPLI CATION
_
PATH "/controllers"
reso urces. frontC ontrol ler.pa rams.d isplay Except ions = 0
[staging : product ion]
[testing : product ion]
phpS etting s.disp lay
_
star tup
_
erro rs = 1
phpS etting s.disp lay
_
erro rs = 1
[development : pr oducti on]
phpS etting s.disp lay
_
star tup
_

erro rs = 1
phpS etting s.disp lay
_
erro rs = 1
reso urces. frontC ontrol ler.pa rams.d isplay Except ions = 1
Download from Wow! eBook <www.wowebook.com>
10 ” Zend F r a m e w o r k
This contains basic information for Zend_Application setup and configuration in-
formation for r e u s a b l e r e s o u r c e s , such as database connections, which will - more
or less automagically - be made available to the application. Bootstrap.php is also
part of the Zend_Application bootstrap flow and allows for the creation of additional
r e s o u r c e s which are not out-of-the-box supported as Zend_Application r e s o u r c e s .
Y o u can also manipulate r e s o u r c e s described in application.ini inside this class.
A common example of a r e s o u r c e which can be defined in application.ini is a
database connection, since a r e s o u r c e definition for it is provided for you b y the
framework, in library/Zend/Application/Resource/Db.php. On the other hand, a
caching layer using Zend_Cache would be an example of a r e s o u r c e you would cre-
ate and configure manually inside a class method in Bootstrap.php. That method
would be named something along the lines of
_
initCache(), as any methods whose
names begin with the prefix “_init” and are defined within the Bootstrap class will be
runautomatically b y the bootstrap procedure.
R e m e m b e r also that application.ini is parsed b y Zend_Config, and as such sup-
ports sections and inheritance between sections. There are sections for “ p r o d u c -
tion” and “ d e v e l o p m e n t ” , and development inherits most settings from “ p r o d u c -
tion”. I n our virtual host configuration file above, we used the SetEnv directive to
define an environment variable with the name APPLICATION
_
ENV and the value “ d e -

velopment”. This r e s u l t s in Zend_Application parsing the “ d e v e l o p m e n t ” section of
application.ini. This also makes it possible to define different database connec-
tions for production and development purposes and allow for more explicit error
display during development.
The application/controllers/ directory contains two controllers: the de-
fault index controller in IndexController.php, and an error controller in
ErrorController.php. The index controller contains the action method
indexAction(), while the error controller contains errorAction(). If you have
A p a c h e ’s mod_rewrite module enabled, URLs for your application are mapped
(thanks to some rules inside .htaccess) as follows: http://zfws/index/index,
http://zfws/index and http://zfws/ all map to the index action of the index
controller.
S u p p o s e there is an action method named detailAction() in the IndexController,
then http://zfws/index/detail and http://zfws/detail would both map to that ac-
tion. Errors like application errors (if for example an exception in the code has
Download from Wow! eBook <www.wowebook.com>
Zend F r a m e w o r k ” 11
been caught b y the MVC stack) are routed to the errorAction() method of the
ErrorController.
Do note that once you start working using modules (a separate directory contain-
ing views, controllers and models r e l a t i n g to a subset of the application’s functional-
ity), the controllers, views and models directly under the application folder are con-
sidered to be part of the default module. Creating a module is as simple as running
the commands:
cd /web /zfws
./bin/zf.sh creat e modul e admin
./bin/zf.sh creat e contr oller in dex index-action-included=1 admi n
This will create a modules directory containing controllers and an index view script.
Y o u can enable modules support in application.ini, b y adding the following lines:
reso urces. frontC ontrol ler.mo duleDi rector y = APPLIC ATION

_
PATH "/modules"
reso urces. module s[] = "admin"
N o w check the URLs http://zfws/admin/index and http://zfws/admin/index/index.
They should map to the index action in the index controller in
modules/admin/controllers/IndexController.php.
Zend F r a m e w o r k allows for heavy modification of the routes in your application to
make your URLs even more user friendly, or to define aliases when things are moved
around, for example during r e f a c t o r i n g .
T y p i c a l l y views are called from a controller action and bear a similar name. If you
point your browser to http://zfws you will see the r e s u l t i n g default Zend F r a m e w o r k
welcome page (see Figure 1.1). The index action inside IndexController.php will be
fired and the corresponding view in views/scripts/index/index.phtml will be parsed
and displayed as the r e s u l t i n g default page. Views are written in P H P. C o m b i n i n g
Zend F r a m e w o r k with a templating engine is quite possible but in general the built-
in view mechanism should give you plenty of flexibility.
M o d e l s , the last directory left unmentioned until now will contain model classes,
giving you an object-oriented way of r e t r i e v i n g and manipulating your data. The
use of models in Zend F r a m e w o r k has led to heated debates in the past. S o m e want
them to be lightweight and without much business logic inside, others place almost
Download from Wow! eBook <www.wowebook.com>
12 ” Zend F r a m e w o r k
Figure 1.1
Download from Wow! eBook <www.wowebook.com>
Zend F r a m e w o r k ” 13
the entire logic found in the application inside these models and make the V and C
parts of the MVC of less importance.
F o r the code being built throughout this book, we will make use of fairly simple
models, most of the time based on Zend_Db_Table, a T a b l e Data G a t ew a y
4

imple-
mentation which will provide a very convenient way of working with the data in our
database tables. The models more or less r e p r e s e n t the corresponding tables one-
to-one. C a l l s like $model->find(10) will r e t u r n an object of class Zend
_
Db
_
Table
_
Row
r e p r e s e n t i n g the row from the table where the primary key equals “10”. W e will see
more on this in the next few chapters, whenever we deal with the data in the M y S Q L
database.
N o w you might ask yourself: why do we need all this MVC code with views, routing,
controllers and r e s o u r c e s being bootstrapped if we are just going to build some web
services? F o r some cases, this is indeed unnecessary. A SOAP service could be run
from a procedural script which launches a SoapServer with only a few lines of code,
but on the other hand, both SOAP and REST services can be integrated well with
the Zend_Application MVC approach. The o v e r h e a d of launching a SOAP service
from the MVC stack and how to avoid it eventually will be covered further in Chapter
3, and we’ll look at performance in general in more depth in Chapter 9. F o r now,
we will take baby steps to arrive at multiple types of web services combined with a
simple Zend F r a m e w o r k application setup.
Don’t forget to double-check file o w n e r s h i p s and permissions on the freshly-
generated application files. Y o u need write access to be able to change them so run
the chown and chmod commands again if you need to.
S u m m a r y
Y o u should now have:
• a fresh development environment with a basic website inside its o w n virtual
host

• all the Zend F r a m e w o r k files and application bootstrap code in place for our
next steps
4
/>Download from Wow! eBook <www.wowebook.com>
14 ” Zend F r a m e w o r k
• M y S Q L r e a d y and waiting for the database which we are to import
S o , let’s move forward to the next chapter, where we’ll populate our database in
preparation for building web services on top of it.
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×