CakePHP: Dynamic Content in Layout

CakePHP

I was working on my first CakePHP project. I created a layout for the project and I realized that the layout was the same for multiple pages in the application – only the content was different. At first, I began to duplicate the layout for the other pages and changed the content accordingly. This didn’t make sense – I hate repeating the same code. CakePHP did not seem to have a way to create dynamic content within a layout. My first thought was to create the project using CodeIgniter’s, but I really wanted to create a web application using CakePHP.

After struggling to find a solution, I finally found the the correct way to create dynamic content with CakePHP.

Note: This post has been rewritten. When I first created this post, I was new to CakePHP. The original post did not display the best solution for creating dynamic content, so I rewrote it.

Continue reading

Checkout My New Site - T-shirts For Geeks

CakePHP and JQuery

Ajax, CakePHP, JQuery

Although Prototype is the default Javascript Framework for CakePHP, using JQuery (or any other Ajax Framework) with Cake could not be easier. Perhaps the most difficult aspect of integrating JQuery with Cake was trying to find documentation on some of those little things that are necessary to return a result set without an error (see my post, CakePHP and Ajax: Missing View).

Note: I purposely wrote this post as if I were speaking to someone who has never used CakePHP. So some parts of this post may be common knowledge to the more experienced Bakers.

Continue reading

Checkout My New Site - T-shirts For Geeks

Setting up Aptana Studio to work with CakePHP

CakePHP, IDE

I’ve been using Aptana Studio (based on Eclipse) on my Mac for over a year. It has become my default development IDE. When I first installed CakePHP, I could not understand why I was receiving errors when viewing test files in the browser. I eventually figured out that the problem was the location of my Cake files.

The important thing to remember is that the CakePHP files must exist on the root level of your web server – it can’t be located in a sub directory. Most of my projects are located under my ‘Aptana Studio’ workspace (a sub directory located on the root of my web server). So I assumed that I could do the same with CakePHP – although the documentation specifically says to place the files directly under root (at times it helps to follow the directions).

The problem is that I often work on multiple projects at a time so if I placed the Cake files outside my primary workspace, I would have to continually switch in and out of workspaces. But I found out that Aptana Studio (or eclipse) can be easily set up to work with CakePHP.

  1. Create a folder under root. Name it cake (can be named anything).
  2. Download CakePHP from here and unzip. Place the unzipped files in the cake folder.
  3. Start a new project in Aptana Studio (File->New->Project).
  4. Select PHP Project as a type and click next.
  5. On the next screen, name your project then uncheck Use Default Location
  6. Browse to the cake folder that you created (and select).
  7. Select this folder and then click finish.

That’s it. You should now be able to see your CakePHP files in your current workspace.

Checkout My New Site - T-shirts For Geeks

CakePHP: Creating Custom Components

CakePHP

CakePHP has a number of core components that can be used with an application (email, cookie, session, etc). One of the controllers that I am currently using on a project calls a custom component. (I often use controllers that do not have an associated Data Model, but use custom components instead). Setting up a custom component is rather simple.

Continue reading

Checkout My New Site - T-shirts For Geeks

CakePHP: Setting Up Multiple DataSources

CakePHP

I am working on a project using CakePHP where I initially created JSON objects to store the data. Once I began to add the JSON data to a MySQL database, I grew weary of all the input. I decided to keep one the JSON objects as is. It was data that would never change (and a lot of data). Fortunately with CakePHP you can use multiple DataSources in a project. And the DataSources don’t have to be an actual database, you can use JSON (or XML for that mater).

Continue reading

Checkout My New Site - T-shirts For Geeks

CakePHP and Ajax: Missing View

CakePHP, Pebblet

If you are using an Ajax Framework with CakePHP and receive the error message “Missing View”, then you need to add the following to your controller method:

function someMethod() {
     $this->autoRender = false;
}

Also, if you have debug turned on, you will receive a Parse error when JSON data is returned back. CakePHP set the debug level to 2 by default. You can turn it off in the app/config/core.php file by setting the debug level to 0. YOu probably don’t want to do this in the development environment. A better solution is to add the following to your controller method:

function someMethod() {
     $this->autoRender = false;
     if ( $this->RequestHandler->isAjax() ) {
          Configure::write ( 'debug', 0 );
     }
}

This let’s CakePHP know that it is an Ajax request. If you use this method, make sure that you add the RequestHandler Component to your controller.

class MyController extends AppController {
        var $components = array('RequestHandler');
}

Checkout My New Site - T-shirts For Geeks