You are previewing Learning Phalcon PHP.
O'Reilly logo
Learning Phalcon PHP

Book Description

Learn Phalcon interactively and build high performance web applications

About This Book

  • Learn how to install and configure Phalcon PHP on your server

  • Develop a fully functional multi-module application with Phalcon PHP

  • A step-by-step guide with in-depth coverage of Phalcon and best practices

  • Who This Book Is For

    If you are a web developer and want to build effective web applications with Phalcon PHP, then this book is ideal for you. The book does not assume detailed knowledge of PHP frameworks.

    What You Will Learn

  • Compile and install Phalcon from source code

  • Set up an MVC project and learn how to use the Dependency Injection (DI), configuration, and routing

  • Get to grips with Phalcon's ORM, ODM, validation and caching, and generate models using Phalcon Developer Tools

  • Create a command-line application from scratch

  • Write a fully functional REST module with Phalcon PHP

  • Use Volt as template engine to create your first views

  • Understand how session works and learn how to implement an authentication system

  • In Detail

    Phalcon is a full-stack PHP framework implemented as a C extension. Building applications with Phalcon will offer you lower resource consumption and high performance whether your application runs on a Linux machine or a Windows one. Phalcon is loosely coupled, allowing you to use its objects as glue components based on the needs of your application. Phalcon PHP's mission is to give you an advanced tool to develop faster websites and applications.

    This book covers the most common and useful parts of PhalconPHP, which will guide you to make the right decisions while developing a Phalcon-driven application.

    You will begin the journey by installing and setting up Phalcon for your environment followed by the development of each module. You will be introduced to Phalcon's ORM and ODM. Furthermore, you will also be able to create the first models and database architecture for your project. You will then cover command-line applications, API module, volt syntax, and hierarchical views. Installing and working with Node and Bower for assets management will also be covered. Finally, you will gain insights into creating the backoffice and frontend module along with best practices and resources for development with Phalcon PHP.

    By the end of this book, you will be able to confidently develop any kind of application using the Phalcon PHP framework in a short time.

    Style and approach

    This step-by-step guide will walk you through the fundamentals of Phalcon PHP. It will then help you to create a simple but fully functional news website and gain advanced knowledge of how Phalcon PHP works.

    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 files e-mailed directly to you.

    Table of Contents

    1. Learning Phalcon PHP
      1. Table of Contents
      2. Learning Phalcon PHP
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Phalcon
        1. Installing the required software
          1. Installing PHP
          2. Installing Nginx
          3. Installing MySQL
          4. Installing Redis
          5. Installing MongoDB
          6. Installing Git
        2. Installing Phalcon
        3. The Apache and Nginx configuration files
          1. Apache
          2. The host file
          3. Nginx
        4. Understanding the framework's internals
          1. The dependency injection
            1. The request component
            2. The response component
            3. The logger component
            4. The crypt component
            5. The flash component
            6. The router component
            7. The config component
            8. The view component
            9. The session component
            10. The cache component
        5. Summary
      9. 2. Setting Up the MVC Structure and the Environment for Our Project
        1. What is MVC?
          1. Model
          2. View
          3. Controller
        2. The MVC structure
        3. Creating the structure for our project
          1. PSR
        4. Creating the configuration file and the Bootstrap
        5. Preparing the initial DI interface and the router
        6. Using the router component in a module
        7. Create the base layout
        8. Summary
      10. 3. Learning Phalcon's ORM and ODM
        1. The main differences between SQL and NoSQL databases
        2. Connecting to the database
        3. ORM/ODM operations (create, update, delete, transactions, and validations)
          1. Adding the routing information
          2. Creating the controller and the actions
          3. Creating the views
        4. CRUD operations using ORM
          1. CRUD – reading data
          2. CRUD – creating data
          3. CRUD – updating data
          4. CRUD – deleting data
        5. Using PHQL
        6. Using raw SQL
        7. Database transactions
          1. A manual transactions example
          2. An implicit transactions example
          3. An isolated transactions example
        8. ODM/MongoDB
        9. ORM – drawbacks and caching
        10. Summary
      11. 4. Database Architecture, Models, and CLI Applications
        1. The database architecture
          1. The User table
          2. The UserGroup table
          3. The UserProfile table
        2. Models
          1. The User model
          2. The UserGroup model
          3. The UserProfile model
          4. Registering a new user
          5. Creating a user profile
          6. The Category model
          7. The Category translation model
          8. The Article translation model
          9. The Article model
          10. The Article-Category-Article model
        3. Summary
      12. 5. The API Module
        1. Using APIs – recommended practices
        2. Enabling SSL on our local machine
        3. Creating the module structure
        4. Writing a fully functional REST module with Phalcon PHP
          1. Articles
        5. Securing an API
          1. Using SSL
          2. Adding an API key for extra protection
          3. Limiting the number of requests per second from the same IP
          4. Limiting access to resources such as DELETE, PUT, and POST for authenticated users
        6. Documenting the API
          1. Installation
          2. Usage
        7. Summary
      13. 6. Assets, Authentication, and ACL
        1. Assets management
        2. Developing an authentication system
          1. The database structure
          2. Models
        3. Securing the application using the ACL component
        4. Summary
      14. 7. The Backoffice Module (Part 1)
        1. Editing the main layout
          1. common/topbar.volt
          2. common/sidebar.volt
        2. Cleaning the Core module
        3. Hashtag CRUD
          1. The hashtag controller within the API module
          2. A common method to reduce code duplication
          3. Retrieving the data
          4. The layout structure
          5. The hashtag form
          6. The hashtag controller
          7. The hashtag manager
          8. The View template for the add() method
          9. Improving the database table structure and adding validation
          10. Editing hashtags
          11. Deleting hashtags
        4. Category CRUD
          1. The Category form
          2. Creating the Category templates
            1. list.volt
            2. add.volt
            3. edit.volt
            4. delete.volt
          3. Creating the Category controller
          4. Creating the Category manager
        5. Summary
      15. 8. The Backoffice Module (Part 2)
        1. User CRUD
          1. Creating the controller (API)
          2. The user controller from the Backoffice module
          3. The user form
          4. The user manager
        2. User templates
        3. Article CRUD
          1. The Controller (API)
          2. The Article controller from the Backoffice module
          3. The Article form
          4. The Article manager
        4. Summary
      16. 9. The Frontend Module
        1. The Frontend layout and basic functionality
          1. footer.volt
          2. paginator.volt
          3. navbar.volt
          4. layout.volt
        2. Modifying BaseController.php
          1. The Article item template
          2. Retrieving articles from a category
        3. Implementing ElasticSearch
          1. Installing ElasticSearch
          2. Enabling a client in DI
          3. Indexing (storing) documents
        4. Implementing MongoDB
          1. Mongo models
            1. modules/Core/Models/Mongo/BaseCollection.php
            2. modules/Core/Models/Mongo/ArticleLog.php
        5. Summary
      17. 10. Going Further
        1. Uploading files with Phalcon
        2. Using the Annotation router
        3. Summary
      18. Index