Document not found! Please try again

Phalcon PHP Framework Documentation

28 downloads 19728 Views 5MB Size Report
2.40 AccessControlListsACL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 ...... ServerAdmin [email protected]. DocumentRoot "/var/vhosts/test/public".
Phalcon PHP Framework Documentation Release 3.1.1

Phalcon Team

Aug 08, 2017

Contents

1

Czym jest Phalcon

2

Spis tre´sci 2.1 Installation . . . . . . . . . . . . . . . . . . . . 2.1.1 Instalacja . . . . . . . . . . . . . . . . 2.1.2 Phalcon Developer Tools . . . . . . . . 2.2 Tutorials . . . . . . . . . . . . . . . . . . . . . 2.2.1 Tutorial 1: Nauczmy si˛e przez przykład 2.2.2 Tutorial 2: Introducing INVO . . . . . . 2.2.3 Tutorial 3: Securing INVO . . . . . . . 2.2.4 Tutorial 4: Working with the CRUD . . 2.2.5 Tutorial 5: Customizing INVO . . . . . 2.2.6 Tutorial 6: Vökuró . . . . . . . . . . . 2.2.7 Tutorial 7: Creating a Simple REST API 2.2.8 List of examples . . . . . . . . . . . . 2.3 Components . . . . . . . . . . . . . . . . . . . 2.3.1 Dependency Injection/Service Location 2.3.2 The MVC Architecture . . . . . . . . . 2.3.3 Using Controllers . . . . . . . . . . . . 2.3.4 Working with Models . . . . . . . . . . 2.3.5 Model Relationships . . . . . . . . . . 2.3.6 Model Events . . . . . . . . . . . . . . 2.3.7 Model Behaviors . . . . . . . . . . . . 2.3.8 Models Metadata . . . . . . . . . . . . 2.3.9 Model Transactions . . . . . . . . . . . 2.3.10 Validating Models . . . . . . . . . . . . 2.3.11 Working with Models (Advanced) . . . 2.3.12 Phalcon Query Language (PHQL) . . . 2.3.13 Caching in the ORM . . . . . . . . . . 2.3.14 ODM (Object-Document Mapper) . . . 2.3.15 Using Views . . . . . . . . . . . . . . . 2.3.16 View Helpers (Tags) . . . . . . . . . . 2.3.17 Assets Management . . . . . . . . . . . 2.3.18 Volt: Template Engine . . . . . . . . . 2.3.19 MVC Applications . . . . . . . . . . . 2.3.20 Routing . . . . . . . . . . . . . . . . .

3

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5 5 5 25 45 45 55 61 70 86 88 92 102 103 103 116 117 123 142 156 162 167 172 175 178 189 206 219 232 251 263 270 294 301

i

2.4

2.5 2.6

2.3.21 Dispatching Controllers . . . . . . . . . . . . . 2.3.22 Micro Applications . . . . . . . . . . . . . . . 2.3.23 Working with Namespaces . . . . . . . . . . . 2.3.24 Events Manager . . . . . . . . . . . . . . . . . 2.3.25 Request Environment . . . . . . . . . . . . . . 2.3.26 Returning Responses . . . . . . . . . . . . . . 2.3.27 Cookies Management . . . . . . . . . . . . . . 2.3.28 Generating URLs and Paths . . . . . . . . . . 2.3.29 Flashing Messages . . . . . . . . . . . . . . . 2.3.30 Storing data in Session . . . . . . . . . . . . . 2.3.31 Filtering and Sanitizing . . . . . . . . . . . . . 2.3.32 Contextual Escaping . . . . . . . . . . . . . . 2.3.33 Validation . . . . . . . . . . . . . . . . . . . . 2.3.34 Forms . . . . . . . . . . . . . . . . . . . . . . 2.3.35 Reading Configurations . . . . . . . . . . . . . 2.3.36 Pagination . . . . . . . . . . . . . . . . . . . . 2.3.37 Improving Performance with Cache . . . . . . 2.3.38 Security . . . . . . . . . . . . . . . . . . . . . 2.3.39 Encryption/Decryption . . . . . . . . . . . . . 2.3.40 Access Control Lists (ACL) . . . . . . . . . . 2.3.41 Multi-lingual Support . . . . . . . . . . . . . . 2.3.42 Class Autoloader . . . . . . . . . . . . . . . . 2.3.43 Logging . . . . . . . . . . . . . . . . . . . . . 2.3.44 Annotations Parser . . . . . . . . . . . . . . . 2.3.45 Command Line Applications . . . . . . . . . . 2.3.46 Images . . . . . . . . . . . . . . . . . . . . . . 2.3.47 Queueing . . . . . . . . . . . . . . . . . . . . 2.3.48 Database Abstraction Layer . . . . . . . . . . 2.3.49 Internationalization . . . . . . . . . . . . . . . 2.3.50 Database Migrations . . . . . . . . . . . . . . 2.3.51 Debugging Applications . . . . . . . . . . . . 2.3.52 Unit testing . . . . . . . . . . . . . . . . . . . In Depth Explanations / Further Reading . . . . . . . . 2.4.1 Increasing Performance: What’s next? . . . . . 2.4.2 Dependency Injection Explained . . . . . . . . 2.4.3 Understanding How Phalcon Applications Work API . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.1 API Indice . . . . . . . . . . . . . . . . . . . . Legal . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.1 License . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

322 333 348 350 357 360 363 365 368 372 375 379 383 391 403 406 409 418 421 423 433 435 440 446 452 456 461 463 478 480 488 492 496 496 504 510 513 513 1109 1109

3

Previous Versions

1111

4

Other formats

1113

ii

Phalcon PHP Framework Documentation, Release 3.1.1

Witamy w Phalconie, zupełnie nowym podej´sciu do frameworków PHP. Naszym celem jest danie Ci zaawansowanego narz˛edzia potrzebnego do tworzenia stron i aplikacji internetowych bez przejmowania si˛e problemami wydajno´sci.

Contents

1

Phalcon PHP Framework Documentation, Release 3.1.1

2

Contents

CHAPTER

1

Czym jest Phalcon

Phalcon jest open source’owym, pełno warto´sciowym frameworkiem dla PHP 5 napisanym w C jako rozszerzenie, zoptymalizowanym pod katem ˛ wysokiej wydajno´sci. Nie musisz si˛e uczy´c lub uz˙ ywa´c j˛ezyka C, gdyz˙ funkcjonalno´sci sa˛ wyeksponowane jako klasy PHP gotowe do uz˙ ycia. Phalcon jest równiez˙ lu´zno powiazany, ˛ umoz˙ liwia korzystanie z jego obiektów jako doklejanych składników w zalez˙ no´sci od potrzeb Twojej aplikacji. Phalcon to nie tylko wydajno´sc´ , naszym celem jest przygotowanie narz˛edzia solidnego, bogatego w funkcje i prostego do uz˙ ycia!

3

Phalcon PHP Framework Documentation, Release 3.1.1

4

Chapter 1. Czym jest Phalcon

CHAPTER

2

Spis tre´sci

Installation Instalacja Rozszerzenia PHP wymagaja˛ nieco innej metody instalacji niz˙ tradycyjna biblioteka PHP lub framework. Moz˙ esz pobra´c pakiet binarny dla wybranego przez siebie systemu lub skompilowa´c go ze z´ ródeł. Windows Aby korzysta´c z Phalcon na systemie Windows moz˙ esz pobra´c bibliotek˛e DLL. Edytuj swój plik php.ini i dodaj na jego ko´ncu: extension=php_phalcon.dll

Zrestartuj swój serwer. Poniz˙ szy filmik jest przewodnikiem “krok po kroku” jak zainstalowa´c Phalcon na systemie Windows: Powiazane ˛ Przewodniki Instalacja na XAMPP XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl. Once you download XAMPP, all you have to do is extract it and start using it. Below are detailed instructions on how to install Phalcon on XAMPP for Windows. Using the latest XAMPP version is highly recommended.

5

Phalcon PHP Framework Documentation, Release 3.1.1

´ Pobierz własciw a˛ wersje˛ Phalcona XAMPP wydaje zawsze 32-bitowe wersje Apache i PHP. Musisz pobra´c wersj˛e x86 Phalcona dla systemu Windows z sekcji Pobierz. Po pobraniu biblioteki Phalcon, b˛edziesz miał plik zip jak na obrazku poniz˙ ej:

Wypakuj bibliotek˛e z archiwum aby otrzyma´c Phalcon DLL:

Skopiuj plik php_phalcon.dll do folderu rozszerze´n PHP. Jez˙ eli zainstalowałe´s XAMPP w folderze C:\xampp, rozszerzenie musi by´c w C:\xampp\php\ext

Edytuj plik php.ini, jest zlokalizowany w C:\xampp\php\php.ini. Moz˙ esz go edytowa´c Notatnikiem lub innym, podobnym programem. Polecamy Nodepad++, aby unikna´ ˛c problemów z zako´nczeniami linii. Dodaj na ko´ncu pliku: extension=php_phalcon.dll i zapisz go. 6

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

2.1. Installation

7

Phalcon PHP Framework Documentation, Release 3.1.1

Zrestartuj serwer Apache z panelu kontrolnego XAMPP. To załaduje nowa˛ konfiguracj˛e PHP.

Otwórz swoja˛ przegladark˛ ˛ e i przejd´z do http://localhost. Pojawi si˛e strona powitalna WAMP. Kliknij na link phpinfo(). phpinfo() wy´swietli znaczna˛ ilo´sc´ informacji o obecnym stanie PHP. Przewi´n w dół, aby sprawdzi´c czy rozszerzenie phalcon zostało załadowane poprawnie. Jez˙ eli widzisz wersj˛e phalcona w phpinfo(), gratulacje!, Teraz latasz z Phalconem :) Poradniki Wideo Poniz˙ szy filmik jest przewodnikiem “krok po kroku” jak zainstalowa´c Phalcona na systemie Windows: Powiazane ˛ przewodniki • General Installation • Detailed Installation on WAMP for Windows

8

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

2.1. Installation

9

Phalcon PHP Framework Documentation, Release 3.1.1

Instalacja na WAMP WampServer to darmowy, zintegrowany pakiet webowy, z pomoca˛ którego łatwo uruchomimy na swoim komputerze własny serwer WWW. Z powodzeniem posłuz˙ y´c ono moz˙ e do testowania skryptów PHP, jak równiez˙ hostowania niewielkich stron internetowych oraz aplikacji webowych. Poniz˙ ej przedstawiamy szczegółowa˛ instrukcj˛e instalacji Phalcona na WampServer. Korzystanie z najnowszej wersji WampServer jest wysoce zalecane. ´ Pobierz własciw a˛ wersje˛ Phalcona WAMP ma zarówno wersje 32 jak i 64 bitowe. Z sekcji Download moz˙ esz wybra´c wersje Phalcona odpowiednia˛ dla swojej architektury. Po pobraniu biblioteki Phalcon, b˛edziesz miał plik zip jak na obrazku poniz˙ ej:

Wypakuj bibliotek˛e z archiwum, aby otrzyma´c Phalcon DLL:

Skopiuj plik php_phalcon.dll do folderu rozszerze´n PHP. Jez˙ eli zainstalowałe´s XAMPP w folderze C:\wamp, rozszerzenie musi by´c w C:\wamp\bin\php\php5.5.12\ext Edytuj plik php.ini, jest zlokalizowany w C:\wamp\bin\php\php5.5.12\php.ini. Moz˙ esz go edytowa´c Notatnikiem lub innym, podobnym programem. Polecamy Nodepad++, aby unikna´ ˛c problemów z zako´nczeniami linii. Dodaj na ko´ncu pliku: extension=php_phalcon.dll i zapisz go. Edytuj jeszcze jeden plik php.ini, który zlokalizowany jest w C:\wamp\bin\apache\apache2.4.9\bin\php.ini. Dodaj na ko´ncu pliku: extension=php_phalcon.dll i zapisz go. Zrestartuj serwer Apache. Kliknij pojedy´nczo na ikonie WampServer w prawym dolnym rogu paska zada´n. Z menu podr˛ecznego wybierz “Restart All Services”. Ikona na pasku zada´n b˛edzie znowu zielona. Otwórz swoja˛ przegladark˛ ˛ e i przejd´z do http://localhost. Pojawi si˛e strona powitalna WAMP. Spójrz na sekcj˛e “extensions loaded”, aby sprawdzi´c czy phalcon został załadowany. Gratulacje!, Teraz latasz z Phalconem. Powiazane ˛ przewodniki • General Installation • Detailed Installation on XAMPP for Windows

10

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

2.1. Installation

11

Phalcon PHP Framework Documentation, Release 3.1.1

12

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

Linux/Solaris Debian / Ubuntu To add the repository to your distribution: # Stable releases curl -s https://packagecloud.io/install/repositories/phalcon/stable/script.deb.sh | ˓→sudo bash # Nightly releases curl -s https://packagecloud.io/install/repositories/phalcon/nightly/script.deb.sh | ˓→sudo bash

This only needs to be done only once, unless your distribution changes or you want to switch from stable to nightly builds. To install Phalcon: sudo apt-get install php5-phalcon # or for PHP 7 sudo apt-get install php7.0-phalcon

2.1. Installation

13

Phalcon PHP Framework Documentation, Release 3.1.1

RPM distributions (i.e. CentOS) To add the repository to our distribution: # Stable releases curl -s https://packagecloud.io/install/repositories/phalcon/stable/script.rpm.sh | ˓→sudo bash # Nightly releases curl -s https://packagecloud.io/install/repositories/phalcon/nightly/script.rpm.sh | ˓→sudo bash

This only needs to be done only once, unless your distribution changes or you want to switch from stable to nightly builds. To install Phalcon: sudo yum install php56u-phalcon # or for PHP 7 sudo yum install php70u-phalcon

Compile from source Na systemach Linux/Solaris moz˙ esz w łatwy sposób skompilowa´c i zainstalowa´c rozszerzenie z kodu z´ ródłowego: Wst˛epnie wymagane pakiety: • Pliki z´ ródłowe PHP >= 5.5 • Kompilator GCC (Linux/Solaris) • Git (je´sli nie jest jeszcze zainstalowany na twoim systemie - chyba ze pobierzesz pakiet z GitHub i prze´slesz go na swój serwer przez FTP/SFTP) Specyficzne pakiety dla wspólnych platform: # Ubuntu sudo apt-get install php5-dev libpcre3-dev gcc make php5-mysql # Suse sudo yast -i gcc make autoconf php5-devel php5-pear php5-mysql # CentOS/RedHat/Fedora sudo yum install php-devel pcre-devel gcc make # Solaris pkg install gcc-45 php-56 apache-php56

Tworzenie rozszerzenia: git clone git://github.com/phalcon/cphalcon.git cd cphalcon/build sudo ./install

14

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

Dodaj rozszerzenie do swojej konfiguracji PHP: # Suse: Stwórz plik o nazwie phalcon.ini w /etc/php5/conf.d/ z nast˛ epujac ˛ a˛ ˓→zawarto´ scia: ˛ extension=phalcon.so # CentOS/RedHat/Fedora: Stwórz plik o nazwie phalcon.ini w /etc/php.d/ z nast˛ epujac ˛ a˛ ˓→zawarto´ scia: ˛ extension=phalcon.so # Ubuntu/Debian z apache2: Stwórz plik o nazwie 30-phalcon.ini w /etc/php5/apache2/ ˓→conf.d/ z nast˛ epujac ˛ a˛ zawarto´ scia: ˛ extension=phalcon.so # Ubuntu/Debian z php5-fpm: Stwórz plik o nazwie 30-phalcon.ini w /etc/php5/fpm/conf. ˓→d/ z nast˛ epujac ˛ a˛ zawarto´ scia: ˛ extension=phalcon.so # Ubuntu/Debian z php5-cli: Stwórz plik o nazwie 30-phalcon.ini w /etc/php5/cli/conf. ˓→d/ z nast˛ epujac ˛ a˛ zawarto´ scia: ˛ extension=phalcon.so

Zrestartuj serwer. If you are running Ubuntu/Debian with php5-fpm, restart it: sudo service php5-fpm restart

Phalcon automatycznie wykrywa architektur˛e twojego systemu, moz˙ esz jednak wymusi´c kompilacj˛e dla konkretnej architektury: cd cphalcon/build # One of the following: sudo ./install 32bits sudo ./install 64bits sudo ./install safe

Jez˙ eli autoinstalator wyrzuca bład, ˛ spróbuj skompilowa´c rozszerzenie r˛ecznie: cd cphalcon/build/64bits export CFLAGS="-O2 --fvisibility=hidden" ./configure --enable-phalcon make && sudo make install

Mac OS X Na systemach Mac OS X moz˙ esz skompilowa´c rozszerzenie z kodu z´ ródłowego: Wymagania Wst˛epnie wymagane pakiety:

2.1. Installation

15

Phalcon PHP Framework Documentation, Release 3.1.1

• Pliki z´ ródłowe PHP >= 5.5 • XCode # brew brew tap homebrew/homebrew-php brew install php55-phalcon brew install php56-phalcon # MacPorts sudo port install php55-phalcon sudo port install php56-phalcon

Dodaj rozszerzenie do swojej konfiguracji PHP. FreeBSD Dost˛epny jest port dla Free BSD. Wystarczy tylko uz˙ y´c tej prostej komendy aby go zainstalowa´c: pkg_add -r phalcon

lub export CFLAGS="-O2 --fvisibility=hidden" cd /usr/ports/www/phalcon make install clean

Sprawdzenie instalacji Sprawd´z wynik funkcji phpinfo() w poszukiwaniu sekcji zawierajacej ˛ “Phalcon” lub uruchom poniz˙ szy kod:

Rozszerzenie Phalcon powinno pojawi´c si˛e jako cz˛es´c´ wyniku: Array ( [0] [1] [2] [3] [4] [5] [6] )

=> => => => => => =>

Core libxml filter SPL standard phalcon pdo_mysql

Uwagi instalacyjne Informacje o instalacji dla serwerów WWW:

16

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

Apache Installation Notes Apache is a popular and well known web server available on many platforms. Configuring Apache for Phalcon The following are potential configurations you can use to setup Apache with Phalcon. These notes are primarily focused on the configuration of the mod_rewrite module allowing to use friendly URLs and the router component. Commonly an application has the following structure: test/ app/ controllers/ models/ views/ public/ css/ img/ js/ index.php

Directory under the main Document Root This being the most common case, the application is installed in any directory under the document root. In this case, we use two .htaccess files, the first one to hide the application code forwarding all requests to the application’s document root (public/). # test/.htaccess RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule ((?s).*) public/$1 [L]

Now a second .htaccess file is located in the public/ directory, this re-writes all the URIs to the public/index.php file: # test/public/.htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]

If you do not want to use .htaccess files you can move these configurations to the apache’s main configuration file: RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule ((?s).*) public/$1 [L]

2.1. Installation

17

Phalcon PHP Framework Documentation, Release 3.1.1

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]

Virtual Hosts And this second configuration allows you to install a Phalcon application in a virtual host: ServerAdmin [email protected] DocumentRoot "/var/vhosts/test/public" DirectoryIndex index.php ServerName example.host ServerAlias www.example.host Options All AllowOverride All Allow from all

Or if you are using Apache 2.4 or above: ServerAdmin [email protected] DocumentRoot "/var/vhosts/test/public" DirectoryIndex index.php ServerName example.host ServerAlias www.example.host Options All AllowOverride All Require all granted

Nginx Installation Notes Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Unlike traditional servers, Nginx doesn’t rely on threads to handle requests. Instead it uses a much more

18

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. The PHP-FPM (FastCGI Process Manager) is usually used to allow Nginx to process PHP files. Nowadays, PHP-FPM is bundled with any Unix PHP distribution. Phalcon + Nginx + PHP-FPM provides a powerful set of tools that offer maximum performance for your PHP applications. Configuring Nginx for Phalcon The following are potential configurations you can use to setup nginx with Phalcon: Basic configuration Using $_GET['_url'] as source of URIs: server { listen server_name root index charset

80; localhost.dev; /var/www/phalcon/public; index.php index.html index.htm; utf-8;

location / { try_files $uri $uri/ /index.php?_url=$uri&$args; } location ~ \.php { fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index /index.php; include fastcgi_params; fastcgi_split_path_info fastcgi_param PATH_INFO fastcgi_param PATH_TRANSLATED fastcgi_param SCRIPT_FILENAME

^(.+\.php)(/.+)$; $fastcgi_path_info; $document_root$fastcgi_path_info; $document_root$fastcgi_script_name;

} location ~ /\.ht { deny all; } }

Using $_SERVER['REQUEST_URI'] as source of URIs: server { listen server_name root index charset

80; localhost.dev; /var/www/phalcon/public; index.php index.html index.htm; utf-8;

location / { try_files $uri $uri/ /index.php; } location ~ \.php$ {

2.1. Installation

19

Phalcon PHP Framework Documentation, Release 3.1.1

try_files

$uri =404;

fastcgi_pass 127.0.0.1:9000; fastcgi_index /index.php; include fastcgi_params; fastcgi_split_path_info fastcgi_param PATH_INFO fastcgi_param PATH_TRANSLATED fastcgi_param SCRIPT_FILENAME

^(.+\.php)(/.+)$; $fastcgi_path_info; $document_root$fastcgi_path_info; $document_root$fastcgi_script_name;

} location ~ /\.ht { deny all; } }

Instalacja na serwerach Cherokee Cherokee jest wysoce wydajnym serwerem WWW. Jest bardzo szybki, elastyczny i prosty w konfiguracji. Configuring Cherokee for Phalcon Cherokee oferuje przyjazny interfejs graficzny, aby skonfigurowa´c niemal kaz˙ de ustawienie dost˛epne w serwerze WWW. Uruchom administratora cherokee wykonujac ˛ polecenie /´sciezka-do-cherokee/sbin/cherokee-admin jako root: Stwórz nowy serwer wirtualny klikajac ˛ na ‘vServers’, nast˛epnie dodaj nowy serwer wirtualny: Ostatnio dodany wirtualny serwer musi pojawi´c si˛e na lewym pasku ekranu. W zakładce ‘Behaviors’ zobaczysz zestaw domy´slnych zachowa´n dla tego serwera wirtualnego. Kliknij na przycisk ‘Rule Management’. Usu´n te oznaczone jako ‘Directory /cherokee_themes’ i ‘Directory /icons’: Dodaj zachowanie ‘PHP Language’ uz˙ ywajac ˛ kreatora. To pozwoli ci uruchamia´c aplikacje PHP: Normalnie to zachowanie nie wymaga dodatkowych ustawie´n. Dodaj kolejne zachowanie, tym razem w sekcji ‘Manual Configuration’. W ‘Rule Type’ wybierz ‘File Exists’, nast˛epnie upewnij si˛e z˙ e opcja ‘Match any file’ jest właczona: ˛ W zakładce ‘Handler’ wybierz ‘List & Send’ jako handler: Edytuj zachowanie ‘Default’ w celu właczenia ˛ przepisywania linków. Zmie´n handler na ‘Redirection’, nast˛epnie dodaj do silnika nast˛epujace ˛ wyraz˙ enie regularne ^(.*)$: Na koniec, upewnij si˛e, z˙ e zachowania maja˛ nast˛epujac ˛ a˛ kolejno´sc´ : Uruchom aplikacj˛e w przegladarce: ˛ Using PHP Built-in webserver As of PHP 5.4.0, you can use PHP’s on built-in web server for development. To start the server type: php -S localhost:8000 -t /public

20

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

2.1. Installation

21

Phalcon PHP Framework Documentation, Release 3.1.1

22

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

2.1. Installation

23

Phalcon PHP Framework Documentation, Release 3.1.1

24

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

If you want to rewrite the URIs to the index.php file use the following router file (.htrouter.php):

Name

E-Mail





Przegladanie ˛ formularza w Twojej przegladarce ˛ wy´swietli co´s takiego: Phalcon\Tag dostarcza równiez˙ uz˙ ytecznych metod do budowania elementów formularza. Metoda Phalcon\Tag::form() odbiera tylko jeden parametr od razu - relatywny URI do kontrolera/akcji w aplikacji. Klikajac ˛ przycisk “Send”, zaobserwujesz wyjatek ˛ pochodzacy ˛ z framework’a, wskazujacy, ˛ z˙ e brakuje nam akcji “register” w kontrolerze “signup”. Nasz plik public/index.php wyrzuca taki wyjatek: ˛ Exception: Action “register” was not found on handler “signup” Implementacja takiej metody usunie wspomniany wyjatek: ˛ ./

Modify the phpunit.xml to fit your needs and save it in tests/. This will run any tests under the tests/ directory. Sample unit test To run any unit tests you need to define them. The autoloader will make sure the proper files are loaded so all you need to do is create the files and phpunit will run the tests for you. This example does not contain a config file, most test cases however, do need one. You can add it to the DI to get the UnitTestCase file.

494

´ Chapter 2. Spis tresci

Phalcon PHP Framework Documentation, Release 3.1.1

First create a base unit test called UnitTestCase.php in your /tests directory: