Tag Archives: CakePHP

CSS files and $scripts_for_layout

You probably know that if you have $scripts_for_layout in your site’s layout, then you can include JS files from the view. For example, if you’d like to include ‘myScript.js’ from some view you would add the following code:

Read more

Cakephp – Beautify Html Code Output

It’s no secret that CakePHP often outputs some very ugly and hard to read HTML. This little trick will make it a lot cleaner.
Create a file named app_helper.php in your app’s root directory.

Read more

CakePHP – Models Part 2

Callback Methods

If you want to sneak in some logic just before or after a CakePHP model operation, use model callbacks. These functions can be defined in model classes (including your AppModel) class. Be sure to note the expected return values for each of these special functions.

Read more

CakePHP – Models Part 1

Naming

  • Ingredient
  • extends AppModel
  • ingredient.php

The Model is automatically available to the Controller when the name matches

function index() {
//grab all ingredients and pass it to the view:
$ingredients = $this->Ingredient->find(‘all’);
$this->set(‘ingredients’, $ingredients);
}


Read more

CakePHP – Components

Naming:

  • MathComponent
  • extends Object
  • /app/controllers/components/math.php

Initialising:

var $components = array(‘Math’, ‘Session’);

When including Components in a Controller you can also declare a set of parameters that will be passed on to the Component’s initialize() method. These parameters can then be handled by the Component.
var $components = array(
‘Math’ => array(
‘precision’ => 2,
‘randomGenerator’ => ’srand’
),
‘Session’, ‘Auth’
);


Read more

CakePHP – Controllers

Naming conventions:

  • RecipesController
  • recipes_controller.php
  • extends AppController

Different Variables

var $name = ‘Recipes’;
var $uses = array(‘Recipe’, ‘User’);
var $helpers = array(‘Ajax’);
var $components = array(‘Email’);

Read more