Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
CakePHP 1.3 Application Development Cookbook

You're reading from  CakePHP 1.3 Application Development Cookbook

Product type Book
Published in Mar 2011
Publisher Packt
ISBN-13 9781849511926
Pages 360 pages
Edition 1st Edition
Languages
Toc

Table of Contents (17) Chapters close

CakePHP 1.3 Application Development Cookbook
Credits
About the Author
About the Reviewers
1. www.PacktPub.com
2. Preface
1. Authentication 2. Model Bindings 3. Pushing the Search 4. Validation and Behaviors 5. Datasources 6. Routing Magic 7. Creating and Consuming Web Services 8. Working with Shells 9. Internationalizing Applications 10. Testing 11. Utility Classes and Tools

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 show how to allow text that is part of any CakePHP view, or model validation messages, to be ready for translation. The third recipe shows how to translate more complex expressions. The fourth recipe shows how to run CakePHP's built in tools to extract all static content that needs translation, and then translate that content to different languages. The fifth recipe shows how to translate database records. Finally, the last recipe shows how to allow users to change the current application language.

Chapter 10, Testing: This chapter covers one of the most interesting areas of application programming: unit testing through CakePHP's built-in tools, which offers a complete and powerful unit testing framework.

The first recipe shows how to set up the test framework so that we can create our own test cases. The second recipe shows how to create test data (fixtures) and use that data to test model methods. The third and fourth recipes show how to test controller actions, and how to test that our views are showing what we expect. The last recipe shows how to run the test in a non-ordinary fashion.

Chapter 11, Utility Classes and Tools: This chapter introduces a set of utility classes and helpful techniques that improve the architecture of a CakePHP application.

The first recipe shows how to work with a CakePHP class that optimizes the manipulation of arrays. The second recipe shows how to send an e-mail using the Email component. The third recipe shows how to use the MagicDb class to detect the type of a file, and the last recipe shows how to create application exceptions, and properly handle them when they are thrown.

What you need for this book

We need the following software for the book:

  • A web server supported by CakePHP (such as Apache)

  • A database engine supported by CakePHP (such as MySQL)

  • CakePHP installed, configured, and working properly

Who this book is for

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.

Conventions

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: "Create a file named query_log.php and place it in your app/controllers/components folder with the following contents:"

A block of code is set as follows:

CREATE TABLE `accounts`(
`id` INT UNSIGNED AUTO_INCREMENT NOT NULL,
`email` VARCHAR(255) NOT NULL,
PRIMARY KEY(`id`)
);

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: "In that screen, make sure to grab what is shown as Consumer key and Consumer secret, as we will need it when going through this recipe."

Note

Warnings or important notes appear in a box like this.

Note

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 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 , and mention the book title via 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 in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

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.

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.

Note

Downloading the example code for this book

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.

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 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/support, 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 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

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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}