Phalcon PHP Framework Documentation Release 2.0.8
Total Page:16
File Type:pdf, Size:1020Kb
Phalcon PHP Framework Documentation Release 2.0.8 Phalcon Team September 30, 2015 Contents 1 What is Phalcon? 3 2 Table of Contents 5 2.1 Installation................................................5 2.2 Tutorial 1: Let’s learn by example.................................... 23 2.3 Tutorial 2: Introducing INVO...................................... 34 2.4 Tutorial 3: Securing INVO........................................ 39 2.5 Tutorial 4: Using CRUDs........................................ 48 2.6 Tutorial 5: Customizing INVO...................................... 60 2.7 Tutorial 6: Vökuró............................................ 62 2.8 Tutorial 7: Creating a Simple REST API................................ 65 2.9 List of examples............................................. 74 2.10 Dependency Injection/Service Location................................. 75 2.11 The MVC Architecture.......................................... 92 2.12 Using Controllers............................................. 93 2.13 Working with Models.......................................... 100 2.14 Models Meta-Data............................................ 154 2.15 Model Transactions........................................... 159 2.16 Phalcon Query Language (PHQL).................................... 162 2.17 Caching in the ORM........................................... 177 2.18 ODM (Object-Document Mapper).................................... 193 2.19 Using Views............................................... 206 2.20 View Helpers............................................... 224 2.21 Assets Management........................................... 235 2.22 Volt: Template Engine.......................................... 242 2.23 MVC Applications............................................ 266 2.24 Routing.................................................. 274 2.25 Dispatching Controllers......................................... 292 2.26 Micro Applications............................................ 302 2.27 Working with Namespaces........................................ 313 2.28 Events Manager............................................. 315 2.29 Request Environment.......................................... 322 2.30 Returning Responses........................................... 324 2.31 Cookies Management.......................................... 328 2.32 Generating URLs and Paths....................................... 329 2.33 Flashing Messages............................................ 332 2.34 Storing data in Session.......................................... 335 2.35 Filtering and Sanitizing......................................... 338 i 2.36 Contextual Escaping........................................... 342 2.37 Validation................................................. 345 2.38 Forms................................................... 351 2.39 Reading Configurations......................................... 360 2.40 Pagination................................................ 363 2.41 Improving Performance with Cache................................... 366 2.42 Security.................................................. 374 2.43 Encryption/Decryption.......................................... 377 2.44 Access Control Lists (ACL)....................................... 379 2.45 Multi-lingual Support.......................................... 383 2.46 Universal Class Loader.......................................... 385 2.47 Logging.................................................. 390 2.48 Annotations Parser............................................ 394 2.49 Command Line Applications....................................... 400 2.50 Queueing................................................. 404 2.51 Database Abstraction Layer....................................... 406 2.52 Internationalization............................................ 419 2.53 Database Migrations........................................... 421 2.54 Debugging Applications......................................... 428 2.55 Phalcon Developer Tools......................................... 433 2.56 Increasing Performance: What’s next?.................................. 454 2.57 Unit testing................................................ 461 2.58 API Indice................................................ 465 2.59 License.................................................. 939 3 Other formats 941 ii Phalcon PHP Framework Documentation, Release 2.0.8 Welcome to Phalcon framework. Our mission is to give you an advanced tool for developing the faster web sites and applications with PHP. Contents 1 Phalcon PHP Framework Documentation, Release 2.0.8 2 Contents CHAPTER 1 What is Phalcon? Phalcon is an open source, full stack framework for PHP written as a C-extension, optimized for high performance. You don’t need to learn or use the C language, since the functionality is exposed as PHP classes ready for you to use. Phalcon also is loosely coupled, allowing you to use its objects as glue components based on the needs of your application. Phalcon is not only about performance, our goal is to make it robust, rich in features and easy to use! 3 Phalcon PHP Framework Documentation, Release 2.0.8 4 Chapter 1. What is Phalcon? CHAPTER 2 Table of Contents 2.1 Installation PHP extensions require a slightly different installation method to a traditional PHP-based library or framework. You can either download a binary package for the system of your choice or build it from the sources. 2.1.1 Windows To use phalcon on Windows you can download a DLL library. Edit your php.ini file and then append at the end: extension=php_phalcon.dll Restart your webserver. The following screencast is a step-by-step guide to install Phalcon on Windows: If you don’t know what DLL to download, use the following script_ to figure it out. Related Guides Installation on 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. Download the right version of Phalcon XAMPP is always releasing 32 bit versions of Apache and PHP. You will need to download the x86 version of Phalcon for Windows from the download section. After downloading the Phalcon library you will have a zip file like the one shown below: Extract the library from the archive to get the Phalcon DLL: Copy the file php_phalcon.dll to the PHP extensions. If you have installed XAMPP in the c:\xampp folder, the extension needs to be in c:\xampp\php\ext Edit the php.ini file, it is located at C:\xampp\php\php.ini. It can be edited with Notepad or a similar program. We recommend Notepad++ to avoid issues with line endings. Append at the end of the file: extension=php_phalcon.dll and save it. Restart the Apache Web Server from the XAMPP Control Center. This will load the new PHP configuration. 5 Phalcon PHP Framework Documentation, Release 2.0.8 6 Chapter 2. Table of Contents Phalcon PHP Framework Documentation, Release 2.0.8 2.1. Installation 7 Phalcon PHP Framework Documentation, Release 2.0.8 Open your browser to navigate to http://localhost. The XAMPP welcome page will appear. Click on the link phpinfo(). phpinfo() will output a significant amount of information on screen about the current state of PHP. Scroll down to check if the phalcon extension has been loaded correctly. If you can see the phalcon version in the phpinfo() output, congrats!, You are now flying with Phalcon. Screencast The following screencast is a step by step guide to install Phalcon on Windows: Related Guides • General Installation • Detailed Installation on WAMP for Windows 8 Chapter 2. Table of Contents Phalcon PHP Framework Documentation, Release 2.0.8 Installation on WAMP WampServer is a Windows web development environment. It allows you to create web applications with Apache2, PHP and a MySQL database. Below are detailed instructions on how to install Phalcon on WampServer for Windows. Using the latest WampServer version is highly recommended. Download the right version of Phalcon WAMP has both 32 and 64 bit versions. From the download section, you can choose the Phalcon for Windows accordingly to your desired architecture. After download the Phalcon library you will have a zip file like the one shown below: Extract the library from the archive to get the Phalcon DLL: Copy the file php_phalcon.dll to the PHP extensions. If WAMP is installed in the C:\wamp folder, the extension needs to be in C:\wamp\bin\php\php5.3.10\ext Edit the php.ini file, it is located at C:\wamp\bin\php\php5.3.10\php.ini. It can be edited with Notepad or a simi- lar program. We recommend Notepad++ to avoid issues with line endings. Append at the end of the file: exten- sion=php_phalcon.dll and save it. Also edit another php.ini file, which is located at C:\wamp\bin\apache\Apache2.2.21\bin\php.ini. Append at the end of the file: extension=php_phalcon.dll and save it. Restart the Apache Web Server. Do a single click on the WampServer icon at system tray. Choose “Restart All Services” from the pop-up menu. Check out that tray icon will become green again. Open your browser to navigate to http://localhost. The WAMP welcome page will appear. Look at the section “exten- sions loaded” to check if phalcon was loaded. Congrats!, You are now flying with Phalcon. Related Guides • General Installation 2.1. Installation 9 Phalcon PHP Framework Documentation, Release 2.0.8 10 Chapter 2. Table of Contents Phalcon PHP Framework Documentation,