



Customizing Scaffold Views
If you’re looking for something a little different in your scaffolded views, you can create them yourself. We still don’t recommend using this technique for production applications, but such a customization may be extremely useful for prototyping iterations.
If you’d like to change your scaffolding views, you’ll need to supply your own:
Example 5.1. Custom Scaffolding Views for a Single Controller
Custom scaffolding views for a PostsController should be placed like so: /app/views/posts/scaffold/index.scaffold.thtml /app/views/posts/scaffold/show.scaffold.thtml /app/views/posts/scaffold/edit.scaffold.thtml /app/views/posts/scaffold/new.scaffold.thtml
Example 5.2. Custom Scaffolding Views for an Entire Application
Custom scaffolding views for all controllers should be placed like so: /app/views/scaffold/index.scaffold.thtml /app/views/scaffold/show.scaffold.thtml /app/views/scaffold/edit.scaffold.thtml /app/views/scaffold/new.scaffold.thtml
If you find yourself wanting to change the controller logic at this point, it’s time to take the scaffolding down from your application and start building it.
One feature you might find helpful is Cake’s code generator: Bake. Bake allows you to generate a coded version of scaffolded code you can then move on to modify and customize as your application requires.




So cool that you’ll want to use it in production apps. Now, we think its cool, too, but please realize that scaffolding is… well… just scaffolding. It’s a bunch of stuff you throw up real quick during the beginning of a project in order to get started. It isn’t meant to be completely flexible. So, if you find yourself really wanting to customize your logic and your views, its time to pull your scaffolding down in order to write some code.
Scaffolding is a great way of getting the early parts of developing a web application started. Early database schemas are volatile and subject to change, which is perfectly normal in the early part of the design process. This has a downside: a web developer hates creating forms that never will see real use. To reduce the strain on the developer, scaffolding has been included in Cake. Scaffolding analyzes your database tables and creates standard lists with add, delete and edit buttons, standard forms for editing and standard views for inspecting a single item in the database. To add scaffolding to your application, in the controller, add the $scaffold variable:
<?php
class CategoriesController extends AppController
{
var $scaffold;
}
?>
One important thing to note about scaffold: it expects that any field name that ends with _id is a foreign key to a table which has a name that precedes the underscore. So, for example, if you have nested categories, you’d probably have a column called parent_id. With this release, it would be best to call this parentid. Also, when you have a foreign key in your table (e.g. titles table has category_id), and you have associated your models appropriately (see Understanding Associations, 6.2), a select box will be automatically populated with the rows from the foreign table (category) in the show/edit/new views. To set which field in the foreign table is shown, set the $displayField variable in the foreign model. To continue our example of a category having a title:
<?php
class Title extends AppModel
{
var $name = 'Title';
var $displayField = 'title';
}
?>




There are some settings in /app/config/core.php you can take advantage of in order to organize your application and craft URLs that make the most sense to you and your users.
The first of these is admin routing. If your application has a ProductsController as well as a NewsController, you might want to set up some special URLs so users with administrative privileges can access special actions in those controllers. To keep the URLs nice and easy to read, some people prefer /admin/products/add and /admin/news/post to something like /products/adminAdd and /news/adminPost.
To enable this, first, uncomment the CAKE_ADMIN line in your /app/config/core.php file. The default value of CAKE_ADMIN is ‘admin’, but you can change it to whatever you like. Remember this string, because you’ll need to prepend it to your administrative actions in your controller. So, admin actions in this case would be named admin_actionName(). Here’s some examples of desired URLs and possible CAKE_ADMIN and controller action settings:
/admin/products/add CAKE_ADMIN = 'admin'
name of action in ProductsController = 'admin_add()'
/superuser/news/post CAKE_ADMIN = 'superuser'
name of action in NewsController = 'superuser_post()'
/admin/posts/delete CAKE_ADMIN = 'admin'
name of action in PostsController = 'admin_delete()'
Using admin routes allows you to keep your logic organized while making the routing very easy to accomplish.
![]() |
Note |
|---|---|
| Please note that enabling admin routes or using them does not enable any sort of authentication or security. You’ll need implement those yourself. |
Similarly, you can enable Cake’s webservices routing to make easier there as well. Have a controller action you’d like to expose as a webservice? First, set WEBSERVICES in /app/config/core.php to ‘on’. This enables some automatic routing somewhat similar to admin routing, except that a certain set of route prefixes are enabled:
What this does is allows you to provide an alternate views that will automatically be available at /rss/controllerName/actionName or /soap/controllerName/actionName. This allows you to create a single action that can have two views: one for normal HTML viewiers, and another for webservices users. By doing this, you can easily allow much of the functionality of your application to be available via webservices.
For example, let’s say I have some logic in my application that tells users who is on the phone in my office. I already have a HTML view for this data, but I want to offer it in XML so it can be used in a desktop widget or handheld application. First I need to enable Cake’s webservice routing:
Example 4.6. /app/config/core.php (partial)
/**
* The define below is used to turn cake built webservices
* on or off. Default setting is off.
*/
define('WEBSERVICES', 'on');
Next, I can structure the logic in my controller just as I normally would:
Example 4.7. messages_controller.php
<?php
class PhonesController extends AppController
{
function doWhosOnline()
{
// this action is where we do all the work of seeing who's on the phone...
// If I wanted this action to be available via Cake's xml webservices route,
// I'd need to include a view at /app/views/posts/xml/do_whos_online.thtml.
// Note: the default view used here is at /app/views/layouts/xml/default.thtml.
// If a user requests /phones/doWhosOnline, they will get an HTML version.
// If a user requests /xml/phones/doWhosOnline, they will get the XML version.
}
}
?>
(Optional) Custom Inflections Configuration
Cake's naming conventions can be really nice - you can name your model Box,
your controller Boxes, and everything just works out. There are occasions
(especially for our non-english speaking friends) where you may run into
situations where Cake's inflector (the class that pluralizes, singularizes,
camelCases, and under_scores) might not work as you'd like. If Cake won't
recognize your Foci or Fish, editing the custom inflections configuration file
is where you'll need to go.
Found at /app/config/inflections.php is a list of Cake variables you can use
to adjust the pluralization, singularization of classnames in Cake, along with
definining terms that shouldn't be inflected at all (like Fish and Deer, for you
outdoorsman cakers) along with irregularities.
Follow the notes inside the file to make adjustments, or use the examples in
the file by uncommenting them. You may need to know a little regex before diving
in.




CakePHP’s global configuration can be found in app/config/core.php. While we really dislike configuration files, it just had to be done. There are a few things you can change here, and the notes on each of these settings can be found within the comments of the core.php file.
DEBUG: Set this to different values to help you debug your application as you build it. Specifying this setting to a non-zero value will force Cake to print out the results of pr( ) and debug( ) function calls, and stop flash messages from forwarding automatically. Setting it to 2 or higher will result in SQL statements being printed at the bottom of the page.
Also when in debug mode (where DEBUG is set to 1 or higher), Cake will render certain generated error pages, i.e. “Missing Controller,” “Missing Action,” etc. In production mode, however (where DEBUG is set to 0), Cake renders the “Not Found” page, which can be overridden in app/views/errors/error404.thtml.
CAKE_SESSION_COOKIE: Change this value to the name of the cookie you’d like to use for user sessions in your Cake app.
CAKE_SECURITY: Change this value to indicate your preferred level of sessions checking. Cake will timeout sessions, generate new session ids, and delete old session files based on the settings you provide here. The possible values are:
CAKE_SESSION_SAVE: Specify how you’d like session data saved. Possible values are:




Your app/config/database.php file is where your database configuration all takes place. A fresh install doesn’t have a database.php, so you’ll need to make a copy of database.php.default. Once you’ve made a copy and renamed it you’ll see the following:
Example 4.1. app/config/database.php
var $default = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'project_name',
'prefix' => '');
Replace the information provided by default with the database connection information for your application.
One note about the prefix key: the string you enter there will be prepended to any SQL call that Cake makes to your database when working with tables. You define it here once so you don’t have to specify it in other places. It also allows you to follow Cake’s table naming conventions if you’re on a host that only gives you a single database. Note: for HABTM join tables, you only add the prefix once: prefix_apples_bananas, not prefix_apples_prefix_bananas.
CakePHP supports the following database drivers:
The ‘connect’ key in the $default connection allows you to specify whether or not the database connection will be treated as persistent or not. Read the comments in the database.php.default file for help on specifying connection types for your database setup.
Your database tables should also follow the following conventions:
You’ll also notice that there is a $test connection setting included in the database.php file. Fill out this configuration (or add other similarly formatted configurations) and use it in your application by placing something like:
var $useDbConfig = 'test';
Inside one of your models. You can add any number of additional connection settings in this manner.




In order use CakePHP you must first have a server that has all the required libraries and programs to run CakePHP:
There are a few ways you can secure a copy of CakePHP: getting a stable release from CakeForge, grabbing a nightly build, or getting a fresh version of code from SVN.
To download a stable version of code, check out the files section of the CakePHP project at CakeForge by going to http://cakeforge.org/projects/cakephp/.
To grab a nightly, download one from http://cakephp.org/downloads/index/nightly. These nightly releases are stable, and often include the bug fixes between stable releases.
To grab a fresh copy from our SVN repository, use your favorite SVN client and connect to https://svn.cakephp.org/repo/trunk/cake/ and choose the version you’re after.
Now that you’ve downloaded the most recent release, place that compressed package on your web server in the webroot. Now you need to unpack the CakePHP package. There are two ways to do this, using a development setup, which allows you to easily view many CakePHP applications under a single domain, or using the production setup, which allows for a single CakePHP application on the domain.
Configuring Apache and mod_rewrite
While CakePHP is built to work with mod_rewrite out of the box, we’ve noticed that a few users struggle with getting everything to play nicely on their systems. Here are a few things you might try to get it running correctly:
RewriteBase /~myusername/“.php_flag session.trans_id off” to the .htaccess file at the root of your installation as well.Make Sure It’s Working
Alright, lets see this baby in action. Depending on which setup you used, you should point your browser to http://www.example.com or http://www.example.com/cake. At this point, you’ll be presented with CakePHP’s default home, and a message that tells you the status of your current database connection.
Congratulations! You are ready to create your first Cake-based application.


More Options ...
Categories
Tag Cloud
Blog RSS
Comments RSS


Void « Default
Life
Earth
Wind
Water
Fire
Light 