Phalcon PHP Framework Documentation Release 0.9.0
Total Page:16
File Type:pdf, Size:1020Kb
Phalcon PHP Framework Documentation Release 0.9.0 Phalcon Team February 05, 2013 Contents 1 What is Phalcon? 3 2 Table of Contents 5 2.1 Our motivation..............................................5 2.2 Framework Benchmarks.........................................6 2.3 Installation................................................ 22 2.4 Tutorial 1: Let’s learn by example.................................... 35 2.5 Tutorial 2: Explaining INVO....................................... 45 2.6 Tutorial 3: Creating a Simple REST API................................ 62 2.7 Using Dependency Injection....................................... 70 2.8 The MVC Architecture.......................................... 86 2.9 Using Controllers............................................. 87 2.10 Working with Models.......................................... 94 2.11 Phalcon Query Language (PHQL).................................... 143 2.12 ODM (Object-Document Mapper).................................... 154 2.13 Using Views............................................... 165 2.14 View Helpers............................................... 179 2.15 Volt: Template Engine.......................................... 188 2.16 MVC Applications............................................ 206 2.17 Routing.................................................. 213 2.18 Dispatching Controllers......................................... 226 2.19 Micro Applications............................................ 230 2.20 Working with Namespaces........................................ 238 2.21 Events Manager............................................. 239 2.22 Request Environment.......................................... 245 2.23 Returning Responses........................................... 247 2.24 Generating URLs and Paths....................................... 250 2.25 Flashing Messages............................................ 253 2.26 Storing data in Session.......................................... 255 2.27 Filtering and Sanitizing......................................... 258 2.28 Contextual Escaping........................................... 262 2.29 Reading Configurations......................................... 265 2.30 Data Pagination............................................. 267 i 2.31 Improving Performance with Cache................................... 269 2.32 Security.................................................. 275 2.33 Access Control Lists ACL........................................ 278 2.34 Multi-lingual Support.......................................... 281 2.35 Universal Class Loader.......................................... 283 2.36 Logging.................................................. 287 2.37 Annotations Parser............................................ 290 2.38 Command Line Applications....................................... 295 2.39 Database Abstraction Layer....................................... 296 2.40 Internationalization............................................ 306 2.41 Database Migrations........................................... 307 2.42 Debugging Applications......................................... 314 2.43 Phalcon Developer Tools......................................... 318 2.44 API Indice................................................ 340 2.45 License.................................................. 629 3 Other formats 631 ii Phalcon PHP Framework Documentation, Release 0.9.0 Welcome to Phalcon framework, a new approach on PHP frameworks. Our mission is to give you an advanced tool for developing web sites and applications without worrying about performance. Contents 1 Phalcon PHP Framework Documentation, Release 0.9.0 2 Contents CHAPTER 1 What is Phalcon? Phalcon is an open source, full stack framework for PHP 5 written as a C-extension, optimized for high performance. You don’t need 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 and easy to use! 3 Phalcon PHP Framework Documentation, Release 0.9.0 4 Chapter 1. What is Phalcon? CHAPTER 2 Table of Contents 2.1 Our motivation There are many PHP frameworks nowadays, but none of them is like Phalcon (Really, trust us on this one). Almost all programmers prefer to use a framework. This is primarily because it provides a lot of functionality that is already tested and ready to use, therefore keeping code DRY (Don’t Repeat Yourself). However, the framework itself demands a lot of file inclusions and hundreds of lines of code to be interpreted and executed on each request from the actual application. This operation slows the application down and subsequently impacts the end user experience. 2.1.1 The Question Why can’t we have a framework with all of its advantages but with none or very few disadvantages? This is why Phalcon was born! During the last few months, we have extensively researched PHP’s behavior, investigating areas for significant opti- mizations (big or small). Through understanding of the Zend Engine, we managed to remove unnecessary validations, compacted code, performed optimizations and generated low-level solutions so as to achieve maximum performance from Phalcon. 2.1.2 Why? • The use of frameworks has become mandatory in professional development with PHP • Frameworks offer a structured philosophy to easily maintain projects writing less code and making work more fun 2.1.3 Inner workings of PHP? • PHP has dynamic and weak variable types. Every time a binary operation is made (ex. 2 + “2”), PHP checks the operand types to perform potential conversions • PHP is interpreted and not compiled. The major disadvantage is performance loss • Every time a script is requested it must be first interpreted. 5 Phalcon PHP Framework Documentation, Release 0.9.0 • If a bytecode cache (like APC) isn’t used, syntax checking is performed every time for every file in the request 2.1.4 How traditional PHP frameworks work? • Many files with classes and functions are read on every request made. Disk reading is expensive in terms of performance, especially when the file structure includes deep folders • Modern frameworks use lazy loading (autoload) to increase performance (for load and execute only the code needed) • Continuous loading or interpreting is expensive and impacts performance • The framework code does not change very often, therefore an application needs to load and interpret it every time a request is made 2.1.5 How does a PHP C-extension work? • C extensions are loaded together with PHP one time on the web server’s daemon start process • Classes and functions provided by the extension are ready to use for any application • The code isn’t interpreted because is already compiled to a specific platform and processor 2.1.6 How does Phalcon work? • Components are loosely coupled. With Phalcon, nothing is imposed on you: you’re free to use the full frame- work, or just some parts of it as a glue components. • Low-level optimizations provides the lowest overhead for MVC-based applications • Interact with databases with maximum performance by using a C-language ORM for PHP • Phalcon directly accesses internal PHP structures optimizing execution in that way as well 2.1.7 Conclusion Phalcon is an effort to build the fastest framework for PHP. You now have an even easier and robust way to develop applications without be worrying about performance. Enjoy! 2.2 Framework Benchmarks In the past, performance was not considered one of the top priorities when developing web applications. Reasonable hardware was able to compensate for that. However when Google decided to take site speed into account in the search rankings, performance became one of the top priorities alongside functionality. This is yet another way in which improving web performance will have a positive impact on a website. The benchmarks below, show how efficient Phalcon is when compared with other traditional PHP frameworks. These benchmarks are updated as stable versions are released from any of the frameworks mentioned or Phalcon itself. We encourage programmers to clone the test suite that we are using for our benchmarks. If you have any additional optimizations or comments please write us. Check out source at Github 6 Chapter 2. Table of Contents Phalcon PHP Framework Documentation, Release 0.9.0 2.2.1 What was the test environment? APC intermediate code cache was enabled for all frameworks. Any Apache mod-rewrite feature was disabled when possible to avoid potentially additional overheads. The testing hardware environment is as follows: • Operating System: Mac OS X Lion 10.7.4 • Web Server: Apache httpd 2.2.22 • PHP: 5.3.15 • CPU: 2.04 Ghz Intel Core i5 • Main Memory: 4GB 1333 MHz DDR3 • Hard Drive: 500GB SATA Disk PHP version and info: APC settings: 2.2.2 List of Benchmarks Hello World Benchmark How the benchmarks were performed? We created a “Hello World” benchmark seeking to identify the smallest load overhead of each framework. Many people don’t like this kind of benchmark because real-world applications require more complex features or structures. However, these tests identify the minimum time spent by each framework to perform a simple task. Such a task represents the mimimum requirement for every framework to process a single request. More specifically, the benchmark only measures the time it takes for a framework to start, run an action and free up resources at the end