Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

CakePHP 1.3 Application Development Cookbook
CakePHP 1.3 Application Development Cookbook
CakePHP 1.3 Application Development Cookbook
Ebook781 pages4 hours

CakePHP 1.3 Application Development Cookbook

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Written in cookbook style, this book offers learning and techniques through recipes. It contains step-by-step instructions for CakePHP developers to create unique web applications. The book is designed in such a way that you can refer to things chapter by chapter, and read them in no particular order. If you are a CakePHP developer who wants to discover quick and easy ways to improve web applications, and to leverage all aspects of the framework, this book is for you. This book assumes that you already have knowledge of CakePHP, and general PHP development skills.
LanguageEnglish
Release dateMar 8, 2011
ISBN9781849511933
CakePHP 1.3 Application Development Cookbook

Related to CakePHP 1.3 Application Development Cookbook

Related ebooks

Computers For You

View More

Related articles

Reviews for CakePHP 1.3 Application Development Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    CakePHP 1.3 Application Development Cookbook - Mariano Iglesias

    Table of Contents

    CakePHP 1.3 Application Development Cookbook

    Credits

    About the Author

    About the Reviewers

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    Why Subscribe?

    Free Access for Packt account holders

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Conventions

    Reader feedback

    Customer support

    Errata

    Piracy

    Questions

    1. Authentication

    Introduction

    Setting up a basic authentication system

    Getting ready

    How to do it...

    How it works...

    Hashing a password confirmation field

    See also

    Using and configuring the Auth component

    Getting ready

    How to do it...

    How it works...

    Changing the default user model

    There's more...

    See also

    Allowing logins with username or e-mail

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Saving the user details after login

    Getting ready

    How to do it...

    How it works...

    See also

    Getting the current user's information

    Getting ready

    How to do it...

    How it works...

    See also

    Using prefixes for role-based access control

    Getting ready

    How to do it...

    How it works...

    See also

    Setting up Access Control Layer-based authentication

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Integrating with OpenID

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    2. Model Bindings

    Introduction

    Adding Containable to all models

    How to do it...

    How it works...

    See also

    Limiting the bindings returned in a find

    Getting ready

    How to do it...

    How it works...

    Format of the contain find parameter

    Reset of binding changes

    See also

    Modifying binding parameters for a find

    Getting ready

    How to do it...

    How it works...

    See also

    Modifying binding conditions for a find

    Getting ready

    How to do it...

    How it works...

    Changing the JOIN type of one-to-one associations

    Getting ready

    How to do it...

    How it works...

    Defining multiple associations to the same model

    Getting ready

    How to do it...

    There's more...

    Adding bindings on the fly

    Getting ready

    How to do it...

    How it works...

    3. Pushing the Search

    Introduction

    Performing GROUP and COUNT queries

    Getting ready

    How to do it...

    How it works...

    See also

    Using virtual fields

    Getting ready

    How to do it...

    How it works...

    See also

    Building queries with ad-hoc JOINs

    Getting ready

    How to do it...

    How it works...

    See also

    Searching for all items that match search terms

    Getting ready

    How to do it...

    How it works...

    See also

    Implementing a custom find type

    Getting ready

    How to do it...

    How it works...

    See also

    Paginating a custom find type

    Getting ready

    How to do it...

    How it works...

    See also

    Implementing AJAX based pagination

    Getting ready

    How to do it...

    How it works...

    4. Validation and Behaviors

    Introduction

    Adding multiple validation rules

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Creating a custom validation rule

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Using callbacks in behaviors

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Using behaviors to add new fields for saving

    Getting ready

    How to do it...

    How it works...

    See also

    Using the Sluggable behavior

    Getting ready

    How to do it...

    How it works...

    Geocoding addresses with the Geocodable behavior

    Getting ready

    How to do it...

    How it works...

    There's more...

    5. Datasources

    Introduction

    Improving the SQL datasource query log

    Getting ready

    How to do it...

    How it works...

    Parsing CSV files with a datasource

    Getting ready

    How to do it...

    How it works...

    There's more...

    Dynamic loading of CSV files

    Consuming RSS feeds with a datasource

    Getting ready

    How to do it...

    How it works...

    There's more...

    Changing connection settings at runtime

    Building a Twitter datasource

    Getting ready

    How to do it...

    How it works...

    Adding transaction and locking support to the MySQL datasource

    Getting ready

    How to do it...

    How it works...

    6. Routing Magic

    Introduction

    Using named and GET parameters

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Using routes with prefixes

    Getting ready

    How to do it...

    How it works...

    See also

    Working with route elements

    Getting ready

    How to do it...

    How it works...

    There's more...

    Using reverse routing

    See also

    Adding catch-all routes for profile pages

    Getting ready

    How to do it...

    How it works...

    See also

    Adding validation for catch-all routes

    Getting ready

    How to do it...

    How it works...

    See also

    Creating custom Route classes

    Getting ready

    How to do it...

    How it works...

    See also

    7. Creating and Consuming Web Services

    Introduction

    Creating an RSS feed

    Getting ready

    How to do it...

    How it works...

    Adding view caching to an RSS feed

    See also

    Consuming a JSON service

    How to do it...

    How it works...

    See also

    Building REST services with JSON

    Getting ready

    How to do it...

    How it works...

    See also

    Adding authentication to REST services

    Getting ready

    How to do it...

    How it works...

    Implementing token-based authorization for API access

    Getting ready

    How to do it...

    How it works...

    8. Working with Shells

    Introduction

    Building and running a shell

    Getting ready

    How to do it...

    How it works...

    Using the Auth component for hashing passwords

    See also

    Parsing command line parameters

    Getting ready

    How to do it...

    How it works...

    See also

    Creating reusable shell tasks

    Getting ready

    How to do it...

    How it works...

    Sending e-mails from shells

    Getting ready

    How to do it...

    How it works...

    See also

    Non-interactive tasks with the robot plugin

    Getting ready

    How to do it...

    How it works...

    9. Internationalizing Applications

    Introduction

    Internationalizing controller and view texts

    Getting ready

    How to do it...

    How it works...

    Domains and categories

    See also

    Internationalizing model validation messages

    Getting ready

    How to do it...

    How it works...

    See also

    Translating strings with dynamic content

    Getting ready

    How to do it...

    How it works...

    Reordering and reusing interpolation arguments

    See also

    Extracting and translating text

    Getting ready

    How to do it...

    How it works...

    Translating database records with the Translate behavior

    Getting ready

    How to do it...

    How it works...

    Using separate translation tables

    See also

    Setting and remembering the language

    Getting ready

    How to do it...

    How it works...

    10. Testing

    Introduction

    Setting up the test framework

    Getting ready

    How to do it...

    How it works...

    Creating fixtures and testing model methods

    Getting ready

    How to do it...

    How it works...

    There's more...

    Extending models to avoid testing unneeded bindings

    Analyzing code coverage

    See also

    Testing controller actions and their views

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Using mocks to test controllers

    Getting ready

    How to do it...

    How it works...

    There's more...

    Running tests from the command line

    Getting ready

    How to do it...

    How it works...

    11. Utility Classes and Tools

    Introduction

    Working with the Set class

    Getting ready

    How to do it...

    How it works...

    There's more...

    Manipulating strings with the String class

    Getting ready

    How to do it...

    How it works...

    Sending an e-mail

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Detecting file types with MagicDb

    Getting ready

    How to do it...

    How it works...

    Throwing and handling exceptions

    Getting ready

    How to do it...

    How it works...

    Index

    CakePHP 1.3 Application Development Cookbook


    CakePHP 1.3 Application Development Cookbook

    Copyright © 2011 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: March 2011

    Production Reference: 1030311

    Published by Packt Publishing Ltd.

    32 Lincoln Road

    Olton

    Birmingham, B27 6PA, UK.

    ISBN 978-1-849511-92-6

    www.packtpub.com

    Cover Image by Javier Barria (<[email protected]> )

    Credits

    Author

    Mariano Iglesias

    Reviewers

    Nick Baker

    Sam Sherlock

    Jeff Smith

    Mark Story

    Acquisition Editor

    Sarah Cullington

    Development Editor

    Reshma Sundaresan

    Technical Editor

    Gauri Iyer

    Indexer

    Hemangini Bari

    Editorial Team Leader

    Mithun Sehgal

    Project Team Leader

    Lata Basantani

    Project Coordinator

    Leena Purkait

    Proofreader

    Glinert Stevens

    Graphics

    Nilesh R. Mohite

    Production Coordinator

    Adline Swetha Jesuthas

    Cover Work

    Adline Swetha Jesuthas

    About the Author

    Mariano Iglesias started programming at the early age of 11, moving through a variety of programming languages, and becoming a proficient PHP developer of over 10 years.

    Throughout the years, he collaborated on several open source projects, mostly in C++, Java, Python, and PHP. He became a CakePHP and a Lithium contributor, frameworks that are among the most popular web development frameworks.

    He divides his time between client work and his current open source projects, which are built with C++, Python, and PHP.

    There are no words that can express my gratitude for the CakePHP community, and the CakePHP core team. Their passion and commitment to build one of the best PHP frameworks is to be cherished. Their sincere dedication to sharing knowledge cannot be praised enough.

    This book has also benefited from the best team of technical reviewers a CakePHP book could possibly have. Here is my aknowledgement for Jeffrey Smith, John Anderson, Mark Story, Nick Baker, Paul Gardner, and Sam Sherlock. You not only helped me find flaws in my code, but also suggested some impressive improvements. The CakePHP community should be proud to count you as members.

    Everyone on the Packt Publishing team has not only accommodated my schedule, but helped me throughout the writing of this book. They are truly a joy to work with, and no writer should walk away from the opportunity of writing for Packt. Thank you guys for your support and trust.

    About the Reviewers

    Nick Baker, owner of WebTechNick LLC and Senior IT/Developer at healthyhearing.com. Nick is a gifted problem solver who sincerely enjoys all aspects of programming and design, from development to implementation - excelling at programming, testing, documentation, and training in multiple platforms. Nick has worked over 13 years focusing on web development and is an expert in multiple web 2.0 frameworks such as CakePHP and Ruby on Rails. Prior to working with Allied Health LLC, Nick studied Computer Science at the University of New Mexico and helped build the Natural Heritage of New Mexico website - a site used by research scientists to store/retrieve data and map the distribution of endangered plants and animals in the Southwest. UNM holds a patent for multi-layer vector searches in GIS as a direct result of Nick's work.

    Sam Sherlock is a web developer who is inspired by the innovations of modern web technology and loves to see them put to use in creative ways that benefit the masses. He has been a keen, early adopter of many popular Open source projects (jQuery, SWFObject. and of course, CakePHP).

    He is a co-creator of a CakePHP plugin named BakingPlate, soon to be available via Github. One day he hopes to use modern web technologies to create mathematical teaching guides that are accessible and enjoyable to all.

    Jeff Smith grew up in a small town in Indiana named Odon, but has lived in Indianapolis, Indiana (USA) for the last 15 years. Web programming is his hobby, but he plans to make it his career later. He enjoys working with many established frameworks, as well as trying out new ones. He is currently working with Drupal, Joomla!, and Ruby on Rails for various small projects. He is looking forward to learning about Non-SQL databases, Lift, and Scala.

    I would like to thank Packt for giving me an opportunity to help out on this book, the Object-oriented PHP book I am working on, and I look forward to helping out on any books I can in the future.

    Mark Story graduated from the Ontario College of Art and Design with a degree in Illustration. Art being a difficult industry to break into, he tried his hand at programming. This grew into a skill and passion that allowed him to become the lead developer of the CakePHP framework and secure a position with FreshBooks as a front-end developer. Mark's design background and skills in development produce a unique combination of attention to detail, technical expertise, and, above all, patience. Leading the CakePHP project as the primary contributor for the past two years, Mark contributes to several open-source projects in his spare time.

    I'd like to thank my wonderful wife for putting up with all the late nights and my incessant talking about things she doesn't really understand. I'd also like to thank the fantastic CakePHP community CakePHP without you, this book would never have been possible.

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    You might want to visit www.PacktPub.com for support files and downloads related to your book.

    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 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.

    http://PacktLib.PacktPub.com

    Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.

    Why Subscribe?

    Fully searchable across every book published by Packt

    Copy & paste, print and bookmark content

    On demand and accessible via web browser

    Free Access for Packt account holders

    If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

    I dedicate this book to:

    My parents, who have embraced the passion of a restless kid when he decided to become a programmer, and taught him that family is the most important thing.

    My brothers (Lorena, Nicolas, Pedro, Ignacio) for accomodating the kid that would not play with them so he could spend countless hours on the computer (or in a notebook when a computer wasn't available). Pedro: thank you for teaching me what life is all about, I miss you every day.

    My wife Claudia, who is my soulmate and a partner for life; who has taught me the meaning of love; whose happiness has become my most important objective; and without whom, nothing, nothing at all would be possible. You are the reason why I am, my dear princess.

    Preface

    CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying web applications. While the framework has a lot of documentation and reference guides available for beginners, developing more sophisticated and scalable applications require a deeper knowledge of CakePHP features, a challenge that proves difficult even for well established developers.

    The recipes in this cookbook will give you instant results and will help you to develop web applications, leveraging the CakePHP features that allow you to build robust and complex applications. Following the recipes in this book (which show how to work with AJAX, datasources, GEO location, routing, performance optimization, and more), you will be able to understand and use these features in no time.

    What this book covers

    Chapter 1, Authentication: This chapter explains how to set up authentication on a CakePHP application, starting from the most basic setup and finishing with advanced authorization mechanisms. This is accomplished through the use of tools that are built into the framework core, which allow us to quickly set up secure areas, without losing flexibility to build more complex solutions.

    The first two recipes show us how to set up a basic, yet fully working authentication system. The next three recipes allow our users to log in using different information, have their user details saved after a successful login, and show us how to get this user information. The sixth recipe shows a more complex authorization technique that relies on route prefixes. The seventh recipe sets up a complex authentication system through the use of CakePHP's Access Control Layer. Finally, the last recipe shows us how to integrate our application with OpenID.

    Chapter 2, Model Bindings: This chapter deals with one of the most important aspects of a CakePHP application: the relationship between models, also known as model bindings or associations. Being an integral part of any application's logic, it is of crucial importance that we master all aspects of how model bindings can be manipulated to get the data we need, when we need it.

    In order to do so, we will go through a series of recipes that will show us how to change the way bindings are fetched, what bindings and what information from a binding is returned, how to create new bindings, and how to build hierarchical data structures

    Chapter 3, Pushing the Search: Using models to fetch data is one of the most important aspects of any CakePHP application. As such, a good use of the find functions the framework provides can certainly guarantee the success of our application, and as importantly ensure that our code is readable and maintainable.

    In this chapter, we have several recipes to resort to manual SQL-based queries when the need arises.

    CakePHP also lets us define our custom find types that will extend the basic ones, allowing our code to be even more readable. The last recipes in this chapter show us how to add pagination support to our find type.

    Chapter 4, Validation and Behaviors: This chapter deals with two aspects of CakePHP models that are fundamental to most applications: validation and behaviors.

    When we are saving information to a data source (such as a database), CakePHP will automatically ensure that the data is quoted in order to prevent attacks, SQL injection being the most common one. If we also need to ensure that the data follows a certain format (for example, that a phone number is valid), we use validation rules.

    There are also times where we need to do more than just validate the data we are working with. In some cases, we need to set values for fields that the end user can't specify but are part of our application logic. CakePHP's behaviors allow us to extend the functionality provided by a model, using callbacks to manipulate the data before it's saved, or after it's fetched.

    The third recipe shows us how to use model callbacks (such as beforeFind and afterFind) in behaviors, while the fourth recipe shows how to use behaviors to add additional field values when a save operation is being undertaken.

    The last two recipes in this chapter give examples on how to use the Sluggable behavior (for creating SEO friendly URLs), and the Geocodable behavior (to add geocoding support to an Address model).

    Chapter 5, Datasources: Datasources are the backbone of almost all model operations. They provide an abstraction between model logic and the underlying data layer, allowing a more flexible approach to data manipulation. Through this abstraction, CakePHP applications are able to manipulate data without knowing the specifics of how it's stored, or fetched.

    This chapter shows how to get information from existing datasources, use pre-built datasources to deal with non-relational data, and teaches us how to create a full-featured twitter datasource.

    Chapter 6, Routing Magic: Almost every web-based application will eventually have to develop a successful strategy to obtain better search engine position through a technique known as search engine optimization.

    This chapter starts by introducing some basic concepts of routing through the use of route parameters, and continues to build optimized routes to leverage our search engine placement.

    The final section in this chapter shows us how to create highly optimized URLs for our user profiles, and how to build custom Route classes to obtain even more flexibility.

    Chapter 7, Creating and Consuming Web Services: Web services are essential when looking forward to expose application functionality to third-party applications, or when looking forward to integrate foreign services into our own applications. They offer a broad set of technologies and definitions so systems written in different programming languages can communicate.

    This chapter introduces a set of recipes to consume web services and to expose parts of our application as web services.

    Chapter 8, Working with Shells: One of the most powerful, yet unknown, features of CakePHP is its shell framework. It provides applications with all that is required for building command line tools, which can be used to perform intensive tasks and any other type of non interactive processing.

    This chapter introduces the reader to CakePHP shells by starting with the process of building basic shells, and then moving on to more advanced features, such as sending e-mails, and running controller actions from shells. It finishes by presenting the robot plugin, which offers a fully featured solution for scheduling and running tasks.

    Chapter 9, Internationalizing Applications: This chapter includes a set of recipes that allow the reader to internationalize all aspects of their CakePHP applications, including static content (such as those available in views), and dynamic content (such as database records).

    The first two recipes

    Enjoying the preview?
    Page 1 of 1