Phalcon Cookbook
By David Schissler and Serghei Iakovlev
()
About this ebook
Related to Phalcon Cookbook
Related ebooks
Entity Framework Core Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsFlask Framework Cookbook Rating: 5 out of 5 stars5/5Delphi Cookbook - Second Edition Rating: 5 out of 5 stars5/5Laravel 5.x Cookbook Rating: 0 out of 5 stars0 ratingsElixir Cookbook Rating: 0 out of 5 stars0 ratingsCakePHP 2 Application Cookbook Rating: 0 out of 5 stars0 ratingswxPython Application Development Cookbook Rating: 0 out of 5 stars0 ratingsService Worker Development Cookbook Rating: 0 out of 5 stars0 ratingsMeteor Cookbook Rating: 5 out of 5 stars5/5Delphi Cookbook Rating: 0 out of 5 stars0 ratingsYii2 Application Development Cookbook - Third Edition Rating: 0 out of 5 stars0 ratingsPhpStorm Cookbook Rating: 0 out of 5 stars0 ratingsVisualforce Development Cookbook Rating: 0 out of 5 stars0 ratingsDart Cookbook Rating: 0 out of 5 stars0 ratingsAlfresco 3 Cookbook Rating: 0 out of 5 stars0 ratingsServiceNow Cookbook Rating: 0 out of 5 stars0 ratingsPHP 7 Programming Cookbook Rating: 0 out of 5 stars0 ratingsD Cookbook Rating: 0 out of 5 stars0 ratingsBusiness Intelligence Cookbook: A Project Lifecycle Approach Using Oracle Technology Rating: 0 out of 5 stars0 ratingsServiceStack 4 Cookbook Rating: 0 out of 5 stars0 ratingsLiferay 6.x Portal Enterprise Intranets Cookbook Rating: 0 out of 5 stars0 ratingsOdoo Development Cookbook Rating: 0 out of 5 stars0 ratingsWeb Development with Django Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsLift Application Development Cookbook Rating: 0 out of 5 stars0 ratingsLaravel Application Development Cookbook Rating: 0 out of 5 stars0 ratingsOracle ADF Faces Cookbook Rating: 0 out of 5 stars0 ratingsPuppet 2.7 Cookbook Rating: 3 out of 5 stars3/5NHibernate 4.x Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsOracle Data Integrator 11g Cookbook Rating: 0 out of 5 stars0 ratingsC# Programming Cookbook Rating: 0 out of 5 stars0 ratings
Internet & Web For You
No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsSocial Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Coding For Dummies Rating: 5 out of 5 stars5/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Get Rich or Lie Trying: Ambition and Deceit in the New Influencer Economy Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5200+ Ways to Protect Your Privacy: Simple Ways to Prevent Hacks and Protect Your Privacy--On and Offline Rating: 0 out of 5 stars0 ratingsPodcasting For Dummies Rating: 4 out of 5 stars4/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Remote/WebCam Notarization : Basic Understanding Rating: 3 out of 5 stars3/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsMore Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5The Cyber Attack Survival Manual: Tools for Surviving Everything from Identity Theft to the Digital Apocalypse Rating: 0 out of 5 stars0 ratingsThe Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5The Internet Is Not What You Think It Is: A History, a Philosophy, a Warning Rating: 4 out of 5 stars4/5How To Start A Podcast Rating: 4 out of 5 stars4/5How to Destroy Surveillance Capitalism Rating: 4 out of 5 stars4/5Introduction to Internet Scams and Fraud: Credit Card Theft, Work-At-Home Scams and Lottery Scams Rating: 4 out of 5 stars4/5
Reviews for Phalcon Cookbook
0 ratings0 reviews
Book preview
Phalcon Cookbook - David Schissler
Table of Contents
Phalcon Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why Subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There’s more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Used to Phalcon
Introduction
Getting your IDE to work nicely with Phalcon
Getting ready
How to do it…
Ways to enable Phalcon API autocompletion in major IDEs
PhpStorm
NetBeans
Ways to enable Volt syntax highlighting in major IDEs
Netbeans
PhpStorm
Syntax highlighting for Sublime Text or TextMate
Installation via Package Control
Manual installation
How it works…
Creating the application directory structure
Getting ready
How to do it…
How it works…
Setting up your request entry point
Getting ready
How to do it…
How it works…
Easily loading code on demand
Getting ready
How to do it…
How it works…
There's more…
Setting up your request entry point
Getting ready
How to do it…
How it works…
There's more…
Understanding the request life cycle
Getting ready
How to do it…
How it works...
There's more…
See also
2. Structuring Your Projects
Introduction
Choosing the best place for an implementation
Getting ready
How to do it…
How it works…
Automation of routine tasks
Getting ready
How to do it…
How it works…
There's more…
Creating the application structure by using code generation tools
Getting ready
How to do it…
How it works…
There's more…
Get more power by adding a middleware between Phalcon and your application
Getting ready
How to do it…
Logging critical errors
Changing the description and keywords meta tags depending on your route
Receiving site status reports
Caching pages to reduce processing
Redirecting the user to the login screen if access to the resource is protected
How it works…
There's more…
3. Processing Requests
Introduction
Understanding the default routing strategy for controllers
Getting ready
How to do it…
Handling a request along multiple controllers
Getting ready
How to do it…
How it works…
Using flexible key-value action parameter pairs
Getting ready
How to do it…
How it works…
Restricting controllers by managing end user privileges
Getting ready
How to do it…
How it works…
Making unsuccessful requests fail softly
Getting ready
How to do it…
How it works…
Creating a server-sent message server
Getting ready
How to do it…
How it works…
Creating a Ratchet Websocket server
Getting ready
How to do it…
How it works…
4. Dealing with Data
Introduction
Preparation
Applying your own naming conventions to models
Getting ready
How to do it…
How it works…
Defining relationships between models
Getting ready
How to do it…
How it works…
Using the query builder for complex yet fluent model querying
Getting ready
How to do it…
How it works…
Being aware of PHQL capabilities
Getting ready
How to do it…
How it works…
Fetching models from raw SQL queries
Getting ready
How to do it…
How it works…
Getting the most out of your RDBMS of choice
Getting ready
How to do it…
How it works…
Using models as information repositories
Getting ready
How to do it…
How it works…
Storing models across multiple databases
Getting ready
How to do it…
How it works…
5. Presenting Your Application
Introduction
HTML fragments for AJAX-based applications
Getting ready
How to do it…
How it works…
There's more…
See also
Splitting your layout structure
Getting ready
How to do it…
How it works…
Volt and dynamic view implementations
Getting ready…
How to do it…
How it works…
Presenting models nicely
Getting ready
How to do it…
How it works…
There's more…
See also
Reusing view snippets
Getting ready
How to do it…
How it works…
Creating your own Volt keywords and methods
Getting ready
How to do it…
How it works…
There's more…
6. Making Use of Advanced Features
Introduction
Registering dependencies in an effective way
Getting ready
How to do it…
How it works…
There's more…
See also
Using the DI container on different scopes
Getting ready
How to do it…
How it works…
There's more…
See also
Unleashing the real power of event driven programming
Getting ready
How to do it…
How it works…
There's more…
Centralizing validations for rock solid business rules
Getting ready
How to do it…
How it works…
There's more…
Complex routing with regular expressions
Getting ready
How to do it…
How it works…
There's more…
Using in-memory session handlers
Getting ready
How to do it…
How it works…
There's more…
Handy persistence for controllers and components
Getting ready
How to do it…
How it works…
There's more…
Transactional controller actions ensuring consistent operations
Getting ready
How to do it…
How it works…
There's more…
Auditing complex user actions with simplicity
Getting ready
How to do it…
How it works…
There's more…
7. Debugging and Profiling
Introduction
Improved exception reporting
Getting ready
How to do it…
How it works…
Logging server messages to the browser console
Getting ready
How to do it…
How it works…
Creating a route test suite
Getting ready
How to do it…
How it works…
Handling abnormal terminations
Getting ready
How to do it…
How it works…
Detecting slow queries
Getting ready
How to do it…
How it works…
8. Fine Tuning and Optimizing
Introduction
Handling a database timeout
Getting ready
How to do it…
How it works…
Precompiling Volt templates for increased performance and security
Getting ready
How to do it…
How it works…
Optimizing PHQL queries
Getting ready
How to do it…
How it works…
Caching model metadata and query results for faster performance
Getting ready
How to do it…
How it works…
9. High Performance Applications with Phalcon
Introduction
Introduction to asynchronous work by using Beanstalk
Getting ready
How to do it…
How it works…
There's more…
Implementing a three-level cache to increase performance
Getting ready
How to do it…
How it works…
There's more…
Cache view fragments or full pages in Phalcon
Getting ready
How to do it…
How it works…
There's more…
Implementing your own cache adapters
Getting ready
How to do it…
How it works…
There's more…
Cache data to reduce access to database systems
Getting ready
How to do it…
How it works…
There's more…
Creating a PHP extension using Zephir
Getting ready
How to do it…
How it works…
There's more…
Extending Phalcon using Zephir
Getting ready
How to do it…
How it works…
There's more…
See also
10. Securing Your Applications
Introduction
Securing data with encryption
Getting ready…
How to do it…
How it works…
Securing passwords with hashing
Getting ready
How to do it…
How it works…
Preventing Cross-site scripting (XSS) attacks
Getting ready…
How to do it…
How it works…
Preventing Cross-Site Request Forgery (CSRF) attacks
Getting ready…
How to do it…
How it works…
Implementing alternative access control lists
Getting ready…
How to do it…
How it works…
Using OAuth for account authorization
Getting ready…
How to do it…
How it works…
Index
Phalcon Cookbook
Phalcon Cookbook
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2016
Production reference: 1281116
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-688-6
www.packtpub.com
Credits
Authors
David Schissler
Serghei Iakovlev
Reviewer
Nikolaos Dimopoulos
Acquisition Editor
Reshma Raman
Content Development Editor
Parshva Sheth
Technical Editor
Rutuja Vaze
Copy Editor
Safis Editing
Project Coordinator
Ritika Manoj
Proofreaders
Safis Editing
Indexer
Tejal Daruwale Soni
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
About the Authors
David Schissler was born and raised in Northern California. He received a significant portion of his education from Santa Rosa Junior College, Chico State University, and ultimately received a Bachelor of Computer Science from Sonoma State University.
David started his career as a programmer working with VB.net before transitioning solely into web development. He is a longtime Linux user and is an avid supporter and user of completely open source development platforms, including, the firmware, operating system, programming languages, and text editors.
David is a world traveler who has lived on three different continents for extended periods of time. He is quite comfortable living out of a single bag while developing web software, writing books, and traveling the world.
I would like to thank Bill Wiggins for sharing our dreams and for my profound cafe experiences in many parts of the world. I would like to thank John Gudgeon for giving me that necessary kick and helping me evacuate my apartment so that I could jump onto that fateful sail boat heading south , and my father Jim Schissler for writing puzzle games for the TRS-80 that we hooked up to the living room television. I’d also like to thank everyone that has been supportive of my education and adventures.
Serghei Iakovlev has been working as a Ruby/PHP developer for over 7 years and has been specializing in web application development. As a supporter and advocate of Behavior Driven Development (BDD), he is also a member of the Codeception team.
Serghei can be found neck deep in innovative technologies, which introduced him to Phalcon. He is responsible for the coordination of bug fixes, testing, developer tools, incubator, and supports other Phalcon-related projects.
When he’s not working, Serghei enjoys watching documentaries, science fiction movies, and sometimes plays Mahjong or Go.
I’d like to thank my wife, Yaroslava, for her patience, encouragement, and assistance in translation of my part of the book. I’d also like to thank my technical leader, Andres Gutierrez, for creating such a wonderful framework, and my team mate, Nikolaos Dimopoulos, for his assistance in technical details and for all his recommendations. These people helped me get this book finished.
About the Reviewer
Nikolaos (Nikos) Dimopoulos has over 20 years of progressive experience in designing and developing applications. He is currently serving as a Senior Developer/Analyst for ITWorks Inc. Nikolaos has always been passionate about quality, testability, and good design. Throughout his career, he has been everywhere, from leading the Technical Cooperation Project Monitoring and Management System (TCPMMS) at the UN/International Atomic Energy Agency in Vienna, to being a main catalyst in the growth of a widely popular online community.
He is constantly curious about new technologies and finds ways to increase the performance of his applications. This led him to discover PhalconPHP a few years back. With his contributions, it was not long before he became a member of the core team, helping with documentation, tests, and other duties.
In his free time, he enjoys time with his family, contributes to open source, writes his thoughts on his blog, and tries to keep up to speed with technology.
I would like to thank my family for all the support they have given me throughout the review of this book. Without it, I would have never made it.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Preface
Phalcon was created to maximize the performance of PHP applications. Along the way, Phalcon adopted many industry best practices and became a model of what a modern PHP application should look like.
In the beginning, Phalcon was written purely in C language, and this made it difficult for many types of developers to contribute to the project. Starting with version 2.0, the project was rewritten in a new PHP resembling language called Zephir, which allowed many more people to contribute. Recently, with the release of Phalcon 3.0, the framework has begun its first period of long-term support and adherence to semantic versioning.
What this book covers
Chapter 1, Getting Used to Phalcon, demonstrates how to start a Phalcon project, configure your IDE to make your work on the project easier, and brings to light the main principles you must understand while dealing with the Phalcon Framework.
Chapter 2, Structuring Your projects, covers the project structure. This chapter demonstrates one of the optional versions of the project structure and helps configuring routing and introduces how to use middlewares in your project.
Chapter 3, Processing Requests, takes you through the process of a normal web request as well as explores the Websockets and EventSource request models.
Chapter 4, Dealing with Data, teaches you to the skills to deal with real data using relations, PHQL, or raw SQL and how to use customized database adapters.
Chapter 5, Presenting Your Application, introduces Views. It covers aspects such as using HTML fragments, working with layout structure, viewing snippets, as well as working with Volt Template Engine.
Chapter 6, Making Use of Advanced Features, covers the strong points of Phalcon Framework, such as dependency injection, validation, working with sessions, event management, complex routing, and so on.
Chapter 7, Debugging and Profiling, demonstrates how to handle the unusual and troublesome aspects of a project.
Chapter 8, Fine Tuning and Optimizing, talks about how to optimize a project for high throughput and reliability.
Chapter 9, High Performance Applications with Phalcon, covers the work with high load projects. You will learn how to work with the message queues and caching. This chapter also introduces Zephir language. It covers the main principles of creating extensions in Zephir and the Phalcon Framework enhancement.
Chapter 10, Securing Your Applications, avoids common attacks and implement an ACL with password hashing.
What you need for this book
This book requires PHP 5.5 or later, a web server, database server, and command line access. Although any web and database server can be used in this book, the reader will have a much easier time with Apache and MySQL. This book makes extensive use of the Phalcon Developer Tools to create projects, and this will require command line usage. Some recipes in this book will require specific PHP extensions and so the reader will need to have the ability to install these extensions on their machine as well as the ability to restart the web and database server, to read the web server logs and many other things that require administrator access. Windows, Mac, and Linux are all supported environments.
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.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There’s more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
Getting ready
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous section.
There’s more…
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: The stubs are located in the phalcon-devtools repository you just cloned under the /ide subfolder
A block of code is set as follows:
class Hats extends Phalcon\Mvc\Model
{
}
Any command-line input or output is written as follows:
$phql = SELECT Hats.* FROM Hats JOIN Colors WHERE Colors.name = ‘red’
;
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Create a user by entering the user name and password and clicking the Create a User button
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book’s title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book 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.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you’re looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book’s webpage at the Packt Publishing website. This page can be accessed by entering the book’s name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Phalcon-Cookbook.We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <questions@packtpub.com>, and we will do our best to address the problem.
Chapter 1. Getting Used to Phalcon
In this chapter, we will cover the following topics:
Getting your IDE to work nicely with Phalcon
Creating the application directory structure
Setting up your request entry point
Easily loading code on demand
Initializing Phalcon to handle a request
Understanding the request life cycle
Introduction
Despite the fact that Phalcon is a framework for PHP application development, it isn't itself PHP-based. Therefore, some difficulties may appear when using syntax highlighting or code autocompletion. In this chapter, we will try to study how to set up autocompletion in a modern Integrated Development Environment (IDE) and how to enable code autocompletion.
An essential step in the architecture process when developing an application is a clear understanding of the project directory structure and the role of each of the directories. We will use a variation of a Phalcon project directory structure, which is easy to deploy and upgrade. The directory structure, which we work with in this chapter, isn't a rule set in stone. In future you will, time after time, use different directory structures, which will be different than the one we will be using. That structure is only one of the options and a starting point for your needs, and it easily responds to changes caused by certain technical conditions.
Phalcon is not only the fastest framework in the world, it is one of the easiest frameworks. As a rule, you don't need any special knowledge or additional software. However, when developing an application with Phalcon, you will need to create a base system configuration and application skeleton. In this chapter, we will learn how to deploy an application with the use of Phalcon, how to make its base configuration, and how to create a flexible and efficient application skeleton, which you can use in future.
Any object-oriented PHP application needs classes. You have to tell your class autoloader where your classes are situated and which way they can be searched for. We will look at some possible options of class auto-loading in Phalcon and their usage methods.
Many developers, despite their experience, have difficulty when working with a Request component. Phalcon isn't an exception. We will discover how to make a base application configuration so that Phalcon starts handling requests. We will learn how Phalcon handles these requests and in what sequence they are executed. We will learn which components take part in a request life cycle, where it is possible to catch the request, how to specify its state, as well as how to take any suitable actions.
Getting your IDE to work nicely with Phalcon
Most IDEs have some form of code completion as part of the program. To enable auto-completion for Phalcon's namespaces, we will need to help the IDE recognize those namespaces. We will also see how to enable Volt syntax highlighting and Phalcon API auto-completion for major IDEs.
Getting ready
To get started, we should have Git installed. To enable auto-completion in your IDE, you need to get the Phalcon stubs. For these purposes, clone the Phalcon Developer Tools repository:
git clone git@github.com:phalcon/phalcon-devtools.git
How to do it…
The following are the steps needed to complete this recipe.
Ways to enable Phalcon API autocompletion in major IDEs
PhpStorm
Using the Project Tool window in PhpStorm, select External Libraries, right-click on the section, and select Configure PHP Include Paths... from the drop-down menu.
Then, using the External Libraries dialog box, click the + button. Next, select the Specify Other... option in the drop-down menu and specify the location of the Phalcon stubs. The stubs are located in the phalcon-devtools repository you just cloned under the /ide subfolder. You should specify the path phalcon-devtools/ide/stubs/Phalcon and click on Apply.
NetBeans
In NetBeans, you need to open the Projects window (Ctrl + F1) and select Include Path. Right-click on the section and select Properties from the drop-down menu.
Then, open the Project Properties window and click on the Add Folder... button. Next, specify the location of the Phalcon stubs. The stubs are located in the phalcon-devtools repository you just cloned under the /ide subfolder. You should specify the path phalcon-devtools/ide/stubs/Phalcon and click on the OK button.
Ways to enable Volt syntax highlighting in major IDEs
Netbeans
Click on the Tools menu entry and select Options.
In the opened window, select Miscellaneous | Files.
In the File Associations group box, click on the button New… next to the File Extensions option, enter the extension volt, and click on OK.
Select TWIG(text/x-twig) in the Associated File Type (MIME) drop-down menu.
PhpStorm
Open File | Settings. Next select Editor | File Types.
In the Recognized File Types group box, select Twig and add the extension *.volt into the Registered Patterns group box by pressing the + button.
Syntax highlighting for Sublime Text or TextMate
Installation via Package Control
If you have Sublime Package Control, you know what to do. If not, well, it's a package manager for Sublime Text, and it's awesome!
After installing Sublime Package Control and restarting the editor:
Open theCommand Palette (Ctrl + Shift + P or Cmd + Shift + P).
Type Install Package and press Enter.
Find Volt and press Enter.
Manual installation
For manual installation, clone the Volt syntax highlight for the Sublime Text 2/Textmate repository:
git@github.com:phalcon/volt-sublime-textmate.git
Depending on your OS, copy the volt-sublime-textmate/Volt directory to any of the following locations:
Sublime Text 2:
Mac OS X:
~/Library/Application Support/Sublime Text 2/Packages
Linux:
~/.Sublime Text 2/Packages
Windows:
%APPDATA%/Sublime Text 2/Packages/
Sublime Text 3:
Mac OS X:
~/Library/Application Support/Sublime Text 3/Packages
Linux:
~/.Sublime Text 3/Packages
Windows:
%APPDATA%/Sublime Text 3/Packages/
TextMate:
/Library/Application Support/TextMate/Bundles
How it works…
Some IDEs need help understanding the framework syntax. To get the IDE to understand, we download a list of all the Phalcon stubs. Then, when we add it to the include path, NetBeans (or PhpStorm) will automatically check the file and show us the autocomplete options. To enable the Volt or Zephir syntax highlight, we have to configure our IDE or text editor accordingly.
Creating the application directory structure
In this recipe, we will take a brief look at the commonly used directory structure of a single module application. A structure of this type is aimed at providing a great starting point for different applications.
Getting ready
We don't need