20,73 €
CodeIgniter is an open source PHP framework with a small footprint and exceptional performance. It gives you a rich set of libraries for common tasks, with a simple interface to access them. There are several unexplored aspects of CodeIgniter that can help developers build applications more easily and quickly. In this book, you will learn the intricacies of the framework and explore some of its hidden gems.
If you want to get the most out of CodeIgniter, this book is for you. It teaches you what you need to know to use CodeIgniter on a daily basis. You will create mini-applications that teach a specific technique and let you build on top of the base.
This book will take you through developing applications with CodeIgniter. You will learn how to make your CodeIgniter application more secure than a default installation, how to build large-scale applications and web services, how to release code to the community, and much more. You will be able to authenticate users, validate forms, and also build libraries to complete different tasks and functions.
The book starts off introducing the framework and how to install it on your web server or a local machine. You are introduced to the Model-View-Controller design pattern and how it will affect your development. Some important parts of the CodeIgniter Style Guide are included to keep CodeIgniter development as standardized as possible; this helps greatly when working as part of a team or taking on an old CodeIgniter project. You will quickly move on to how CodeIgniter URLs work and learn about CodeIgniter-specific files such as helpers and plugins. By the time you finish this book, you will be able to create a CodeIgniter application of any size with confidence, ease, and speed.
This guide will enable you to become well-versed with CodeIgniter through practical applications using the tools and techniques used by many seasoned CodeIgniter developers.
This book is a practical guide that takes you through a number of techniques. Each chapter builds upon knowledge from the previous chapter. Step-by-step instructions with examples and illustrative screenshots ensure that you gain a firm grasp of the topic being explained.
This book is written for advanced PHP developers with a good working knowledge of Object Oriented Programming techniques who are comfortable with developing applications and wish to use CodeIgniter to make their development easier, quicker, and more fun. Basic knowledge of CodeIgniter will be helpful. This book will suit developers who fall into three categories:
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 302
Veröffentlichungsjahr: 2010
Copyright © 2010 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: April 2010
Production Reference: 1190410
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849510-90-5
www.packtpub.com
Cover Image by Filippo (<[email protected]>)
Author
Adam Griffiths
Reviewers
Jose Argudo
Saidur Rahman
Acquisition Editor
Dilip Venkatesh
Development Editor
Dilip Venkatesh
Technical Editor
Aaron Rosario
Indexer
Monica Ajmera Mehta
Editorial Team Leader
Akshara Aware
Project Team Leader
Lata Basantani
Project Coordinator
Joel Goveya
Proofreader
Dirk Manuel
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
Adam Griffiths is a student and freelance CodeIgniter Developer based in the United Kingdom. He has five years of web development experience, the last two being largely influenced by CodeIgniter. He has worked on many websites, both large and small, from small blogs to large websites for multi-national corporate companies. He is well versed in development techniques and how to squeeze that little bit more from an application. He has also made a number of contributions to the CodeIgniter Community, most notably The Authentication Library, a very simple to use but full-featured Authentication Library for CodeIgniter.
When CodeIgniter and PHP aren't spiralling around his head, Adam enjoys practising card and mentalism tricks, mainly sleight of hand and card handling tricks. He has performed at local and formal functions for hundreds of people. He is also a guitar player and enjoys playing acoustically at pubs and small gigs. Moving back towards computing, he has a deep interest in Cryptography. He loves finding patterns in data and loves using pen and paper to decipher any cipher text he may find around the web. Find out more and read his blog at http://www.adamgriffiths.co.uk.
I would like to thank my parents for encouraging me to better myself. If it weren't for them I may not have written this book. I would also like to thank my friends for letting me bounce ideas off of them and develop these ideas. I've wanted to write a book for a while now, so a big thank you goes to Packt Publishing for giving me this opportunity; and to everybody involved in reviewing, editing, and managing the book as a whole.
To the readers—Thank you for purchasing this book. It means a great deal to me that you will be reading the content that I spent a lot of time on, and you will hopefully learn a lot from it.
JoseArgudo is a web developer from Valencia, Spain. After finishing his studies he started working for a web design company. Six years later, he decided to start working as a freelancer.
Now that some years have passed as a freelancer, he thinks it's the best decision he has ever taken—a decision that let him work with the tools he likes, such as Joomla!, Codeigniter, Cakephp, Jquery, and other well-known open source technologies.
His desire to learn and share his knowledge has led him to be a regular reviewer of books from Packt, such as Joomla! With Flash, Joomla! 1.5 SEO, Magento Theme Design or Symfony 1.3 web application development.
Recently he has even published his own book, Codeigniter 1.7, which you can also find at Packt's site. If you work with PHP, take a look at it!
If you want to know more about him, you can check out his site at www.joseargudo.com
To my girlfriend and to my brother, I wish them the best.
Saidur Rahman Bijon is an open source enthusiast from Bangladesh. He graduated in computer science in from BRAC university and has been developing web applications for over four years. In this time, he has developed ecommerce, web 2.0, social networking, and microblogging applications. He shares his knowledge and ideas at http://saidur.wordpress.com.
He started his career by developing a large scale application for the Bangladesh Navy. Since then, he has worked mainly for Japanese and USA based outsourcing companies, where he has built applications in CodeIgniter. Currently, he is working for a USA based company, Blueliner Bangla (http://www.bluelinerny.com/) as a senior software engineer.
I'd like to thank Packt for giving me the opportunity to review this book.
I enjoyed it thoroughly.
I'm really thankful for my family, friends, and colleagues for their help and support.
I dedicate this book to my family.
This book takes you beyond the CodeIgniter user guide and into more advanced subjects that you need to know if you plan to use CodeIgniter on a daily basis. The book will teach you how to build libraries in order to complete different tasks and functions. You will create mini-applications each of which teaches a specific technique and builds on top of the CodeIgniter base. By the time that you finish this book you will be able to create a CodeIgniter application of any size with confidence, ease, and speed.
Chapter 1, Getting Started with CodeIgniter—This chapter guides you from installing CodeIgniter to learning about its URL structure, the MVC design pattern, helpers, plugins, and extending and replacing libraries.
Chapter 2, Learning the Libraries—You are taken through a number of the core CodeIgniter libraries, being introduced to each library, what it does and how you go about using it. Some libraries work together, and if this is the case, then this chapter explains how they can be used together.
Chapter 3, Form Validation and Database Interaction—Form validation is a task that some users find difficult. This chapter focuses on teaching you the correct way to validate your forms, by using the Form Validation library. You will also cover the Database library and Database Forge, a way to easily manage database tables.
Chapter 4, User Authentication 1—The first User Authentication chapter focuses on building your own authentication system. We build a model that handles the registration and logging in of users. We also include a function to check whether a user is logged in or not.
Chapter 5, User Authentication 2—The second and final User Authentication chapter focuses solely on user log-in in through Twitter oAuth and Facebook Connect. For each example the CodeIgniter code is explained, as well as how both company's APIs work, and to some extent how each differs from the other.
Chapter 6, Application Security—This chapter discusses how CodeIgniter is secure by design, for example, disallowing certain characters in the URI strings. We also go over what you can do to make your CodeIgniter application more secure than a default installation.
Chapter 7, Building a Large-Scale Application—This chapter takes you through some of the techniques that you can use to make your CodeIgniter application scalable. You learn about identifying bottlenecks via benchmarking results, caching, using better PHP functions, optimizing SQL queries, and using memcache and multiple application instances.
Chapter 8, Web Services—This chapter builds an example web service by using the REST principle. This includes a simple client library that issues requests, and a server library that deals with requests and responds as appropriate.
Chapter 9, Extending CodeIgniter—This chapter covers everything you need to know about extending CodeIgniter's default functionality without hacking at the core files. This is very useful, especially when it is time to upgrade to the newest version of CodeIgniter.
Chapter 10, Developing and Releasing Code to the Community—In this chapter you will learn how to release code to the community, gain exposure for your released code, and how to properly maintain the code and give good support to those using your code.
This book is written for advanced PHP developers with a good working knowledge of Object Oriented Programming techniques who are comfortable with developing applications and wish to use CodeIgniter to make their development easier, quicker and more fun. Basic knowledge of CodeIgniter will be helpful. This book will suit developers who fall into three categories:
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "We can include other contexts through the use of the include directive."
A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking on the Next button moves you to the next screen".
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 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 in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note via the SUGGEST A TITLE form on www.packtpub.com, or send an e-mail to <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, 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.
Downloading the example code for the book
Visit http://www.packtpub.com/files/code/0905_Code.zip to directly download the example code.
The downloadable files contain instructions on how to use them.
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 to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know 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.
CodeIgniter is an open source web application framework for the PHP language. CodeIgniter has many features that make it stand out from the crowd. Unlike some other PHP frameworks you may have come across, the documentation is very thorough and complete—covering every aspect of the framework. CodeIgniter will also run in shared hosting environments as it has a very low footprint, yet it still has exceptional performance.
On the programming side, CodeIgniter is both PHP4 and PHP5 compatible, so it will run on the majority of web hosts out there. It also uses the Model View Controller (MVC) design pattern, which is a way to organize your application into three different parts: models—the database abstraction layer, views—the front end template files, and controllers—the business logic of your applications. In the core, CodeIgniter also makes extensive use of the Singleton design pattern. This is a way to load classes so that if they are called multiple times, the same instance of the class will be returned. This is highly useful for database connections, as you would only want one connection each time that the class is used.
CodeIgniter also has an implementation of the Active Record pattern. This makes it easy to write complex SQL queries and makes your application more readable. Active Record also allows you to easily swap and change database drivers. This allows you to write the queries in PHP and still use a MySQL backend, and also gives you the option to switch to an Oracle database –without having to rewrite the queries in your application.
CodeIgniter also comes with a number of highly useful libraries and other sets of functions that help you to build your applications. This leaves you to focus on the small part of your application that is unique, rather than the part that is used across all projects, such as database queries and parsing data.
In this chapter, we will:
Users on older versions of CodeIgniter should upgrade to the latest version for a number of reasons.
Firstly, each new release comes with many bug fixes, and more importantly, security patches. This makes applications running on older versions of CodeIgniter more vulnerable to attack than the newer versions.
There are also more features. For example, a new Cart Library was introduced in CodeIgniter 1.7, allowing users to build a simple shopping cart application easily, using a native supported library rather than a third-party one.
To upgrade to CodeIgniter 1.7, follow the instructions given next and simply migrate your application folder over to the newer codebase.
This book assumes prior knowledge of PHP—this should also stretch to a web server. CodeIgniter needs a web server to run on, just like any other PHP application. You can install CodeIgniter locally just for testing, or use it on your current web server; anywhere will suffice.
The first step to getting started with CodeIgniter is to download the files from the website. The website is located at www.codeigniter.com. This website includes a backlog of all of the previous versions of CodeIgniter, as well as a Subversion Repository (SVN) for the latest version. In our case, we can simply download the latest version straight from the home page—currently 1.7.2— by clicking on the Download CodeIgniter button, as seen in the next screenshot.
When you have downloaded the ZIP file, unzip it using your favourite file archiving program. You will now have a folder named CodeIgniter_1.7.2. The folder should contain two files and two directories, as seen in the next screenshot.
The system directory holds the core CodeIgniter files, libraries, and other CI specific stuff, such as the logs and cache directories. It also houses the application folder—this is the only folder you truly need to worry about, as this is the only place where you need to put your files. As you can guess, this is the folder where all your application-specific code goes, and includes the configuration files that you may need to edit.
Simply open this folder in your code editor of choice and we'll go ahead and install CodeIgniter in our final step. We need to edit the base URL—the URL at which you will you access your application—and to do this we need to open the file /system/application/config/config.php. The value that we need to change is the array item $config['base_url'] which is currently set to http://example.com/—simply change this to your URL.
The base URL value needs to have a trailing slash (a forward slash) at the end.
When that is done, navigate to your base URL and you should see the CodeIgniter welcome screen, as seen in the next screenshot.
Although you have heard this term mentioned in this book already, you may not know what the term means. In short, Model View Controller—from now on, referred to as MVC—is a software development design pattern. MVC is an approach to separating your applications into three segments: Models, Views, and Controllers. MVC structures your application in this way in order to promote the reuse of program code.
The Model represents any type of data that your application may use. Some examples of data that your application might use would be: a database, RSS Feeds, API calls, and any other action that involves retrieving, returning, updating, and removing data.
Views are the information that is being presented on the screen to users through their web browsers. These are usually HTML files, sometimes containing PHP code that builds the template for the website. In CodeIgniter however, views can be page segments, partial templates, or any other type of page or template.
Finally, Controllers are the business logic of your application. They serve as an intermediary between Models and Views. The Controller will respond to HTTP requests and generate web pages.
However, CodeIgniter's approach to MVC is very loose. This means that Models are not required. This is for a number of reasons. Firstly, CodeIgniter's Database Library can be used in both Models and Controllers—meaning that the extra separation of Models can be bypassed. Secondly, the Model isn't tied to the database, as it is in other frameworks such as Ruby on Rails, so the Model isn't needed in this regard. Finally, if using a Model in your application will cause unnecessary complexity, then the Model can simply be ignored.
However, Models are extremely useful, even though they are optional. Models can be loaded from any Controller, so if you use a Model function in multiple controllers and you need to change the function, you only need to edit it in one place rather than in all of the controllers. Complex queries should really be put into a Model. A collection of related queries should also be put into a Model as they can be grouped together. This makes your applications simpler, and it allows you to use the functions in any Controller.
