39,59 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 248
Veröffentlichungsjahr: 2015
Copyright © 2015 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 author, 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: August 2015
Production reference: 1210815
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78355-509-3
www.packtpub.com
Author
Calin Rada
Reviewers
Altaf Hussain
Stephan A. Miller
Dilanka Somarathne
Commissioning Editor
Kunal Parikh
Acquisition Editors
Harsha Bharwani
Rebecca Youe
Content Development Editor
Amey Varangaonkar
Technical Editor
Menza Mathew
Copy Editor
Vikrant Phadkay
Project Coordinator
Bijal Patel
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
Calin Rada is a full-stack developer with over 10 years of experience in web development; system architecture; database modeling; the setup, configuration, and administration of servers and hosting systems; and understanding customer and business needs. He is always interested in learning new things and working with new technologies.
I'd like to thank my family and all the people involved in writing this book.
"Touch the untouchable star and don't forget about the people who believed in you."
--Octavian PalerAltaf Hussain is an electrical engineer on paper and a software engineer at heart. He is an e-commerce and mobile applications enthusiast. He acquired his BE degree in electrical engineering (with specialization in computers and communication) from Pakistan. He then worked for numerous organizations as a backend developer and moved to Saudi Arabia to work as a software engineer.
Currently, Altaf is working in the fashion industry at Shy7lo.com. He manages dedicated servers, different VPSes, Staging Servers, and GitLab instances for fast deployment. As a senior team member, he is responsible for creating cross-platform mobile applications and APIs. He also works on different caching systems, such as Varnish and Full Page Cache. In his free time, Altaf writes posts for http://www.programmingtunes.com.
Stephan A. Miller is a software engineer from Kansas City, Missouri, USA. He is currently working for Kinetic Supply Company. He has worked with open source software technologies for over a decade. Some of the languages that he uses are PHP, JavaScript, and Python. He also uses various frameworks in these languages, including Phalcon, Zend, Laravel, jQuery, AngularJS, and Flask. Stephan has written Piwik Web Analytics Essentials and Getting Started with Phalcon for Packt Publishing.
Dilanka Somarathne has worked with the LAMP stack for 2 years. He always tries to learn something new. He performs experiments in PHP to find out the best PHP framework for programming. He also conducted a tech talk session in benchmarking several PHP frameworks at his previous organization, thinkCube Systems, for which he received good feedback from the audience and appreciation from other technical leads and architects. Dilanka has experience of working on Laravel, Zend Framework 2, CodeIgniter, and Phalcon. He has worked on Node.js and Angular projects too.
Phalcon is a new and very fast PHP framework. He started following and learning Phalcon after he attended a Phalcon meetup. As his first step, he created a small application with Phalcon. Because of his amazing experience with the application, he was able to get a clear idea about it.
I feel fortunate to get a chance to join the reviewing panel of Packt Publishing. Leena Purkait gave me this awesome chance. I would like to take this opportunity to thank her. Then, Bijal Patel joined the team. From the first day, she has been very helpful and has always replied to my e-mails patiently. Thank you very much, Bijal.
Working as a Phalcon reviewer for Packt Publishing was a really good experience for me. Thank you very much guys for giving me this huge opportunity.
For support files and downloads related to your book, please visit www.PacktPub.com.
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 <[email protected]> for more details.
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://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Phalcon is the fastest PHP framework out there, and it is delivered as a C extension. More than that, you will find it very easy to learn. This book will present in detail the most common and useful parts of Phalcon PHP, as well as guide you to lean towards making the right decision when developing a Phalcon-driven application.
Learning Phalcon PHP is an interesting journey that starts with guides for installing the required software and preparing the working environment and project structure, and continues with a step-by-step approach development of each module.
By the end of this book, you will have developed a simple but fully functional news website and gained advanced knowledge on how Phalcon works.
Chapter 1, Getting Started with Phalcon, introduces the Phalcon Framework. In this chapter, you learn how to install and configure Phalcon.
Chapter 2, Setting Up the MVC Structure and the Environment for Our Project, helps you get to grips with the basics of MVC (Model-View-Controller) and setting up the work environment.
Chapter 3, Learning Phalcon's ORM and ODM, is about Phalcon's ORM (Object-relational Mapping) and ODM (Object-Document Mapper). You learn how to connect to a database and create models and relations between them.
Chapter 4, Database Architecture, Models, and CLI Applications, teaches you how to create the database architecture and the models needed for our project. You also learn about Phalcon CLI and develop a simple CLI application.
Chapter 5, The API Module, helps you start the development of a RESTful API module.
Chapter 6, Assets, Authentication, and ACL, explains assets management (JavaScript files, style sheets, and images), and you create a simple authentication system based on an ACL (access control list).
Chapter 7, The Backoffice Module (Part 1), shows you how to develop CRUD operations. This first part is about CRUD for categories and hashtags.
Chapter 8, The Backoffice Module (Part 2), is a continuation of the previous chapter. Here, you develop CRUD operations for users and articles.
Chapter 9, The Frontend Module, helps you develop the frontend template. You learn how to implement Elasticsearch and Mongo to improve the speed of your application.
Chapter 10, Going Further, teaches you common operations, such as file uploads and annotations.
The most important thing that you need is some knowledge of PHP 5.3 or later and Linux environments (this book is written based on Ubuntu/Debian). If you are not using a Linux distribution, or you are using a distribution other than Ubuntu/Debian, you will need to look up their official documentation to install the required software.
If you are an intermediate PHP developer with some basic knowledge of installing and configuring your environment, then this book is for you. Familiarity with PHP frameworks will make your life easier.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via 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 on www.packtpub.com/authors.
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.
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.
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 would 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 erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright 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 <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
What is Phalcon? Let's start by quoting from the documentation of the official website (http://phalconphp.com/):
"Phalcon is an open source, full stack framework for PHP written as a C-extension, optimized for high performance."
Version 2.0 of Phalcon was released in April, and it was developed with a new language called Zephir (http://zephir-lang.com/). Zephir was designed especially for developing PHP extensions, and it is quite user friendly for both (PHP and C) developers.
There are many frameworks out there. The main reasons why we choose Phalcon were for its steep learning curve, speed, and because it is decoupled. (We can use any of its components independently.) If you have some knowledge of the Model-View-Controller (MVC) and some experience with anyObject-Relational Mapping (ORM), you will find working with it pretty straightforward.
We will start our journey with this first chapter where we will:
Before starting, we assume that you are using a *nix environment. Personally, I feel comfortable with Debian distributions, especially Ubuntu, which I am using on a daily basis; so, the installations steps that we will talk about are for Ubuntu. The OS is a matter of personal choice, but I highly recommend any *nix distribution for development. (Even Microsoft decided to open source their ASP.NET for Linux early this year)
For other types of OS, you will have to search their official documentation, in terms of "how to". This book is intended to be about Phalcon and tutorials on installing different software on different kinds of OS are out of the scope of this book.
Here is the list of URLs that contain installation instructions for different operating systems:
Senior developers might not agree with me on certain subjects or certain techniques and/or recommendations. In general, as a developer, I think you should analyze what is suitable for you and develop a platform according to your (or client) requirements. In addition, most importantly, there is no such thing as "The Perfect Solution". There is always room for improvement.
We need to install the following software that we are going to use in this book:
You have probably already installed PHP on your system since you are reading this book. However, just in case you haven't, here are the simple steps to quickly install the latest PHP version (Phalcon is running on PHP version >= 5.3). I recommend you to use the Personal Package Archive (PPA) from Ondřej Surý (https://launchpad.net/~ondrej/+archive/ubuntu/php5) because it has the latest PHP version available on it:
If you don't want to use this step, you can simply install PHP from the official repositories:
Apache will be installed by default with PHP. However, if you want Nginx instead of Apache, you must install PHP in a certain order.
The following command will automatically install PHP and Apache. If you don't need/want to use Apache, please skip using this command:
To avoid Apache installation, execute the following commands in the exact same order:
The php5-cgi package fulfills the dependencies that would otherwise be fulfilled by Apache.
To install the Nginx web server, we need to execute the following commands:
MySQL is probably the most widely spread RDBMS system with a market share that is greater than 50 percent. Since we are going to use it to develop our project, we need to install it by executing the following command:
Downloading the example code
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.
Redis is an advanced key-value storage/cache system. We are going to use this mostly for our session and to cache objects to improve the speed of our application. Let's install it by executing the following commands:
MongoDB is a document database (NoSQL database) system. We will use this to store data that is accessed frequently. Let's install it:
Git is a distributed version control system that we will use to track changes to our application and much more. We will install Git by executing the following command:
I strongly recommend that you use the latest versions of all software as much as possible.
Now that we have installed all the required software, we will proceed with the installation of Phalcon. Before we continue, we must install some dependencies:
For Windows systems and more details about how to compile the extension on different systems, please check the latest documentation at http://phalconphp.com/en/download.
Now, we can clone the repository and compile our extension:
If everything goes well, you should be able to see Phalcon in the list of PHP installed modules: