CakePHP

Richard's picture

CakePHP Quick tip: Adding CSS links to $scripts_for_layout in 1.3

Got caught with this one upgrading from an older version of CakePHP to 1.3

In your view, to add a css file to the $script_for_layout variable, you used to do this:

<?php $html->css("css_file", null, array(), false); ?>

In 1.3, you have to change this to:

Richard's picture

Quick CakePHP Tip: Prefix/Admin Routing and form->create()

A quick tip to get your forms to play nice with admin routing:

/views/users/edit.ctp

<?php echo $form->create("User", array("url" => $html->url(array("admin" => true), true))) ?>

Why? For some reason, $form->create() ignores the usual routing prefix attribute ("admin"=>true) so you have to pass the url through $html->url() which does take prefix routing into account.

Richard's picture

CakePHP: Extend PaginatorHelper to indicate sort field and direction

Here's a quick piece of code that extends the default CakePHP PaginatorHelper
to add css classes to the sort column links to indicate which field is being sorted by,
and which direction:

/app/views/helpers/ex_paginator.php:

Richard's picture

CakePHP: Pagination with Containable

Took me a little while to work this out so I'm blogging it here in the hopes it will help someone in the future:

Need to Contain your Paginated data?

in your controller:

$this->paginate = array(
	"contain"=>array(
		// usual contain array
	)
);
 
$this->set("data", $this->paginate("Model"));

For example, to bring back and paginate a User (id, first_name, last_name) and their Title (Mr, Mrs, etc.):

/app/users_controller.php

Richard's picture

CakePHP: Smarter links

The Problem

There's a couple of things I wish were present in CakePHP's excellent HtmlHelper.

  1. Links should automatically add a class to themselves when they are a) pointing to a resource in the same controller and b) pointing to themselves.
  2. Able to show/hide themselves.

Why is #1 important? Imagine a primary navigation that runs across the top of a page:

Richard's picture

CakePHP: Auto populating foreign key dropdown fields

One of the neet features of CakePHP's scaffolding is that it automatically populates your foreign key (belongsTo, hasAndBelongsToMany) fields in your forms.

Lets take a look at a simple example to illustrate this:

app/models/article.php:

class Article extends AppModel {
	var $hasAndBelongsToMany = array(
		"Tag"
	);
}

app/models/tag.php:

Richard's picture

GeekUp Sheffield: The Aftermath

Last nights GeekUp was a lot of fun. Finbarr's presentation on making Agile pay was very informative and my talk on CakePHP went off without a hitch (apart from a minor technical hiccup with the hardware at the start).

As my first presentation, I was looking to learn as much as I could from the experiance. Here's what I came away with:

Richard's picture

OldMasterFlash.com

OldMasterFlash.com

This site was put together to showcase a few 'web 2.0' community features for prospective clients, and for a bit of fun :-)

It also gave us an opportunity to learn how to integrate a flash application with a CakePHP back-end and was my first introduction to the PHP GD library.

It's still a work in progress but is already proving very popular. 

Richard's picture

Plymgo.com

Plymgo.com

Another CakePHP website. Incorporates a number of 3rd party services (Google Maps, BBC Weather, a public transport route planner, US Geological Survey) to provide a public transport portal for the Plymouth Area.

Richard's picture

GeekUp Sheffield April 1st: CakePHP Presentation

GeekUp LogoNext Wednesday (ironically April 1st) I'll be doing a short (30 minute) presentation about the CakePHP framework to the good people at Sheffield GeekUp.

This is the first time I've ever done a 'proper' (ie. one that was planned and prepared) presentation and as you might expect I'm a bit jittery.

Syndicate content