O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Phalcon Cookbook

Book Description

Master Phalcon by implementing hands-on recipes using industry best practices with the Web and CLI interfaces

About This Book

  • Create quick prototypes with the Phalcon Developer Tools
  • Learn to use the powerful and uniquely efficient Phalcon ORM
  • Reuse codes in real projects

Who This Book Is For

If you are a beginner-to-intermediate Phalcon developer who wants to level up or an advanced user who is seeking some new techniques and insight, then this book is perfect for you. This book will be relevant to you over a long period of time due to the mixed nature of this book in providing both abstract comprehension as well as specific examples meant to be usable in your projects. You will be able to experiment with each new aspect of integration in prebuilt recipes meant to best illustrate each specific feature. This will save you lots of time getting up to speed before attempting to integrate into a real application.

What You Will Learn

  • Install the Phalcon extension from both premade packages and source code
  • Create a normal and micro application structure
  • Understand the dispatcher event cycle and create custom plugins
  • Get hands-on using the Phalcon Query Language and ORM
  • Create flexible views with the Volt template system
  • Leverage Phalcon to prevent common security vulnerabilities
  • Optimize an application with focused solutions
  • Profile and debug an application to get increased reliability

In Detail

Phalcon is a high-performance PHP framework delivered as a PHP extension. This provides new opportunities for speed and application design, which until recently have been unrealized in the PHP ecosystem.

Packed with simple learning exercises, technology prototypes, and real-world usable code, this book will guide you from the beginner and setup stage all the way to advanced usage. You will learn how to avoid niche pitfalls, how to use the command-line developer tools, how to integrate with new web standards, as well as how to set up and customize the MVC application structure.

You will see how Phalcon can be used to quickly set up a single file web application as well as a complex multi-module application suitable for long-term projects. Some of the recipes focus on abstract concepts that are vital to get a deep comprehension of Phalcon and others are designed as a vehicle to deliver real-world usable classes and code snippets to solve advanced problems. You’ll start out with basic setup and application structure and then move onto the Phalcon MVC and routing implementation, the power of the ORM and Phalcon Query Language, and Phalcon’s own Volt templating system. Finally, you will move on to caching, security, and optimization.

Style and approach

An easy-to-start guide that provides

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Phalcon Cookbook
    1. Table of Contents
    2. Phalcon Cookbook
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why Subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There’s more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Used to Phalcon
      1. Introduction
      2. Getting your IDE to work nicely with Phalcon
        1. Getting ready
        2. How to do it…
          1. Ways to enable Phalcon API autocompletion in major IDEs
            1. PhpStorm
            2. NetBeans
          2. Ways to enable Volt syntax highlighting in major IDEs
            1. Netbeans
            2. PhpStorm
          3. Syntax highlighting for Sublime Text or TextMate
            1. Installation via Package Control
            2. Manual installation
        3. How it works…
      3. Creating the application directory structure
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Setting up your request entry point
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Easily loading code on demand
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Setting up your request entry point
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      7. Understanding the request life cycle
        1. Getting ready
        2. How to do it…
        3. How it works...
        4. There's more…
        5. See also
    9. 2. Structuring Your Projects
      1. Introduction
      2. Choosing the best place for an implementation
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Automation of routine tasks
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      4. Creating the application structure by using code generation tools
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      5. Get more power by adding a middleware between Phalcon and your application
        1. Getting ready
        2. How to do it…
          1. Logging critical errors
          2. Changing the description and keywords meta tags depending on your route
          3. Receiving site status reports
          4. Caching pages to reduce processing
          5. Redirecting the user to the login screen if access to the resource is protected
        3. How it works…
        4. There's more…
    10. 3. Processing Requests
      1. Introduction
      2. Understanding the default routing strategy for controllers
        1. Getting ready
        2. How to do it…
      3. Handling a request along multiple controllers
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Using flexible key-value action parameter pairs
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Restricting controllers by managing end user privileges
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Making unsuccessful requests fail softly
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Creating a server-sent message server
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Creating a Ratchet Websocket server
        1. Getting ready
        2. How to do it…
        3. How it works…
    11. 4. Dealing with Data
      1. Introduction
        1. Preparation
      2. Applying your own naming conventions to models
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Defining relationships between models
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Using the query builder for complex yet fluent model querying
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Being aware of PHQL capabilities
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Fetching models from raw SQL queries
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Getting the most out of your RDBMS of choice
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Using models as information repositories
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Storing models across multiple databases
        1. Getting ready
        2. How to do it…
        3. How it works…
    12. 5. Presenting Your Application
      1. Introduction
      2. HTML fragments for AJAX-based applications
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Splitting your layout structure
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Volt and dynamic view implementations
        1. Getting ready…
        2. How to do it…
        3. How it works…
      5. Presenting models nicely
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Reusing view snippets
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Creating your own Volt keywords and methods
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    13. 6. Making Use of Advanced Features
      1. Introduction
      2. Registering dependencies in an effective way
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. There's more…
      4. See also
      5. Using the DI container on different scopes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Unleashing the real power of event driven programming
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      7. Centralizing validations for rock solid business rules
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      8. Complex routing with regular expressions
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      9. Using in-memory session handlers
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      10. Handy persistence for controllers and components
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      11. Transactional controller actions ensuring consistent operations
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      12. Auditing complex user actions with simplicity
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    14. 7. Debugging and Profiling
      1. Introduction
      2. Improved exception reporting
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Logging server messages to the browser console
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Creating a route test suite
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Handling abnormal terminations
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Detecting slow queries
        1. Getting ready
        2. How to do it…
        3. How it works…
    15. 8. Fine Tuning and Optimizing
      1. Introduction
      2. Handling a database timeout
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Precompiling Volt templates for increased performance and security
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Optimizing PHQL queries
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Caching model metadata and query results for faster performance
        1. Getting ready
        2. How to do it…
        3. How it works…
    16. 9. High Performance Applications with Phalcon
      1. Introduction
      2. Introduction to asynchronous work by using Beanstalk
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      3. Implementing a three-level cache to increase performance
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      4. Cache view fragments or full pages in Phalcon
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      5. Implementing your own cache adapters
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Cache data to reduce access to database systems
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      7. Creating a PHP extension using Zephir
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      8. Extending Phalcon using Zephir
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
    17. 10. Securing Your Applications
      1. Introduction
      2. Securing data with encryption
        1. Getting ready…
        2. How to do it…
        3. How it works…
      3. Securing passwords with hashing
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Preventing Cross-site scripting (XSS) attacks
        1. Getting ready…
        2. How to do it…
        3. How it works…
      5. Preventing Cross-Site Request Forgery (CSRF) attacks
        1. Getting ready…
        2. How to do it…
        3. How it works…
      6. Implementing alternative access control lists
        1. Getting ready…
        2. How to do it…
        3. How it works…
      7. Using OAuth for account authorization
        1. Getting ready…
        2. How to do it…
        3. How it works…
    18. Index