35,99 €
This book is for intermediary to seasoned web developers who want to learn how to use the FuelPHP framework and build complex projects using it. You should be familiar with PHP, HTML, CSS, and JavaScript, but no prior knowledge about MVC frameworks is required.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 284
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: February 2015
Production reference: 1200215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-540-1
www.packtpub.com
Author
Sébastien Drouyer
Reviewers
Ivan Đurđevac
Márk Sági-Kazár
Kenji Suzuki
Aravind Udayashankara
Commissioning Editor
Kartikey Pandey
Acquisition Editor
Nikhil Chinnari
Content Development Editor
Melita Lobo
Technical Editors
Sebastian Rodrigues
Mohita Vyas
Copy Editors
Pranjali Chury
Sameen Siddiqui
Ashwati Thampi
Project Coordinator
Kinjal Bari
Proofreaders
Simran Bhogal
Kevin McGowan
Linda Morris
Indexer
Tejal Soni
Graphics
Sheetal Aute
Disha Haria
Abhinash Sahu
Production Coordinator
Alwin Roy
Cover Work
Alwin Roy
Sébastien Drouyer is a computer science research engineer from France. He has a master's degree in computer science from the National Institute of Applied Sciences of Lyon, one of the most prestigious engineering schools in France. He has been developing web applications since 2005 and has won various contests and awards from GitHub, NASA, and Intel. He has also been a member of the Novius OS core team (an open source content management system based on the FuelPHP framework) and published many additional open source projects. He has trained several teams on FuelPHP and is a conference speaker on the subject.
First of all, I would like to thank the FuelPHP core team and its community for improving this wonderful framework every day.
If there are only a countable number of errors in this book, then it is due to Aravind Udayashankara, Kenji Suzuki, Sági-Kazár Márk, Ivan Đurđevac, Craig Hooghiem, and John Alder who all did excellent reviews.
I would like to salute the amazing Novius OS core team and I wish them the best in their future endeavors.
I would also like to thank the Packt Publishing team, especially Melita Lobo, for their patience and professionalism.
Last, but not least, I would like to thank my very supportive family and friends. A special thanks to my mother and father for making me the person I am today; I wouldn't be here without you two.
Ivan Đurđevac is a PHP developer from Serbia, Pančevo. Long time ago, he found out the "development" word with Delphi. Prior to 2000, the Web was a huge deal, and he decided to pick PHP as his main tool. He started with CodeIgniter as his first framework. After a while, he realized that CodeIgniter did not allow him to write beautiful code to solve problems with design patterns, and he saw it would stay that way, trapped in PHP4. So, he switched to Kohana and Zend Framework and it was a big step forward. FuelPHP was a new kid on the block. It took all the best features from others and created a modern framework. He used FuelPHP to build large-scale applications. At the time this book was written, he used Laravel as his codebase. No matter which framework he uses, clean code is always a priority for him. He will never stop learning from better developers and modern practices.
He has worked for various companies on many projects, such as e-learning platforms, CMS-ES, social network sites, and business applications. Currently, he is working for a USA-based company that builds various internet marketing tools, which collaborate with AWeber, Infusionsoft, and other tools. He is also interested in DevOps and likes to play with Linux administration and set up server boxes and maintain them.
Márk Sági-Kazár was previously working with CodeIgniter, and picked up FuelPHP in 2011. Since 2014, he has been actively developing for FuelPHP, starting with the E-mail package. From the autumn of 2014, he has been an official member of the FuelPHP team.
Mark started playing with programming at the age of 6. While in high school, he worked with several Microsoft languages; Visual Basic is one of them. After finishing high school, he switched to PHP as his programming language of choice. Starting with CodeIgniter, he quickly switched to FuelPHP and delivered his first production application (an e-commerce site) in 2012, followed by IndigoPHP in 2013, which is an application framework and CMS built on top of FuelPHP. Besides his work on FuelPHP V2, he's currently working on some good quality packages such as a SupervisorPHP (http://supervisorphp.com) and shopping cart abstraction, to name some.
Kenji Suzuki is a programmer and web developer living in Japan. He is a contributor to FuelPHP, BEAR.Sunday, CodeIgniter, and many other open source projects. He is a PHP expert, Certified PHP 5 Engineer Expert by the Engineer Certification Corporation for PHP (http://www.phpexam.jp/about/English/), and coauthor of the Japanese best-seller and highly-praised PHP recipe book, PHP gyakubiki reshipi, SHOEISHA.Co.,Ltd. He has published several books about PHP with famous Japanese IT book publishers. His latest book is Hajimeteno Framework toshiteno FuelPHP,Rutles, Inc. You can find the repositories of his various projects on GitHub at https://github.com/kenjis.
Aravind Udayashankara is an autodidactist and software engineer. He has been working on several open source server-side technologies, such as NodeJS, PHP, and Ruby, and browser-side technologies such as AJAX, JavaScript, XML, HTML, and many more since 2008. He loves and enjoys to learn, understand, and express complex things, as well as blog on his own website, http://aravindhu.com. He is now eagerly eyeing the world of mobile application development and big data.
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.
The main idea behind FuelPHP Application Development Blueprints is to teach you FuelPHP's basic and advanced features by building various projects of increasing levels of complexity. It is very result-oriented; at the beginning of the chapters, we specify the application we want to build, and then we progressively implement it by learning how to use new FuelPHP features along the way. Our approach will therefore be very practical; a lot of concepts will be explained using code examples, or by integrating them directly into our projects. Thus, it is important to highlight that there will be a lot of code and you should be comfortable with reading and understanding PHP and HTML. As we will use them from time to time, having some knowledge about server/system administration and some foundation in JavaScript, jQuery, and CSS will be an added advantage.
Though this book is for intermediary to advanced web developers, any prior knowledge of the FuelPHP framework, or any other PHP framework, is not required. In order to understand this book, you don't have to know common concepts such as MVC, HMVC, or ORM. We take into account this shortcoming some of you might have, and important notions will be explained. We won't explain all of those in the first chapter though, as we want this to be as painless as possible; we will instead approach them when they become necessary for the project completion.
The ultimate purpose of FuelPHP Application Development Blueprints is to give you the ability to build any project using FuelPHP. By the end of this book, you certainly won't know every little detail of the framework, but you will hopefully have the necessary toolbox required to implement complex and maintainable projects.
Chapter 1, Building Your First FuelPHP Application, covers the very basics of the FuelPHP framework; how to install it, how to configure it, how it is organized, and its main components. Along the way, we will generate our first FuelPHP application using the oil utility and tweak some files, in order to illustrate how things work.
Chapter 2, Building a To-do List Application, focuses on FuelPHP's ORM and debugging features. We will illustrate these features using a lot of examples, and then implement a small to-do list application. We will also use some JavaScript and jQuery to send AJAX requests.
Chapter 3, Building a Blog Application, will teach you how to generate and tweak an administration interface easily, how to create your own modules and tasks, how to manage paginations easily, and how to use the Auth and Email packages. We will create a blog application implementing all these features.
Chapter 4, Creating and Using Packages, will approach the FuelPHP package system. This is a rather short chapter; we will first try to protect our website from spam bots by installing an existing package, and then create our own original solution by creating a new package.
Chapter 5, Building Your Own RESTful API, covers more advanced subjects such as building a JSON API, using language agnostic template engines, allowing user subscriptions, and implementing unit tests. To illustrate this, we will create a responsive micro blogging application featuring a public API.
Chapter 6, Building a Website Using Novius OS, will quickly introduce you to Novius OS, a FuelPHP-based Content Management System. Using such a system can greatly speed up the implementation of complex projects.
The applications in this book are based on FuelPHP 1.7.2, which requires:
FuelPHP works on Unix-like and Windows operating systems. The mod_rewrite Apache module and some additional PHP extensions are recommended; the complete list is available at http://fuelphp.com/docs/requirements.html.
This book is for intermediary to seasoned web developers who want to learn how to use the FuelPHP framework and to build complex projects using it. You should be familiar with PHP, HTML, CSS, and JavaScript, but no prior knowledge about MVC frameworks is required.
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.
A block of code is set as follows:
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: " Click on the Generate button."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
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 <[email protected]>, 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.
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 from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/5401OS.pdf.
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 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 <[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.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
Throughout the book, we will use the FuelPHP framework to build different types of projects. The objective of this chapter is to make you familiar with the basics of the framework and create your first project as quickly as possible. We won't create anything exceptional in this chapter and there will be very little coding, but we will go through the whole process from installing FuelPHP to publishing your project on a production server. You will learn the necessary basics for the other projects as well.
By the end of the chapter, you should know the following:
Since this book is intended for intermediate developers, we will assume that you have already installed Apache and MySQL on your system. Some prior knowledge of Git and Composer is an added advantage as you might need it, but you should be fine in this book if you are not familiar with these tools. However, for advanced applications that need collaboration between several developers mastering them is highly recommended.
In this chapter, we will go from installing the FuelPHP framework to having a functional – though limited – web application. As our objective here is to introduce the framework and create a sample application as quickly as possible, we won't address important topics such as the ORM, which will be addressed in Chapter 2, Building a To-do List Application.
Dan Horrigan started the FuelPHP framework in late 2010, and was later joined by Phil Sturgeon, Jelmer Schreuder, Harro Verton, Frank de Jonge, Steve West and Márk Sági-Kazár. The first stable version was released on July 31st, 2011 and this book is based on FuelPHP 1.7.2, the latest stable version available as of writing this book. With over 300 contributors, its community is large and active.
The core team is currently working on the second version of FuelPHP; several alpha versions of it have already been released.
If you want to know more about the FuelPHP team and the framework philosophy, I recommend you to read the About FuelPHP section of the official website at:
http://fuelphp.com/about
You can read the latest news about the framework on its official blog at:
http://fuelphp.com/blogs
The official documentation can be found at: http://fuelphp.com/docs/
If you have any questions about FuelPHP or encounter any issues, you can search the official forum (http://fuelphp.com/forums/) and start a new discussion if you don't find any answer. In a general manner, the official website (http://fuelphp.com) is an excellent resource.
The development process of a FuelPHP application generally contains the steps shown in the following image:
Just to be clear, this is a very general guideline, and of course the order of the steps is not rigid. For instance, developers using the test-driven development process could merge the fourth and fifth steps, or a preproduction step could be added. The development process should only depend on each developer and institution's standards.
The FuelPHP framework needs the following three components:
FuelPHP works on Unix-like and Windows operating systems, but the installation and configuration procedures of these components will depend on the operating system used. In the following sections we will provide some directions to get you started in case you are not used to installing your development environment. Please note that these are very generic guidelines, so you might need to search the web for complimentary information. There are countless resources on the topic.
A complete and very popular solution is to install WAMP. This will install Apache, MySQL, and PHP, in other words everything you need to get started. It can be accessed at http://www.wampserver.com/en/.
PHP and Apache are generally installed on the latest version of the OS, so you just have to install MySQL. To do this, you are recommended to read the official documentation at http://dev.mysql.com/doc/refman/5.1/en/macosx-installation.html.
A very convenient solution for those who have the least system administration skills is to installMAMP, the equivalent of WAMP, but for the Mac operating system. It can be downloaded from http://www.mamp.info/en/downloads/.
As this is the most popular Linux distribution, we will limit our instructions to Ubuntu.
You can install a complete environment by executing the following command lines:
The Apache mod_rewrite module and some additional PHP extensions are also recommended, but not required:
http://fuelphp.com/docs/requirements.html (can be accessed through the FuelPHP website by navigating toDOCS | TABLE OF CONTENTS | FuelPHP | Basic | Requirements)
As this book is being written, there are four common ways to download FuelPHP:
These approaches are very well-documented on the website installation instructions page at http://fuelphp.com/docs/installation/instructions.html (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | FuelPHP | Installation | Instructions)
FuelPHP is always evolving and will continue to evolve even after this book is published. As we used FuelPHP 1.7.2 in this book, you might want to install the same version in order to prevent any conflict. You can do this by either downloading the appropriate ZIP file, cloning the 1.7/master branch of the GitHub repository, or using Composer.
This is the simplest solution. You should be able to download it by requesting the URL http://fuelphp.com/files/download/28.
Alternatively, you can access all the compressed packages of important FuelPHP releases at http://fuelphp.com/docs/installation/download.html (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | FuelPHP | Installation | Download)
First, if you didn't do it yet, you need to install Composer. You can find out how by reading the official website at https://getcomposer.org/.
The installation instructions for major operating systems are given in the Getting Started guide. Please note that you can install Composer either globally or locally.
From now on, we will generally assume that you have installed Composer globally. If Composer is installed locally into your working directory, our instructions will work if you replace composer by php composer.phar.
In order to specifically install FuelPHP 1.7, you can simply execute the following command line (replace TARGET by the directory in which you want to download FuelPHP):
If you have downloaded FuelPHP by cloning the GitHub repository, or if you simply want to update FuelPHP and its dependencies, you have to enter the following command line at the location you installed your instance of FuelPHP:
As you can see, Composer is locally installed in the FuelPHP root directory.
Now that you know how to install FuelPHP in a given directory, we will give you the two main ways you can integrate the framework in your environment.
Assuming you have activated the mod_rewrite Apache module, the simplest way is to install FuelPHP in the root folder of your web server (generally the /var/www directory on Linux systems). If you install FuelPHP in the DIR directory of the root folder (/var/www/DIR), you will be able to access your project at the following URL:
http://localhost/DIR/public/
However, be warned that FuelPHP has not been implemented to support this, and if you publish your project this way in the production server, it will introduce security issues you will have to handle. In such cases, you are recommended to use the second way we will explain in the upcoming section, although, for instance if you plan to use a shared host to publish your project, you might not have the choice. A complete and up-to-date documentation about this issue can be found in the FuelPHP installation instruction page at http://fuelphp.com/docs/installation/instructions.html (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | FuelPHP | Installation | Instructions)
Another way is to create a virtual host to access your application. You will need a little bit more Apache and system administration skills, but the benefit is that it is more secure and you will be able to choose your working directory. You will need to change two files:
In both cases, the files' location will be dependent on your operating system and the server environment you are using; therefore, you will have to figure out their location yourself (if you are using a common configuration, you won't have any problem to finding instructions on your preferred search engine).
In the following example, we will set up your system to call your application when requesting the my.app URL on your local environment (*nix system recommended).
Let's first edit the virtual host file(s). Add the following code at the end:
Then, open your system host file and add the following line at the end:
Depending on your environment, you might need to restart Apache after this. You can now access your website at: http://my.app/.
Congratulations! You just have successfully installed the FuelPHP framework. The welcome page shows some recommended directions to continue your project.
Now that we have installed a