This tutorial helps to start useing CakePHP by preparing the general “Hello World” output. I suppose that you have a fresh and working CakePHP installation.

Default Cake directory structure at a glance

app - Contains all the code, templates, and resources that are specific to your application

  • config - Includes all your application’s configurations, including database, routes and custom paths.
  • controllers - Contains your application’s controller files. Controllers contain your application’s business logic.
  • models - Contains your model files, which are responsible for providing data to your application, and saving it from your application to the datasource, usually a file or database. Models are responsible for things like data validation. Model files themselves are often just a short definition of what kind of data this Model is.
  • plugins - Plugins are third-party applications that integrate with Cake.
  • views - Contains the presentation files that generate what the user gets to see. These files contain the actual HTML that will be sent to the browser when a user requests an action. There is one group of views for each of your controllers.
    • layouts - This is a subfolder of views and contains special view files, that “wrap around” your views before they are sent to the browser. (Think of it as a header and footer in one file.)
  • webroot - Contains all files that are directly accessible by a web browser
    • css - Contains all your CSS files
    • files - Contains all files you wish to make available for download (ZIP files, etc.)
    • img - Contains all your images
    • js - Contains all your JavaScript files

cake - Contains Cake’s core libraries. This directory does not require editing.

tmp - Contains temporary and debugging files.

  • cache - Contains cached files
  • logs - Contains log files
  • test - Contains unit test files for use with SimpleTest

vendors - Contains external PHP classes and libraries you wish to use with Cake. (this section was copied from blog tutorial - 1)

The model

The Model file stands for M from MVC. Our Model file is very simple:

<?php
class HelloWorld extends AppModel
{
    var $useTable = false;
}
?>

You have to put this file into app/models/helloworld.php

Take care of the upper-lowercase naming! CakePHP uses CamelCode nameing for class and function names, but it requires lowercase filenames.

Although we have just finished with the model, I have to admit that we do not need it. Remember, that models are to control the connection to the database? Are we going to use any databases? Not really. If you prefer to delete (not make) a model file, then you have to put

var $uses = null;

in your controller’s code. The uses variable tells to the controller which models it can access.

The Controller

The Controller file stands for C from MVC. Our Controller file will simply pass the text “Hello World!” to the layout and set the page title.

<?php
class HelloWorldController extends AppController
{
  var $name = 'HelloWorld';
  var $pageTitle = 'Hello world!';
 
  function index()
    {
      $this->set('data','Hello World!');
    }
}
?>

You have to put this file into app/controllers/helloworld_controller.php

The View

Probably you could find out alone, that we are going to deal now with the V part from MVC.

<?php echo $data; ?>

Save this file at app/views/hello_world/index.thtml

Now open http://www.yourhost.com/where_cake_is/helloworld . (If you get an url not found error, try http://www.yourhost.com/where_cake_is/index.php/helloworld. That means mod_rewrite is not working. See http://wiki.cakephp.org/faq:how_do_i_set_up_mod_rewrite)

Do you like it? Not really? You would like to get rid of the default CakePHP layout? Let’s do it!

Set the default layout

Take a look at the manual: http://manual.cakephp.org/pages/ch08s01#d0e1237. We are going to prepare a basic default layout.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title_for_layout?></title>
<body>
 
<?php echo $content_for_layout ?>
 
</body>
</html>

Save this at app/views/layouts/default.thtml

Finished!

 
tutorials/hello_world.txt · Last modified: 2006/03/04 18:35 by viktor