Controllers
Front Controller
/cake/app/webroot/index.php is the CakePHP front-end controller. It does three things:- specifies some constants
- requires bootstrap.php
- calls:
$Dispatcher = new Dispatcher (); $Dispatcher->dispatch($url);This looks like the primary purpose of the index.php front-end controller is to setup and use the dispatcher.
Clearly, it relies upon instantiating a dispatcher class. Dispatcher translates URLs to controller-action-parameter triads.
Pages
File Paths
At the highest level CakePHP puts its own files (e.g. its libraries etc.) under its “cake” directory; put your files under the /app/ directory. In particular, layout wrapper templates go in … /app/views/layouts/ … and html “view” files go in … /app/views/pages/.To pick up files, the path must begin with app/… For instance,
Echo $content_for_layout = file_get_contents("app/views/pages/contentPage.thtml") ;
Note: no forward-slash, prior to “app”! CakePHP expects (i.e. requires) layout template and view files with .thtml extensions.
Default templates and pages
/cake/libs/view/templates/layouts/default.thtml is the default global wrapper template page./cake/libs/view/templates/pages/home.thtml is the default sub/component view page ... a la the “decorator design pattern”.
Unless both a default.thtml and home.thtml are present, CakePHP will render its default startup page.
If you provide a /app/views/pages/home.thtml, CakePHP will render it, using the /cake/libs/view/templates/layouts/default.thtml. If you also provide a /app/views/layouts/default.thtml, CakePHP will use both your own home.thtml and default.thtml pages.
Subtle point: The header in home.thtml will NOT appear at top of the whole page, since the layout wrapper page, default.thtml, wraps the content page, home.thtml.
Static pages
One might get started with CakePHP and oriented by rendering a static page. CakePHP provides a “PagesController” which will render pages via the following syntax:
http://localhost/cake/index.php/pages/whatever
… where ‘whatever’ comes from whatever.thtml page in the /app/views/pages/ directory. Note that the file extension, .thtml, is unnecessary.
… where /pages/ refers to the name of the PagesController.
… where /cake/ represents your chosen application name (Apache Alias or virtual host).
Setup
To use CakePHP in any file system directory (e.g. a directory not under the root web path), copy the Cake files and folders to the desired directory (e.g. C:\cake).
Httpd.conf
Apache mod_rewrite is used throughout CakePHP; so, to enable Apache’s mod_rewrite module in httpd.conf un-comment:LoadModule rewrite_module modules/mod_rewrite.so
Add an Alias to xampp/apache/conf/httpd.conf, as follows:
# cake Alias /cake/ "C:/cake/" Alias /cake "C:/cake" <Directory "C:/cake/"> # AllowOverride all Allow from all DirectoryIndex index.php </Directory>AllowOverride All permits CakePHP’s .htaccess overrides. If Apache won’t re-start with AllowOverride, make sure you have two “r’s” in AllowOverride. <G>
With the ApacheFriends Xampp distribution rendering a static page worked with AllowOverride All commented “out”, but not “in”.
Database Setup
Copy /app/config/database.php default to /app/config/database.php.
Note the default database configuration parameters in database.php. Of course, you can modify database parameters to suit your application. To get oriented, at least edit the ‘user’ password and create (e.g. use PHPmyAdmin) a user, named “user” and databases to correspond with database.php.
Terminology
Controllers
Controllers define code logic - usually database model logic. A controller function is called an action. CakePHP functions are defined at api.cakephp.org
Given proper routing, a controller action/function can be accessed as:
http://www.example.com/application/function/
Models
Models are database table abstractions. CakePHP expects plural table names and singular model names.
Views
A view is a page template, usually named after an action/method. Views are comprised of PHP and HTML and can reflect any data perspective. View functions are referred to as “Helpers”. The most commonly used view Helper is “HTML Helper”.
Layouts
A layout (e.g. app/views/layouts/default.thtml) contains the presentation code that “wraps” a view, since a view shouldn’t contain logic code.Anything, common to all site views, should reside in the layout (default.thtml). Once a new default layout has been created (in app/views/layouts/), controller view code is placed inside the default layout when the page is rendered.
Switching between multiple layouts (which need, by CakePHP convention, to reside in app/views/layouts/) is done via either of two ways:
var $layout = 'layout_default_small_ad';or
setLayout();In the default example, CakePHP uses the $title_for_layout and $content_for_layout variables to specify where to place controller view code within the layout.
Elements
Presentational code blocks, repeated from page to page (e.g. sidebars), are called Elements. By CakePHP convention, elements must reside in app/views/layouts/ and have a .thtml file extension. Though no data access is built into Elements, data can be passed to elements via data arrays (e.g. parameters in an array). For example,
<?php echo $this->renderElement('repeatedElement', array ( "parameterFieldName" => "fieldValue" ) ); ?>
Back Home