208

Zend Framework 2.0. Разработка веб-приложений

Embed Size (px)

Citation preview

  • 32.988.02-018 004.738.527

    .27 Zend Framework 2.0 -. .: , 2014. 208 .:

    . ( ). ISBN 978-5-496-00837-2

    Zend Framework 2 Zend Framework. -, . Zend Framework 2 -.

    - - Zend Framework 2. Zend Framework, , - ZF .

    12+ ( 29 2010 . 436-.)

    32.988.02-018 004.738.5

    Packt Publishing. . .

    , , , -. , , , .

    shutterstock.com.

    ISBN 9781782161929 . Packt Publisher, July 22, 2013ISBN 978-5-496-00837-2 , 2014 , , 2014

  • . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    1 . Zend Framework . . . . . . . . . . . . . . . . . . . . . 21

    2 . Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    3 . . . . . . . . . . . . . 52

    4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

    6 . . . . . . . . . . . . . . . . . . . . 116

    7 . Lucene . . . . . . . . . . . . . . . . 134

    8 . . . . . . . . . . . . . . . . . . . . . . . . . 149

    9 . HTML5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

    10 . . . . . . . . . . . . . . . . . . . 185

    . . . . . . . . . . . . . . . . . 201

  • . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    1 . Zend Framework . . . . . . . . . . . . . . . . . . . . . 21

    Zend Framework 2 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Zend Server CE . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    Zend Server CE . . . . . . . . . . . . . . . . . . . . . . 22 Zend Server CE . . . . . . . . . . . . . . . . 22 Zend Server CE . . . . . . . . . . . . . . . . . . . . . 25 Zend Server CE . . . . . . . . . . . . . . . . . 25 Zend Server CE . . . . . . . . . 25

    MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 MySQL . . . . . . . . . . . . . . . . . . . . . . 28 phpMyAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 . . . . . . . . . . . . . . . . . . 30

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

  • 7

    2 . Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 ZendSkeletonApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

    Zend Framework . . . . . . . . . 36 Zend Framework 2 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    Zend Framework . . . . . . . . . . . . . . . . . . . . 41 . . . . . . . . . . . . . . . . . . . . . . 42

    , , . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Zend Framework . . . . . . . . . . . . . . . . . . . . . . . 44

    . . . . 44 Zend Framework . . . . . . . . . . . . . . . . . . . . . 46

    . . . . . . . . . . 47 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    3 . . . . . . . . . . . . . 52

    Zend\Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 . . . . . . . . . 53

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Zend\InputFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 TableGateway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 . . . . . 63

    Zend\Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 . . . . . . . . . . . . 68

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

    4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    Zend Framework 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

    TableGateway . . . . . . . . . . . . . . . . . . . . . . . . . 77

  • 8

    . . . . . . . . . . . . . . . . . . . . . . . . . 78

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 . . . . . . . . . . 84

    . . . . . . . . . . . . . . . . . . . . . . . . . 88

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

    5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

    URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 BasePath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 HeadLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 HeadMeta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 HeadScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 HeadStyle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 HeadTitle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 jQuery UI

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 . . . . . . . . . . . . . . . . . . . . . . . . . . 102

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Zend\Mail\Transport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Zend\Mail\Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Zend\Mime\Message Zend\Mime\Part . . . . . . . . . . . . . . . . 109

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Zend\EventManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

    Zend Framework . . . . . . . . . . . . . . . . . . . . . 113

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

  • 9

    6 . . . . . . . . . . . . . . . . . . . . 116

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

    . . . . . . . . . . . . . . . . . . . . . . 119 . . . . . . . . . 120

    Google Data API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Google Photos API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Google Photos . . . . . 126

    YouTube Data API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 YouTube

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

    7 . Lucene . . . . . . . . . . . . . . . . 134

    Lucene . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 ZendSearch\Lucene . . . 135

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . 138

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Microsoft Office . . . . . . . . . . . . . . . . . . . . 144

    . . . . . . 145 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

    8 . . . . . . . . . . . . . . . . . . . . . . . . . 149

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 . . . . . . . . . . . . . . . . . . . . . . . . 153

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

    PayPal . . . . . . . . . . . . . . . . . . . . . . 156PayPal Zend Framework 2 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

  • 10

    PayPal . . . . . . 157 PayPal Express Checkout . . . . . . . . . . . . . . . . . . . . 158

    PayPal . . . . . . . . 160 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

    9 . HTML5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

    HTML5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 HTML5- . . . . . . . . . . . . . . . . 173

    HTML5- . . . . . 175

    HTML5- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176HTML5- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

    HTML5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

    10 . . . . . . . . . . . . . . . . . . . 185

    . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Zend Studio 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 phpCloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

    phpCloud 187PhoneGap Zend Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

    - . . . . . . . . . . . . . . . . . . . . . . 193 . . . . . . 194

    Zend Server Gateway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 197

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

    . . . . . . . . . . . . . . . . . 201

  • V - - PHP. Lister Technologies, .

    Zend PHP Zend Framework. - (, ) -- (, ).

    , , - . www .clickoffline .com, .

    , , .

    (Mukund Deverajan) , . (Apoorv Bhargava), (Jayabharathi and Souvik Sengupta) . Lister Technologies !

    (Wenbert S . Del Rosario), (Alex Frenkel) - (Islam Mohamed Abdel-aziz) .

    , (Antony Lowe), (Neeshma Ramakrishnan), (Veena Pagare) - Packt Publishing, , . Packt Publishing (Anugya Khurana) . - (Veena Manjrekar) , .

  • -, (Linux, CakePHP, Code Igniter, MySQL, jQuery, Knockout JS WordPress). , . Packt Publishing:

    , - Zend Framework 1.8 (Keith Pope, Zend Framework 1.8 Web Application Development);

    , - CouchDB PHP. - (Tim Juravich, CouchDB and PHP Web Development Beginners Guide).

    http://blog .ekini .net, , - . Twitter @wenbert.

    .

  • - 1998 ( - PHP 3.X) . Zend- PHP 5.3 LAMP- .

    ReutNet -, - OpenIview LTD., - IBM PHP-. , , -.

    GBooking Frenkel-Online.

    GBooking , -, - .

    Frenkel-Online , . , PHP-, , .

  • - - Zend Framework, Zend 2009 .

    - - . , - NoSQL, -, MapReduce.

    - , PHP5, Python Java.

    2008 - Oracle. , Python.

    ADVFN - . ADVFN .

    , , - .

  • , , , .

  • Zend Framework 2 - Zend Framework. -, . Zend Framework 2 -.

    - Zend Framework 2. Zend Framework, - , , .

    Zend Framework. , Zend Framework 2. MVC- Zend Framework 2. , , , . , , .

    - Zend Framework 2.

    1, Zend Framework, - . PHP-, MySQL , Zend Framework .

  • 2, Zend Framework, Zend Framework 2; MVC- Zend Framework 2, , . , , - .

    3, , - Zend/Form. - Zend Framework.

    4, , - Zend Framework. Zend Framework, , TableGateway, .

    5, , JavaScript . , JavaScript -, . , - Zend\Mail, Zend Framework.

    6, , - Zend Framework. Zend Framework, .

    7, Lucene, - Lucene Zend Framework. ZendSearch\Lucene, - .

    8, , . , . - PayPal Express Checkout.

    9, HTML5, HTML5 Zend Framework 2. Zend Framework, 2 - HTML5; HTML5 .

    17

  • 10, , Zend Framework 2 - Zend Studio 10. Zend Framework PHP-.

    , Zend Server CE MySQL. , , - 1.

    PHP-, Zend Framework, , . , - - PHP.

    -, . , ; - ?.

    , , .

    , -, .

    , , . - .

    : TableGateway AbstractTableGateway, TableGatewayInterface.

    18

  • :

    // $indexDoc = new Lucene\Document(); $indexDoc->addField($label); $indexDoc->addField($owner); $indexDoc->addField($fileUploadId); $index->addDocument($indexDoc);}// $index->commit();

    , - :

    // $indexDoc = new Lucene\Document(); $indexDoc->addField($label); $indexDoc->addField($owner); $indexDoc->addField($fileUploadId); $index->addDocument($indexDoc);}// $index->commit();

    , , :

    $ sudo apt-get install php5-cli$ sudo apt-get install git$ curl -s https://getcomposer.org/installer | php

    .

    , , - , : Select Destination Location ( ) Next (), , .

    , .

    19

  • , , , - http://www .packtpub .com.

    , , comp@piter .com ( , ).

    !

    - http://www .piter .com .

    20

  • Zend Framework

    , - Zend Framework 2.0. PHP-, MySQL , Zend Framework . , !

    Zend Framework 2 .0 Zend Framework ( 1.0) 2007 . 5 Zend Framework - PHP-. , Zend Framework 1.0.

    Zend Framework 2.0 Zend Framework, - . Zend Framework 2.0:

    PHP 5.3, ;

    ;

    ;

    (Dependency Injection, DI).

    Zend Framework 2.0 - .

    , Zend Framework 2.0. Zend Framework 2 -, PHP - 5.3.3 .

    1

  • Zend Server Community Edition -, . - PHP-, PHP 5.3.3. Apache PHP PHP Apache.

    , - Linux . , , Windows - .

    Zend Server CEZend Server Community Edition (CE) Zend Server. Zend Server PHP-, , . .

    Zend Server Zend Optimizer+ - PHP- Zend Guard .

    Zend Server CE Zend Server Windows, Mac OS X, , - Linux.

    Zend Server CE http://www .zend .com/en/products/server/system-requirements.

    Zend Server CE Zend Server CE. - Ubuntu 12.04 Precise Pangolin; . - Zend Server. Zend Server, .

    22 1 Zend Framework

  • 1. - Zend Server Community Edition (http://www .zend .com/en/community/zend-server-ce) Zend Server, - . - Linux.

    2. :

    $ tar -zxvf ZendServer-5.6.0-RepositoryInstaller-linux.tar.gz

    3. :

    $ cd ZendServer-RepositoryInstaller-linux/ $ sudo ./install_zs.sh 5.3 ce

    . PHP; 5 .3 . - Zend Server; ce ( Community Edition) .

    4. .

    5. Zend Server /usr/local/zend, /var/www. Zend Server :

    y Apache /etc/apache2/apache2 .conf;

    y PHP- /var/local/zend/etc/php .ini.

    Zend Server.

    Zend Server CE 23

  • 6. http://localhost -. .

    Zend Server, $ sudo service zend-server restart .

    ?

    Zend Server CE . - PHP, - Zend Framework 2.0.

    24 1 Zend Framework

  • Zend Framework Github - Git; Zend Framework 2.0 , SVN Git.

    Git http://www .git-scm .com/ - .

    Git http: //git-scm .com/book/en/Getting-Started-Installing-Git .

    Zend Server CE Zend Server CE , PHP-.

    Zend Server CE Zend Server CE - - :

    PHP-;

    PHP-;

    Zend Server;

    PHP , - .

    Zend Server, .

    Zend Server CE Zend Server .

    1. , , Zend Server (http://localhost:10081/).

    Zend Server 10081, - 80 . URL- .

    Zend Server CE 25

  • 2. Zend Server . c - End User License Agreement Zend Server .

    3. Zend Server.

    4. Zend Server.

    26 1 Zend Framework

  • 5. . :

    1) Directives () Server Setup ( );

    2) session.save_path;

    3) /tmp;

    4) Save Changes ( ), Restart PHP ( PHP).

    Zend Server CE 27

  • ?

    - Zend Server PHP, Zend Server.

    MySQLMySQL . , , - MySQL.

    Zend Framework 2.0 MySQL, SQLite, PostgreSQL Microsoft SQL Server.

    MySQL - . MySQL Linux-. Windows Mac - MySQL http://dev .mysql .com/downloads.

    Windows Mac , MySQL Zend Server CE . Zend Server Windows Mac MySQL Server .

    MySQL MySQL - ; MySQL .

    1. Ubuntu MySQL, - :

    $ sudo apt-get install mysql-server mysql-client

    2. MySQL- . , MySQL-, :

    $ sudo netstat -tap | grep mysql

    28 1 Zend Framework

  • 3. , , MySQL- :

    tcp 0 0 localhost:mysql *:* LISTEN 923/mysqld

    4. MySQL- - , - restart:

    $ sudo service mysql restart

    ?

    MySQL, LAMP1. MySQL-.

    Zend Server, php5-mysql . , MySQL , .

    , - .

    phpMyAdmin phpMyAdmin - - , PHP. phpMyAdmin - MySQL-, , , , . phpMyAdmin .

    , Apache, PHP MySQL, - MySQL-.

    phpMyAdmin Zend Server.

    1 Linux/Apache/PHP/MySQL. . .

    MySQL 29

  • phpMyAdmin http://www .phpmyadmin .net/ .

    phpMyAdmin http://docs .phpmyadmin .net/en/latest/setup .html .

    MySQL MySQL-, - .

    , phpMyAdmin - .

    1. phpMyAdmin - http://localhost/phpmyadmin.

    30 1 Zend Framework

  • 2. Databases ( ), zf_app Create new database ( ) Create ().

    3. . Add a new user ( ) Privileges (). :

    User name ( ) zf_user

    Host () localhost

    Password () zf_pass

    MySQL 31

  • .

    4. Privileges () - Edit Privileges ( ) zf_user.

    5. Database-specific privileges ( ) zf_app.

    6. zf_app zf_user. Check All ( ) Go () ( ).

    , phpMyAdmin zf_user. - zf_app.

    32 1 Zend Framework

  • ?

    MySQL . - . , .

    , - PHP- MySQL, Students phpMyAdmin.

    - PHP, - Students.

    MySQL 33

  • 1. PHP, Zend Framework 2.0?

    1) PHP 4.3 ;

    2) PHP 5.2.0 ;

    3) PHP 5.3.3 ;

    4) PHP 5.4.7 .

    2. php .ini Zend Server?

    1) /home//etc/php/php .inc;

    2) /etc/php/php .ini;

    3) /var/www/php .ini;

    4) /usr/local/zend/etc/php .ini.

    PHP- Zend Server. MySQL- - . Git phpMyAdmin.

    Zend Framework , .

    34 1 Zend Framework

  • Zend Framework

    Zend Framework 2.0. - Zend Framework 2.0, , . Zend Framework , .

    Zend Framework 2.0, , :

    - PHP;

    Git github.com;

    Composer PHP-.

    , Zend Framework 2.0.

    - PHP:

    $ sudo apt-get install php5-cli

    Git:

    $ sudo apt-get install git

    Composer:

    $ curl -s https://getcomposer.org/installer | php

    2

  • ZendSkeletonApplication ZendSkeletonApplication -, - Zend Framework 2.0. -- Zend Framework 2 --- (Model-View-Controller, MVC), . ZendSkeletonApplication GitHub (https://github .com/zendframework/ZendSkeletonApplication).

    Zend Framework Zend Framework, - ZendSkeletonApplication , Zend Framework. .

    1. , Zend Framework:

    $ cd /var/www/

    2. ZendSkeletonApplication GitHub:

    $ git clone git://github.com/zendframework/ ZendSkeletonApplication.git CommunicationApp

    Linux - , /var/www . , , - .

    36 2 Zend Framework

  • 3. Composer:

    $ cd CommunicationApp/ $ php composer.phar self-update $ php composer.phar install

    , Composer - .

    4. , , . Linux , /etc/hosts:

    $ sudo vim /etc/hosts

    Windows %SystemRoot%\system32\drivers\etc\hosts .

    5. hosts:

    127.0.0.1 comm-app.local

    hosts .

    ZendSkeletonApplication 37

  • 6. -; , :

    sudo vim /usr/local/zend/etc/sites.d/vhost_comm-app-80.conf

    , - . , - .

    , - Apache2 Linux, /etc/apache2/sites-available a2ensite comm-app .local .

    7. :

    ServerName comm-app.local DocumentRoot /var/www/CommunicationApp/public SetEnv APPLICATION_ENV "development" DirectoryIndex index.php AllowOverride All Order allow,deny Allow from all

    ZendSkeletonApplication, , DocumentRoot, Directory .

    8. -

    $ sudo service zend-server restart

    9. http://comm-app .local, .

    38 2 Zend Framework

  • mod_rewrite - . URL-, URL-, http://comm-app .local/12345 . Apache 404, .htaccess , , , , URL- .

    ZendSkeletonApplication 39

  • ?

    Zend Framework 2, - ZendSkeletonApplication GitHub, - Composer , Zend Framework 2.0. , public , -.

    Packt, - http://www .packtpub .com, . , - http://www .packtpub .com/support .

    ZendSkeletonAppli-cation, .

    - Composer

    $ php composer.phar create-project repository-url="http://packages.zendframe-work.com" zendframework/skeleton-application //

    Git - :

    $ git clone git://github.com/zendframework/

    ZendSkeletonApplication.git recursive

    http://framework .zend .com/downloads/skeleton-app .

    Zend Framework 2 .0 Zend Framework 2.0 -, , .

    Zend Framework, Zend Framework 2 - . Zend Framework 2 , . Zend Framework 2 , - .

    40 2 Zend Framework

  • Zend Framework 2.0:

    , , ;

    ;

    ;

    Phar Pyrus.

    Zend Framework Zend Framework 2 .

    config

    data , , ,

    module

    module/Application , ; ZendSkeletonApplication

    Zend Framework 2 .0 41

  • public -. -, , Java-

    vendor , . Zend Framework

    vendor/zendframe-work

    Zend Framework 2.0

    Users Zend Fra-me work 2.0. , - , . . ZendSkeletonModule Zend Framework.

    1. module :

    $ cd /var/www/CommunicationApp/ $ cd module/

    2. ZendSkeletonModule, - , Users:

    $ git clone git://github.com/zendframework/ZendSkeletonModule.git Users

    3. .

    42 2 Zend Framework

  • 4. Module .php, Users - (CommunicationApp/module/Users/module .php): - Users. namespace ZendSkeletonModule; namespace Users;.

    5. , :

    y Users/src/ZendSkeletonModule;

    y Users/view/zend-skeleton-module.

    ?

    - Zend Framework; , , . .

    ZFTOOL

    ZFTool Zend Framework; . - ZFTool create module:

    $ php composer.phar require zendframework/zftool:dev-master

    $ cd vendor/zendframework/zftool/

    $ php zf.php create module Users2 /var/www/CommunicationApp

    ZFTool http://framework .zend .com/manual/2 .0/en/modules/zendtool .introduction .html .

    , , , -- (Model-View-Controller, MVC), : , . , , - MVC-.

    . - .

    , -.

    , , 43

  • MVC-; . - -, . .

    MVC- Zend Framework 2 , - . , - , .

    Zend Framework

    Zend Framework 2.0 - , , . , :

    config

    src ,

    view ,

    , , . - , .

    1. :

    $ cd /var/www/CommunicationApp/module/Users

    2. :

    $ mkdir -p src/Users/Controller/

    44 2 Zend Framework

  • 3. IndexController /src//Controller/:

    $ cd src/Users/Controller/ $ vim IndexController.php

    4. IndexController:

  • 9. :

    y index;

    y login;

    y new-user.

    1) view/users/index/index .phtml :

    Welcome to Users Module Login | New User Registration

    2) /users/index/login .phtml :

    Login This page will hold the content for the login form

  • autoload_classmap .php: getAutoloaderConfig() -- autoload_classmap .php, - , , . , autoload_classmap .php.

    config/module .config .php: getConfig() config/module .config .php; , , , .

    Users, .

    1. . , - ZendSkeletonModule, . autoload_classmap .php, - :

  • 'users' => __DIR__ . '/../view', ), ),

    y . - . /users, index Index Users:

    'router' => array( 'routes' => array( 'users' => array( 'type' => 'Literal', 'options' => array( 'route' => '/users', 'defaults' => array( '__NAMESPACE__' => 'Users\Controller', 'controller' => 'Index', 'action' => 'index', ), ),

    3. - config/module .config .php :

  • 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( // , . // ; // // , , // . 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array(), ), ), ), ), ), ), ), 'view_manager' => array( 'template_path_stack' => array( 'users' => __DIR__ . '/../view', ), ), );

    4. . -: , config/application .config .php Users :

    'modules' => array( 'Application', 'Users', ),

    5. , http://comm-app .local/users/ -; .

    Zend Framework 49

  • .

    .

    ?

    ZendSkeletonModule , , Users. , - Zend Framework.

    , , , CurrentTime. :

    : 14:00:00 GMT : 12-Oct-2012

    50 2 Zend Framework

  • 1. ZendSkeletonApplication

    PHP-?

    1) Git;

    2) Composer;

    3) - PHP;

    4) Pyrus.

    2. ?

    1) /module//config .inc;

    2) //config/config .php;

    3) /module//module .config .php;

    4) /module//config/module .config .php.

    , Zend Framework . .

    51

  • - Zend Framework. - Zend Framework.

    , :

    Zend\Form;

    Zend\InputFilter;

    Zend\Validator;

    Zend\Db.

    Zend\Form : HTML-, , , . Zend Framework Zend\Form.

    Zend\Form - . , , . Zend Framework 2.

    3

  • , , . Users.

    1. . src/Users/Form/RegisterForm .php.

    1) RegisterForm Zend\Form\Form; - :

  • 'label' => 'Email', ), 'attributes' => array( 'required' => 'required' ), 'filters' => array( array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'EmailAddress', 'options' => array( 'messages' => array( \Zend\Validator\ EmailAddress::INVALID_FORMAT => 'Email address format is invalid' ) ) ) ) ));

    4) password, confirm_password submit; password confirm_password password, submit button.

    2. . .

    1) . src/view/users/register/index .phtml.

    2) : - , , - , , . :

    Register There were one or more issues with your submission. Please correct them as indicated below.

    3) HTML- - form, :

    54 3

  • 4) , - Name (), Email ( ), Password (), Confirm Password ( ) Submit ():

    5) , HTML- form :

    Zend\Form 55

  • 6) . ; src/view/users/register/confirm .phtml:

    Register Sucessfull Thank you for your registration.

    3. . , , , . RegisterController indexAction. src/Users/Controller/RegisterController .php:

  • 5. , - URL-:

    http://comm-app.local/users/register

    .

    ?

    , . , . http://comm-app .local/users/register, RegisterForm -. RegisterForm -:

    Name;

    Email;

    Password;

    Confirm Password;

    Submit.

    Zend\Form 57

  • Form. ViewModel , form , - FormElement.

    FormElement , Zend\Form\Element . - . - Form View Helpers Zend Framework http://framework .zend .com/manual/2 .0/en/modules/zend .form .view .helpers .html .

    , , . :

    Email;

    Password;

    Submit.

    .

    , , Email Address :

    'attributes' => array( 'required' => 'required'),'filters' => array( array('name' => 'StringTrim'),),'validators' => array( array( 'name' => 'EmailAddress',

    58 3

  • 'options' => array( 'messages' => array( \Zend\Validator\EmailAddress::INVALID_FORMAT => 'Email address format is invalid' )

    :

    , (required);

    , ;

    , , .

    Zend Framework - , . - Zend Framework. - -, , . .

    Zend\InputFilter Zend\InputFilter. - . , , $_GET $_POST, InputFilter .

    InputFilter - .

    InputFilter , - InputFilter .

    Zend\InputFilter 59

  • 1. InputFilter src/Users/Form/RegisterFilter .php. RegisterFilter Zend\InputFilter\InputFilter - :

  • 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 140, ), ), ), ));

    3) Password Confirm Password - , :

    'password' )); $this->add(array( 'name' => 'confirm_password', 'required' => true, ));

    3. InputFilter RegisterForm ; - . RegisterController, processAction - .

    4. RegisterController, processAction:

    public function processAction() { if (!$this->request->isPost()) { return $this->redirect()->toRoute(NULL , array( 'controller' => 'register', 'action' => 'index' )); } $post = $this->request->getPost(); $form = new RegisterForm(); $inputFilter = new RegisterFilter(); $form->setInputFilter($inputFilter); $form->setData($post); if (!$form->isValid()) { $model = new ViewModel(array( 'error' => true, 'form' => $form, )); $model->setTemplate('users/register/index'); return $model; }

    Zend\InputFilter 61

  • return $this->redirect()->toRoute(NULL , array( 'controller' => 'register', 'action' => 'confirm' )); }

    5. - , .

    ?

    . processAction() RegisterController - RegisterForm RegisterFilter $form->setInputFilter(). , , - , isValid(). FormElementErrors.

    , InputFilter .

    62 3

  • InputFilter Zend Framework. , InputFilter Login, .

    MVC-. Zend Framework Zend\Model, - . , , , , - - . - TableGateway .

    TableGateway , Zend Framework . SQL-, select, insert, update delete .

    TableGateway TableGateway , . TableGateway User.

    TableGateway -, exchangeArray() .

    , - MySQL TableGateway. UserTable, .

    63

  • 1. MySQL, :

    CREATE TABLE user ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, name TEXT NOT NULL, email VARCHAR(255) NOT NULL, password TEXT NOT NULL, PRIMARY KEY (id), UNIQUE INDEX idx_email(email) );

    2. , , /config/autoload/global .php:

    return array( 'db' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=test;host=localhost', 'username' => 'db_user', 'password' => '', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), ), 'service_manager' => array( 'factories' => array( 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', ), ), );

    3. User src/Users/Model/User .php:

  • 1) setPassword(), MD5 UserTable:

    public function setPassword($clear_password) { $this->password = md5($clear_password); }

    2) exchangeArray(), User UserTable:

    function exchangeArray($data) { $this->name = (isset($data['name'])) ? $data['name'] : null; $this->email = (isset($data['email'])) ? $data['email'] : null; if (isset($data["password"])) { $this->setPassword($data["password"]); } }

    5. User src/Users/Model/UserTable .php:

  • } else { if ($this->getUser($id)) { $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception('User ID does not exist'); } } } public function getUser($id) { $id = (int) $id; $rowset = $this->tableGateway->select(array('id' => $id)); $row = $rowset->current(); if (!$row) { throw new \Exception("Could not find row $id"); } return $row; } }

    6. UserTable - . , RegisterController. :

    protected function createUser(array $data) { $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User); $tableGateway = new \Zend\Db\TableGateway\TableGateway('user', $dbAdapter, null, $resultSetPrototype); $user = new User(); $user->exchangeArray($data); $userTable = new UserTable($tableGateway); $userTable->saveUser($user); return true; }

    TableGateway TableGateway:

    $table TableGateway;

    Adapter $adapter ;

    66 3

  • $features () API TableGateway, - , ( );

    ResultSet $resultSetPrototype () ResultSet;

    Sql $sql () SQL-; , SQL- , $table .

    http://framework .zend .com/manual/2 .0/en/modules/zend .db .table-gateway .html#zend-dbtablegateway .

    7. processAction() - :

    // $this->createUser($form->getData());

    8. - MySQL, , . , .

    MySQL.

    67

  • ?

    , . , .

    Zend\Authentication Zend\Authentication Zend Framework -, , HTTP- LDAP-. - .

    Zend\Authentication - , .

    Zend\Authentication.

    1. ( Users/Controller/LoginController .php) , :

    // use Zend\Authentication\AuthenticationService; use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter; // public function getAuthService() { if (! $this->authservice) { $dbAdapter = $this->getServiceLocator()->get( 'Zend\Db\Adapter\Adapter'); $dbTableAuthAdapter = new DbTableAuthAdapter( $dbAdapter,'user','email','password', 'MD5(?)'); $authService = new AuthenticationService(); $authService->setAdapter($dbTableAuthAdapter); $this->authservice = $authService; } return $this->authservice; }

    68 3

  • 2. processAction() LoginController - AuthService, authenticate:

    public function processAction() // $this->getAuthService()->getAdapter()->setIdentity( $this->request->getPost('email'))->setCredential( $this->request->getPost('password')); $result = $this->getAuthService()->authenticate(); if ($result->isValid()) { $this->getAuthService()->getStorage()->write( $this->request->getPost('email')); return $this->redirect()->toRoute(NULL , array( 'controller' => 'login', 'action' => 'confirm' )); }

    3. ConfirmAction -:

    public function confirmAction() { $user_email = $this->getAuthService()->getStorage()->read(); $viewModel = new ViewModel(array( 'user_email' => $user_email )); return $viewModel; }

    4. /view/users/login/confirm .phtml :

    Login Successful Welcome!

    5. , . .

    Zend\Authentication 69

  • .

    ?

    user , email password. .

    1.

    , ?

    1) /module//config .inc;

    2) /config/autoload/global .php;

    70 3

  • 3) /module//module .config .php;

    4) /module//config/module .config .php.

    2. ?

    1) $form->setInputFilter($inputFilter);

    2) $form->useInputFilter($inputFilter);

    3) $form->assignInputFilter($inputFilter);

    4) $form->mapInputFilter($inputFilter).

    , , , . TableGateway, - .

    71

  • , , , - Zend Framework.

    :

    Zend Framework 2;

    TableGateway;

    Zend Framework.

    Zend Framework 2 Zend Framework 2 . /, .

    6 ; / - , .

    abstract_factories

    aliases /

    factories / . , , Zend/ServiceManager/FactoryInterface

    4

  • invokables / . , -

    services / . . ,

    shared / , , . , ,

    ; , . -, , , ; .

    , , .

    1. , Module, - getServiceConfig(). , :

    public function getServiceConfig() { return array( 'abstract_factories' => array(), 'aliases' => array(), 'factories' => array(), 'invokables' => array(), 'services' => array(), 'shared' => array(), ); }

    2. service_manager; , .

    3. - config/autoload/ :

    Zend Framework 2 73

  • return $table; }, 'UserTableGateway' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new User()); return new TableGateway('user', $dbAdapter, null, $resultSetPrototype); },

    // 'LoginForm' => function ($sm) { $form = new \Users\Form\LoginForm(); $form->setInputFilter($sm->get('LoginFilter')); return $form; }, 'RegisterForm' => function ($sm) { $form = new \Users\Form\RegisterForm(); $form->setInputFilter($sm->get('RegisterFilter')); return $form; },

    // 'LoginFilter' => function ($sm) { return new \Users\Form\LoginFilter(); }, 'RegisterFilter' => function ($sm) { return new \Users\Form\RegisterFilter(); }, ), 'invokables' => array(), 'services' => array(), 'shared' => array(), ); }

    2. , Module .php :

    use Users\Model\User; use Users\Model\UserTable;

    use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway;

    3. , :

    Zend Framework 2 75

  • // $form = $this->getServiceLocator()->get('LoginForm'); // $userTable = $this->getServiceLocator()->get('UserTable');

    namespace use, PHP 5 .3 . Zend Framework 2 , , . , , - .

    use - , , as . :

    use Zend\Form\Element as Element;

    use Zend\Form\Element; // ,

    4. , , -.

    ?

    , - . , - .

    , , , . (CommunicationApp/module/Users/src/Users/Controller/LoginController .php) getAuthService() . - , .

    , -, (table insert).

    76 4

  • , CRUD1-.

    TableGateway TableGateway AbstractTableGateway, - TableGatewayInterface . TableGatewayInterface ; .

    interface Zend\Db\TableGateway\TableGatewayInterface{ public function getTable(); public function select($where = null); public function insert($set); public function update($set, $where = null); public function delete($where);}

    TableGateway - ; .

    getTable(): , , TableGateway. :

    $myTableName = $myTableGateway->getTable();

    select($where = null): , - , $where; where Zend\Db\Sql\Where . :

    $rowset = $myTableGateway->select( array('id' => 2));

    insert($set): , $set, . :

    $myTableGateway->insert( array('id' => 2, 'name'=>'Ravi'));

    update($set, $where = null): , - , $where;

    1 reate, Read, Update, Delete (, , , ). . .

    77

  • where Zend\Db\Sql\Where -. $set , , $where. :

    $rowset = $myTableGateway->update(array('name' => 'Jerry'), array('id' => 2));

    delete($where): , - , $where; where Zend\Db\Sql\Where . :

    $myTableGateway->delete( array('id' => 2));

    getLastInsertValue(): . . :

    $myTableGateway->insert( array('name'=>'Ravi')); $insertId = $myTableGateway-> getLastInsertValue ();

    - . , , , , .

    1. CommunicationApp/module/Users/src/Users/Model/UserTable .php - , :

    y fetchAll();

    y getUser($id);

    y getUserByEmail($userEmail);

    y deleteUser($id).

    public function fetchAll() { $resultSet = $this->tableGateway->select(); return $resultSet;

    78 4

  • } public function getUser($id) { $id = (int) $id; $rowset = $this->tableGateway->select(array('id' => $id)); $row = $rowset->current(); if (!$row) { throw new \Exception("Could not find row $id"); } return $row; } public function getUserByEmail($userEmail) { $rowset = $this->tableGateway->select(array('email' => $userEmail)); $row = $rowset->current(); if (!$row) { throw new \Exception("Could not find row $ userEmail"); } return $row; } public function deleteUser($id) { $this->tableGateway->delete(array('id' => $id)); }

    2. CommunicationApp/module/Users/src/Users/Controller/UserManagerController .php.

    3. UserManagerController -:

    y indexAction(). -; / , :

    $userTable = $this->getServiceLocator()->get('UserTable'); $viewModel = new ViewModel(array( 'users' => $userTable->fetchAll())); return $viewModel;

    y editAction(). edit, - , :

    $userTable = $this->getServiceLocator()->get('UserTable'); $user = $userTable->getUser($this->params()->fromRoute('id')); $form = $this->getServiceLocator()->get('UserEditForm'); $form->bind($user); $viewModel = new ViewModel(array(

    79

  • 'form' => $form, 'user_id' => $this->params()->fromRoute('id') )); return $viewModel;

    BIND

    bind, Form, . : , , , - ($form->isValid()) .

    http://framework .zend .com/manual/2 .2/en/modules/zend .form .quick-start .html#binding-an-object .

    y processAction(). processAction edit; processAction - indexAction.

    // POST $post = $this->request->getPost(); $userTable = $this->getServiceLocator()->get('UserTable'); // User $user = $userTable->getUser($post->id); // User Form $form = $this->getServiceLocator()->get('UserEditForm'); $form->bind($user); $form->setData($post); // $this->getServiceLocator()->get('UserTable')->saveUser($user);

    y deleteAction(). :

    $this->getServiceLocator()->get('UserTable') ->deleteUser($this->params()->fromRoute('id'));

    4. config/module .config .php, :

    'user-manager' => array( 'type' => 'Segment', 'options' => array( 'route' => '/user-manager[/:action[/:id]]', 'constraints' => array( 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'Users\Controller\UserManager',

    80 4

  • 'action' => 'index', ), ), ),

    5. , invokables:

    'Users\Controller\UserManager' => 'Users\Controller\UserManagerController',

    6. - , - http://comm .-app .local/users/user-manager. , .

    Edit () .

    81

  • Delete () .

    ?

    , . TableGateway CRUD- .

    TableGateway.

    , . , Add User ( ).

    , - . . .

    82 4

  • . , . , -, .

    . , , - , .

    Zend Framework, - .

    (Zend\Form\Element\File). File - . HTML- , . 'type' => 'file' .

    83

  • (Zend\File\Transfer\Adapter\Http). . setDestination() - . receive() .

    ; - , uploads . , - , .

    1. ( config/module .config .php) , :

  • $rowset = $this->tableGateway->select( array('user_id' => $userId)); return $rowset; }

    4. UploadManagerController . indexAction() , - :

    $uploadTable = $this->getServiceLocator()->get('UploadTable'); $userTable = $this->getServiceLocator()->get('UserTable'); // $userEmail = $this->getAuthService()->getStorage()->read(); $user = $userTable->getUserByEmail($userEmail);

    $viewModel = new ViewModel( array( 'myUploads' => $uploadTable->getUploadsByUserId($user->id), )); return $viewModel;

    5. , :

    $this->add(array( 'name' => 'fileupload', 'attributes' => array( 'type' => 'file', ), 'options' => array( 'label' => 'File Upload', ), ));

    .

    85

  • 6. index. , . Zend Framework HTTP, . get('config') . - :

    public function getFileUploadLocation() { // $config = $this->getServiceLocator()->get('config'); return $config['module_config']['upload_location']; }

    7. . , .

    1) , .

    2) 'uploads' , , :

    $uploadFile = $this->params()->fromFiles('fileupload'); $form->setData($request->getPost()); if ($form->isValid()) { // $uploadPath = $this->getFileUploadLocation();

    // $adapter = new \Zend\File\Transfer\Adapter\Http(); $adapter->setDestination($uploadPath); if ($adapter->receive($uploadFile['name'])) { // $exchange_data = array(); $exchange_data['label'] = $request->getPost()->get('label'); $exchange_data['filename'] = $uploadFile['name']; $exchange_data['user_id'] = $user->id;

    $upload->exchangeArray($exchange_data); $uploadTable = $this->getServiceLocator()->get('UploadTable'); $uploadTable->saveUpload($upload);

    return $this->redirect()-> toRoute('users/upload-manager', array('action' => 'index' )); } }

    86 4

  • 8. ( ) Upload Manager, invokables.

    9. - .

    .

    ?

    , . Zend Framework. - , . , .

    Delete (), , . .

    87

  • , , - . . upload_sharing.

    , - upload_sharing , . , .

    1. upload_sharing, :

    CREATE TABLE IF NOT EXISTS uploads_sharing ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, upload_id INT NOT NULL, user_id INT NOT NULL, UNIQUE KEY (upload_id, user_id) );

    2. Module.php TableGateway uploads_sharing:

    'UploadSharingTableGateway' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); return new TableGateway('uploads_sharing', $dbAdapter); },

    3. UploadTable , - TableGateway:

    public function __construct(TableGateway $tableGateway, TableGateway $uploadSharingTableGateway) { $this->tableGateway = $tableGateway; $this->uploadSharingTableGateway = $uploadSharingTableGateway; }

    88 4

  • 4. (Module .php) UploadTable, - UploadSharingTableGateway:

    'UploadTable' => function($sm) { $tableGateway = $sm->get('UploadTableGateway'); $uploadSharingTableGateway = $sm->get('UploadSharingTableGateway'); $table = new UploadTable($tableGateway, $uploadSharingTableGateway); return $table; },

    5. UploadTable - :

    y addSharing() ;

    y removeSharing() ;

    y getSharedUsers() , ;

    y getSharedUploadsForUserId() , .

    :

    public function addSharing($uploadId, $userId) { $data = array( 'upload_id' => (int)$uploadId, 'user_id' => (int)$userId, ); $this->uploadSharingTableGateway->insert($data); }

    public function removeSharing($uploadId, $userId) { $data = array( 'upload_id' => (int)$uploadId, 'user_id' => (int)$userId, ); $this->uploadSharingTableGateway->delete($data); }

    public function getSharedUsers($uploadId) { $uploadId = (int) $uploadId;

    89

  • $rowset = $this->uploadSharingTableGateway->select( array('upload_id' => $uploadId)); return $rowset; }

    public function getSharedUploadsForUserId($userId) { $userId = (int) $userId; $rowset = $this->uploadSharingTableGateway->select( function (Select $select) use ($userId){ $select->columns(array()) ->where(array('uploads_sharing.user_id'=>$userId)) ->join('uploads', 'uploads_sharing.upload_id = uploads.id'); }); return $rowset; }

    Manage Documents ( ) , , , - .

    6. , , . getSharedUsers() UploadTable.

    7. , ; .

    90 4

  • Add Share ( ), upload_sharing .

    $userTable = $this->getServiceLocator()->get('UserTable'); $uploadTable = $this->getServiceLocator()->get('UploadTable'); $form = $this->getServiceLocator()->get('UploadForm'); $request = $this->getRequest(); if ($request->isPost()) { $userId = $request->getPost()->get('user_id'); $uploadId = $request->getPost()->get('upload_id'); $uploadTable->addSharing($uploadId, $userId); }

    Upload Sharing ( - ) .

    8. . - , , fileDownloadAction():

    public function fileDownloadAction() { $uploadId = $this->params()->fromRoute('id'); $uploadTable = $this->getServiceLocator()->get('UploadTable'); $upload = $uploadTable->getUpload($uploadId);

    91

  • // $uploadPath = $this->getFileUploadLocation(); $file = file_get_contents($uploadPath ."/" . $upload->filename);

    // $response = $this->getEvent()->getResponse(); $response->getHeaders()->addHeaders(array( 'Content-Type' => 'application/octet-stream', 'Content-Disposition' => 'attachment;filename= "'.$upload->filename . '"', )); $response->setContent($file); return $response; }

    , . - , HTTP- ( . - ) . setTerminal(), :

    $result = new ViewModel();

    $result->setTerminal(true);

    return $result;

    file_get_contents() - . - HTTP- Zend\Http\Response\Stream() .

    9. , . : - .

    92 4

  • .

    ?

    - . UploadTable, . , , . , , .

    1. TableGateway

    ?

    1) getLastId();

    2) getLastInsertId();

    3) get('last_insert_id') ;

    4) getLastInsertValue().

    93

  • 2. -?

    1) $viewModel->setNoLayouts(true);

    2) $ viewModel->Layouts(false);

    3) $viewModel->setTerminal(true);

    4) $viewModel->setLayouts(false).

    , . , TableGateway . Zend Framework, -. , , Zend Framework , TableGateway. , JavaScript AJAX.

    94 4

  • - -, , JavaScript- CSS-. Zend Framework --- (Model-View-Controller, MVC), , . - Zend Framework 2.0 , .

    , - Zend Framework 2.0 . , :

    JavaScript- Zend Framework 2.0;

    Zend Frame-work 2.0 JavaScript;

    Zend\Mail ;

    Zend Framework.

    MVC Zend Framework - -; , -. HTML-, .

    - ; -

    5

  • . (two-step view).

    , view_manager, - .

    .....

    ......

    ........

    ....... FOOTER ...

    Layout

    View

    , - HTML-: , , , , , , .

    Zend Framework 2 , - . , , Zend\View\HelperInterface.

    , Zend Framework 2.

    URL URL :

    url($name, $urlParams, $routeOptions = array(), $reuseMatchedParams = array())

    96 5

  • URL- . URL- , -. :

  • - (placeholder helpers). , - . , - HTML- , , - headLink appendStylesheet .

    HeadLink HeadLink HTML- head CSS- .

    HeadLink:

    appendStylesheet($href, $media, $conditionalStylesheet, $extras)offsetSetStylesheet($index, $href, $media, $conditionalStylesheet, $extras)prependStylesheet($href, $media, $conditionalStylesheet, $extras)setStylesheet($href, $media, $conditionalStylesheet, $extras)

    Link HTML-/ - .

    HeadMeta HeadMeta HTML- head; meta.

    HeadMeta:

    appendName($keyValue, $content, $conditionalName)offsetSetName($index, $keyValue, $content, $conditionalName)prependName($keyValue, $content, $conditionalName)setName($keyValue, $content, $modifiers)appendHttpEquiv($keyValue, $content, $conditionalHttpEquiv)offsetSetHttpEquiv($index, $keyValue, $content, $conditionalHttpEquiv)prependHttpEquiv($keyValue, $content, $conditionalHttpEquiv)setHttpEquiv($keyValue, $content, $modifiers)setCharset($charset)

    meta HTML-/ - .

    98 5

  • HeadScript HeadScript HTML- head; JavaScript- HTML- head.

    HeadScript:

    appendFile($src, $type = 'text/javascript', $attrs = array())offsetSetFile($index, $src, $type = 'text/javascript', $attrs =array())prependFile($src, $type = 'text/javascript', $attrs = array())setFile($src, $type = 'text/javascript', $attrs = array())appendScript($script, $type = 'text/javascript', $attrs =array())offsetSetScript($index, $script, $type = 'text/javascript', $attrs = array())prependScript($script, $type = 'text/javascript', $attrs = array())setScript($script, $type = 'text/javascript', $attrs = array())

    Script HTML-/ - .

    HeadStyle HeadStyle HTML- head; HTML- head.

    HeadStyle:

    appendStyle($content, $attributes = array())offsetSetStyle($index, $content, $attributes = array())prependStyle($content, $attributes = array())setStyle($content, $attributes = array())

    Style HTML-/ - .

    HeadTitle HeadTitle HTML- head; headTitle() - , /.

    99

  • $setType, - ; APPEND, PREPEND SET (). :

    headTitle($title, $setType = null);

    Title HTML-/ - .

    jQuery UI , jQuery UI, jQuery UI.

    1. , -; Login ( ) Register () , jQuery UI.

    2. Login Register index ( module/Users/view/users/index/index .html) ui-button , :

    Login Register

    3. jQuery UI :

    100 5

  • // jQuery UI $this->headScript()->appendFile( 'http://code.jquery.com/jquery-1.8.3.js','text/javascript');

    $this->headScript()->appendFile( 'http://code.jquery.com/ui/1.10.0/jquery-ui.js','text/javascript');

    // jQuery UI $this->headLink()->appendStylesheet( 'http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css');

    JAVASCRIPT-

    - /public - appendFile appendStylesheet . - basePath() URL- .

    4. , :

    // $this->headScript()->appendScript( '$(function() { $("a.ui-button").button(); });', 'text/javascript');

    5. , Login Register, jQuery UI.

    View Source ( ) - headScript(), :

    101

  • ... ... // ... ...

    ?

    Zend Framework - JavaScript-; JavaScript- HTML- head headScript().

    JavaScript-; - , HTML- head.

    , . , , JavaScript-, jQuery UI -, public/ , jQuery UI.

    jQuery UI http://jqueryui .com/.

    , - . : ,

    102 5

  • . , .

    1. chat_messages

    :

    CREATE TABLE IF NOT EXISTS chat_messages ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, message VARCHAR( 255 ) NOT NULL, stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP )

    2. CommunicationApp/module/ Users/src/Users/Controller/GroupChatController .php.

    3. CommunicationApp/module/Users/config/module .config .php -:

    // 'Users\Controller\GroupChat' => 'Users\Controller\ GroupChatController',

    // 'group-chat' => array( 'type' => 'Segment', 'options' => array( 'route' => '/group-chat[/:action[/:id]]', 'constraints' => array( 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'Users\Controller\GroupChat', 'action' => 'index', ), ), ),

    4. CommunicationApp/module/Users/view/users/groupchat/index .phtml:

    103

  • foreach($chatMessages as $chatMessage) { $fromUser = $userTable->getUser($chatMessage->user_id); $messageData = array(); $messageData['user'] = $fromUser->name; $messageData['time'] = $chatMessage->stamp; $messageData['data'] = $chatMessage->message; $messageList[] = $messageData; }

    $viewModel = new ViewModel(array('messageList' =>$messageList)); $viewModel->setTemplate('users/group-chat/message-list'); $viewModel->setTerminal(true); return $viewModel; }

    6. CommunicationApp/module/Users/view/users/group-chat/message-list .phtml, $messageList:

    [] :

    7. sendMessage(), , - , , - . CommunicationApp/module/Users/src/Users/Controller/GroupChatController .php.

    protected function sendMessage($messageTest $fromUserId) {

    105

  • $chatMessageTG = $this->getServiceLocator() ->get('ChatMessagesTableGateway'); $data = array( 'user_id' => $fromUserId, 'message' => $messageTest, 'stamp' => NULL ); $chatMessageTG->insert($data); return true; }

    8. indexAction , Send Message ( ) sendMessage() . indexAction ( CommunicationApp/module/Users/src/Users/Controller/ GroupChatController .php).

    public function indexAction( { $user = $this->getLoggedInUser(); $request = $this->getRequest(); if ($request->isPost()) { $messageTest = $request->getPost()->get('message'); $fromUserId = $user->id; $this->sendMessage($messageTest, $fromUserId); // return $this->redirect()->toRoute('users/group-chat'); }

    // $form = new \Zend\Form\Form();

    $form->add(array( 'name' => 'message', 'attributes' => array( 'type' => 'text', 'id' => 'messageText', 'required' => 'required' ), 'options' => array( 'label' => 'Message', ), ));

    $form->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'submit',

    106 5

  • 'value' => 'Send' ), ));

    $form->add(array( 'name' => 'refresh', 'attributes' => array( 'type' => 'button', 'id' => 'btnRefresh', 'value' => 'Refresh' ), ));

    $viewModel = new ViewModel( array('form' => $form, 'userName' => $user->name)); return $viewModel; }

    9. - .

    ?

    Zend Framework. . ; Zend Framework .

    , . Refresh (), - iframe. JavaScript-, 5 , .

    107

  • Zend Framework Zend\Mail, . Zend Framework - .

    Zend\Mail , - MIME- . Sendmail, SMTP File; Zend\Mail\Transport\TransportInterface.

    Zend\Mail\Transport Mail - ; Zend\Mail :

    Sendmail Zend\Mail\Transport\Sendmail;

    SMTP Zend\Mail\Transport\Smtp;

    File Transport Zend\Mail\Transport\File.

    Mail send(); Zend\Mail\Message, - . .

    Zend\Mail\Message Zend\Mail\Message Zend Framework. , , , . MIME- , Zend\Mime\Message setBody(), . Zend\Mail\Message:

    setFrom();

    setHeaders;

    108 5

  • setTo();

    addCc() addBcc();

    setSubject();

    setBody().

    Zend\Mime\Message Zend\Mime\Part HTML- Zend\Mime\Part Zend\Mime\Message setParts(). Zend\Mime\Message Zend\Mail\Message setBody().

    , Zend Framework .

    1. , .

    2. -.

    3. , Zend\Mail:

    use Zend\Mail;

    4. , - ; ( CommunicationApp/module/Users/src/Users/Controller/GroupChatController .php) :

    protected function sendOfflineMessage( $msgSubj, $msgText, $fromUserId, $toUserId) { $userTable = $this->getServiceLocator()->get('UserTable');

    $fromUser = $userTable->getUser($fromUserId); $toUser = $userTable->getUser($toUserId);

    109

  • $mail = new Mail\Message(); $mail->setFrom($fromUser->email, $fromUser->name); $mail->addTo($toUser->email, $toUser->name); $mail->setSubject($msgSubj); $mail->setBody($msgText);

    $transport = new Mail\Transport\Sendmail(); $transport->send($mail);

    return true; }

    Linux Sendmail (Zend\Mail\Transport\Sendmail) - - . Windows SMTP (Zend\Mail\Transport\Smtp), SMTP- . - SMTP: https://packages .zendframework .com/docs/latest/manual/en/modules/zend .mail .transport .html#zend-mail-transport-quick-start-smtp-usage .

    5. - , . , .

    110 5

  • ?

    Zend\Mail Sendmail. HTML- .

    , HTML.

    Zend\EventManagerZend Framework 2 , ; . Zend Framework .

    EventManager, - .

    , .

    , .

    attach() trigger() . MVC-

    Zend\EventManager 111

  • ; MVC- .

    Bootstrap

    Route

    Dispatch

    Finish

    View

    Render

    DispatchController

    Routing/Dispatch Error

    Dispatch.error

    Application

    Renderer

    Response

    Zend Framework 2 http://akrabat .com/zend-framework-2/a-list-of-zf2-events/ .

    .

    1. Zend\Mvc\Application: Bootstrap.

    2. Zend\Mvc\Application: Route.

    3. Zend\Mvc\Application: Dispatch.

    4. Zend\Mvc\Controller\ActionController: Dispatch ( - ).

    5. Zend\Mvc\Application: Render.

    6. Zend\View\View: Renderer.

    7. Zend\View\View: Response.

    8. Zend\Mvc\Application: Finish.

    - .

    1. Zend\Mvc\Application: Dispatch.error.

    112 5

  • 2. Zend\Mvc\Application: Render.

    3. Zend\View\View: Renderer.

    4. Zend\View\View: Response.

    5. Zend\Mvc\Application: Finish.

    - Zend Framework.

    Zend Framework1. My Account ( )

    CommunicationApp/module/Users/view/layout/myaccount-layout .phtml.

    2. view_manager -> template_map ( CommunicationApp/module/Users/config/module .config .php):

    'layout/myaccount' => __DIR__ . '/../view/layout/myaccount-layout.phtml',

    3. CommunicationApp/module/Users/module .php MvcEvent:

    use Zend\Mvc\MvcEvent;

    4. onBootStrap(), :

    public function onBootstrap($e) { $eventManager = $e->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager);

    $sharedEventManager = $eventManager->getSharedManager(); // // $sharedEventManager->attach( __NAMESPACE__, MvcEvent::EVENT_DISPATCH, function($e) { $controller = $e->getTarget(); // $controllerName = $controller->getEvent() ->getRouteMatch()->getParam('controller'); if (!in_array($controllerName, array( 'Users\Controller\Index', 'Users\Controller\Register', 'Users\Controller\Login'))) {

    Zend\EventManager 113

  • $controller->layout('layout/myaccount'); } }); }

    5. Communication Application ( ) -. .

    6. .

    114 5

  • ?

    Zend Framework - Dispatch . - . , , , default, layout.

    1. /

    CSS- HTML- head?

    1) HeadLink;

    2) HeadScript;

    3) HeadCss;

    4) HeadStyle.

    2. - Zend Framework 2?

    1) Zend\Mail\Transport\Pop;

    2) Zend\Mail\Transport\Smtp;

    3) Zend\Mail\Transport\Imap;

    4) Zend\Mail\Transport\File.

    : - JavaScript-, , Zend\Mail . Zend Framework. Zend Framework, .

    115

  • - . - , Google, Flickr YouTube. Zend Framework 1.0 Zend_Service; Zend Framework 2 .

    Zend Framework 2.0 . - :

    Zend Framework;

    ;

    , - WebinoImageThumb;

    Zend GData;

    GData Google Photos YouTube.

    Zend Framework 2.0 PHP-, ( Composer).

    PHP-, - . - .

    6

  • -, Google.

    COMPOSER

    Composer , Zend Framework . Composer - . composer .json .

    Zend Framework 1.0 , ; Zend Framework 2.0 . . .

    1. Zend Framework 2: http://modules .zendframework .com/.

    2. WebinoImageThumb.

    3. , composer .json .

    4. , CommunicationApp/composer .json, - :

    "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.0.*", "webino/webino-image-thumb": "1.*", }

    5. composer.phar update, - :

    $ php composer.phar update Loading composer repositories with package information Updating dependencies - Installing webino/webino-image-thumb (1.0.0)

    117

  • Downloading: 100% Writing lock file Generating autoload files

    6. vendor, .

    7. , , CommunicationApp/config/application .config .php, 'WebinoImageThumb' modules:

    return array( 'modules' => array( 'Application', 'WebinoImageThumb', 'Users', ),

    ?

    Composer. , .

    , , Zend Framework 2, :

    118 6

  • ZendGData. https://packages .zendframework .com/. .

    , Zend Framework 2. , .

    Upload; thumbnail , . \data\images. .

    , , WebinoImageThumb:

    resize($maxWidth = 0, $maxHeight = 0)

    ; - 0, - .

    adaptiveResize($width, $height)

    - , ( ), .

    crop($startX, $startY, $cropWidth, $cropHeight)

    , .

    rotateImage($direction = 'CW')

    90 - .

    rotateImageNDegrees($degrees)

    , .

    119

  • save($fileName, $format = null)

    .

    1. ImageUpload

    :

    CREATE TABLE IF NOT EXISTS image_uploads ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, filename VARCHAR( 255 ) NOT NULL, thumbnail VARCHAR( 255 ) NOT NULL, label VARCHAR( 255 ) NOT NULL, user_id INT NOT NULL, UNIQUE KEY (filename) );

    2. ImageUpload src/Users/Model/ ImageUpload .php, TableGateway src/Users/Model/ImageUploadTable .php Controller (MediaManagerController) CommunicationApp/module/Users src/Users/Controller/MediaManagerController .php.

    3. Submit Upload generateThumbnail(); - . resize , 7575 , tn_.

    MediaManagerCon-troller (src/Users/Controller/MediaManagerController .php).

    public function generateThumbnail($imageFileName) { $path = $this->getFileUploadLocation(); $sourceImageFileName = $path . '/' . $imageFileName; $thumbnailFileName = 'tn_' . $imageFileName;

    $imageThumb = $this->getServiceLocator()->get('WebinoImageThumb'); $thumb = $imageThumb->create($sourceImageFileName, $options = array()); $thumb->resize(75, 75); $thumb->save($path . '/' . $thumbnailFileName);

    return $thumbnailFileName; }

    120 6

  • 4. , - Full ( ) Thumbnail (); - , action, id subaction. - (CommunicationApp/module/Users/config/module .config .php):

    'media' => array( 'type' => 'Segment', 'options' => array( 'route' => '/media[/:action[/:id[/:subaction]]]', 'constraints' => array( 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[a-zA-Z0-9_-]*', 'subaction' => '[a-zA-Z][a-zA-Z0-9_-]*',

    ), 'defaults' => array( 'controller' => 'Users\Controller\MediaManager', 'action' => 'index', ), ), ),

    5. , - , . - MediaManagerController (src/Users/Controller/MediaManagerController .php):

    public function showImageAction() { $uploadId = $this->params()->fromRoute('id'); $uploadTable = $this->getServiceLocator()->get('ImageUploadTable'); $upload = $uploadTable->getUpload($uploadId);

    // $uploadPath = $this->getFileUploadLocation(); if ($this->params()->fromRoute('subaction') == 'thumb') { $filename = $uploadPath ."/" . $upload->thumbnail; } else { $filename = $uploadPath ."/" . $upload->filename; } $file = file_get_contents($filename);

    // $response = $this->getEvent()->getResponse(); $response->getHeaders()->addHeaders(array( 'Content-Type' => 'application/octet-stream',

    121

  • 'Content-Disposition' => 'attachment;filename="' .$upload->filename . '"',

    )); $response->setContent($file); return $response; }

    6. , - . - showImageAction() upload ( CommunicationApp/module/Users/view/users/media-manager/view .phtml):

  • View Image ( ) .

    123

  • ?

    , . - resize , -.

    , , WebinoImageThumb, rotate. rotate View Image .

    Google Data API

    (API) Google Data API Google, . Google Data , Atom (Atom publishing protocol). ZendGdata.

    Google, - API ZendGdata:

    Picasa Web Albums;

    YouTube;

    Google Calendar;

    Google Spreadsheets;

    Google Documents;

    Google Provisioning;

    Google Analytics;

    Google Blogger;

    124 6

  • Google CodeSearch;

    Google Notebook.

    ZendGdata Zend Framework, - . Composer "zendframework/zendgdata": "2.*".

    Google Photos API

    Google Photos API , - Picasa Google+. Google Data API ; :

    getUserFeed() , ;

    insertAlbumEntry() ;

    getAlbumFeed() ;

    insertPhotoEntry() ;

    getPhotoFeed() ;

    insertCommentEntry() ;

    getCommentEntry() ;

    insertTagEntry() ;

    getTagEntry() ;

    deleteAlbumEntry() ;

    deletePhotoEntry() ;

    deleteCommentEntry() ;

    deleteTagEntry() .

    - , .

    Google Data API 125

  • , ZendGdata - Composer . , -, requires CommunicationApp/composer .json zendframework/zendgdata: 2 .* . -, Composer - $ php composer .phar update .

    , , Google Photos .

    Google Photos Google Photos, - .

    1. getGooglePhotos(), - Google Photos . MediaManagerController ( src/Users/Controller/MediaManagerController .php).

    2. API- Curl , - sslverifypeer:

    $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setOptions(array( 'curloptions' => array( CURLOPT_SSL_VERIFYPEER => false, ) ));

    $httpClient = new \ZendGData\HttpClient(); $httpClient->setAdapter($adapter);

    $client = \ZendGData\ClientLogin::getHttpClient( self::GOOGLE_USER_ID, self::GOOGLE_PASSWORD, \ZendGData\Photos::AUTH_SERVICE_NAME, $httpClient);

    3. Google Photos API-:

    $gp = new \ZendGData\Photos($client);

    126 6

  • 4. getUserFeed() - getAlbumFeed():

    $userFeed = $gp->getUserFeed( self::GOOGLE_USER_ID );

    foreach ($userFeed as $userEntry) { $albumId = $userEntry->getGphotoId()->getText(); $gAlbums[$albumId]['label'] = $userEntry->getTitle()->getText();

    $query = $gp->newAlbumQuery(); $query->setUser( self::GOOGLE_USER_ID ); $query->setAlbumId( $albumId );

    $albumFeed = $gp->getAlbumFeed($query);

    foreach ($albumFeed as $photoEntry) { $photoId = $photoEntry->getGphotoId()->getText(); if ($photoEntry->getMediaGroup()->getContent() != null) { $mediaContentArray = $photoEntry->getMediaGroup()->getContent(); $photoUrl = $mediaContentArray[0]->getUrl(); }

    if ($photoEntry->getMediaGroup()->getThumbnail() != null) { $mediaThumbnailArray = $photoEntry->getMediaGroup() ->getThumbnail(); $thumbUrl = $mediaThumbnailArray[0]->getUrl(); }

    $albumPhoto = array(); $albumPhoto['id'] = $photoId; $albumPhoto['photoUrl'] = $pho