/  37
 
Cook up Web sites fast with CakePHP, Part 2:Bake bigger and better with CakePHP
Skill Level: IntermediateDuane O'Brien(d@duaneobrien.com) PHP developerFreelance12 Dec 2006CakePHP is a stable production-ready, rapid-development aid for building Web sitesin PHP. This"Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP.
Section 1. Before you start
This series is designed for PHP application developers who want to start usingCakePHP to make their lives easier. In the end, you will have learned how to installand configure CakePHP, the basics of Model-View-Controller (MVC) design, how tovalidate user data in CakePHP, how to use CakePHP helpers, and how to get anapplication up and running quickly using CakePHP. It might sound like a lot to learn,but don't worry -- CakePHP does most of it for you.
About this series
Part 1focuses on getting CakePHP up and running, and the basics ofhow to put together a simple application allowing users to register for anaccount and log in to the application.Part 2 demonstrates how to use scaffolding and Bake to get a jump starton your application, and using CakePHP's access control lists (ACLs).Part 3shows how to use Sanitize, a handy CakePHP class, which helpssecure an application by cleaning up user-submitted data. Part 3 alsocovers the CakePHP security component, handling invalid requests andother advanced request authentication.
Bake bigger and better with CakePHP © Copyright IBM Corporation 1994, 2006. All rights reserved.Page 1 of 37
 
Part 4focuses primarily on the Session component of CakePHP,demonstrating three ways to save session data, as well as the RequestHandler component to help you manage multiple types of requests(mobile browsers, requests containing XML or HTML, etc).AndPart 5deals with caching, specifically view and layout caching, whichcan help reduce server resource consumption and speed up yourapplication.
About this tutorial
This tutorial shows you how to jumpstart your CakePHP application using scaffoldingand Bake. You will also learn the ins and outs of using CakePHP's ACLs. You'll geta look at what scaffolding is and what it provides. Then you'll learn how to use Baketo generate the code for a scaffold, letting you tweak it as you go. Finally, you willlearn about ACLs: what they are, how to create them, and how to use them in yourapplication. This tutorial builds on the online product application
Tor 
created inPart1.
Prerequisites
It is assumed that you are familiar with the PHP programming language, have afundamental grasp of database design, and are comfortable getting your hands dirty.A full grasp of the MVC design pattern is not necessary, as the fundamentals will becovered during this tutorial. More than anything, you should be eager to learn, readyto jump in, and anxious to speed up your development time.
System requirements
Before you begin, you need to have an environment in which you can work.CakePHP has reasonably minimal server requirements:1. An HTTP server that supports sessions (and preferably
mod_rewrite
).This tutorial was written using Apache V1.3 with
mod_rewrite
enabled.2. PHP V4.3.2 or later (including PHP V5). This tutorial was written usingPHP V5.0.43. A supported database engine (currently MySQL, PostgreSQL or using awrapper around ADODB). This tutorial was written using MySQL V4.1.15.You'll also need a database ready for your application to use. The tutorial willprovide syntax for creating any necessary tables in MySQL.The simplest way to download CakePHP is to visitCakeForge.organd download thelatest stable version. This tutorial was written using V1.1.8. (Nightly builds and
developerWorks® ibm.com/developerWorksBake bigger and better with CakePHPPage 2 of 37© Copyright IBM Corporation 1994, 2006. All rights reserved.
 
copies straight from Subversion are also available. Details are in the CakePHPManual (seeResources).)
Section 2. Tor, so far
At the end ofPart 1, you were given an opportunity to put your skills to work bybuilding some missing functionality for Tor. Login/Logout, index, the use of hashedpasswords, and automatically logging a registering user were all on the to-do list.How did you do?
The login view
Your login view might look something like Listing 1.
Listing 1. Login view
<?phpif ($error){e('Invalid Login.');}?>Please log in.</p><?php echo $html->form('/users/login') ?><label>Username:</label><?php echo $html->input('User/username', array) ?><label>Password:</label><?php echo $html->password('User/password', array) ?><?php echo $html->submit('login') ?></form><?php echo $html->link('register', '/users/register') ?>
Your index view might look something like Listing 2.
Listing 2. Index view
<p>Hello, <?php e($User['first_name'] . ' ' . $User['last_name']) ?></p><p>Your last login was on <?php e($last_login) ?></p><?php echo $html->link('knownusers', '/users/knownusers') ?> <?php echo$html->link('logout', '/users/logout') ?>
Both of the views should look pretty straightforward. The index view just checks thesession for the user's username and, if it's not set, sends him to log in. The loginview doesn't set a specific error message, so someone trying to guess their way intothe system doesn't know which parts are correct.
ibm.com/developerWorks developerWorks® Bake bigger and better with CakePHP © Copyright IBM Corporation 1994, 2006. All rights reserved.Page 3 of 37

Share & Embed

More from this user

Recent Readcasters

Add a Comment

Characters: ...

alcooltanleft a comment

i dont like scribd. it's so clunky. sorry ...

leleuleft a comment

I notice in the ACL section, WRT the "redirect" methods (when you try to access the page w/o being logged in) there is a bug. When you set redirect with an action (look at the line $this->redirect( array('action'=>'index'), null, true)) that you do not leave the Products controller (i.e. you get taken to the products index, rather than the login page). I believe the line should be: $this->redirect( '/users/index'); This will take you to the login page. You could set a variable to return you to the requested product page (from the original click, pre-login). Then you would have to modify the login controller, so that if $redir is set, you go there; otherwise goto the default page after login. As much as I love this series, anyone else think it could use another review? I noticed several of these little bugs in it. Maybe I'll write the author...