CakePhp controller with example

1 Jan

As I have already defined core configuration and routing in my previous post, its now time to wit my hands with cakePhp controllers.

In MVC archecticture controllers are used to integrate models and views.

It is not a hard job to define a controller in cakePhp. You would need to define a class exetending it from AppController class of cakePph. E.g

<?php

class MyController extends AppContrller

{

}

write the code above and save it in you app/controllers directory. That’s it you have now define your first controller in cakePhp.

You can define as many action in this controller class as you want. Simple write

<?php

class MyController extends AppContrller

{

// this is view action.

function view()

{

}

}

In the code above you can see that we have defined our view action.

If you want to call this action from you browser, simply write

http://localhost/cake/my/view

I assumed that you have installed your cake in “cake” directory.

Next thing you may want to use might be model(s). If you want to use specific models in the controller, write

class MyController extends AppContrller

{

// initializing models array

var $uses=array('Company','User');

// this is view action.

function view()

{

}

}

The statement var $uses = array(‘company’,’user’) tells that we want to use these two models in our controller.

Models play a extremely vital role in MVC archeticture and we hopefully will cover this topic in our later posts.

Another very nice feature cakePhp provide is that you can define your own components in addition to those shipped with cakePhp itself called cakePhp standard components, and can use them in your application.

Components are very useful in case you need similar functionality throughout you application. Just put those function in your component and simply use that component in your controller as

$components = array('Component_name'); e.g

$components =array('Email');

Email is standard component shipped with cakePhp.

You can define as many component in this array and can use them in your controller. Components will need to be define in separate post, I think. So I am leaving this discussion here.

Next important thing that you can include in your controller and get benefits out of it, would definitely be helpers. Simply write

$helper = array('helper_name');

in your controller. This way you can include and use as many helpers as you want.

Before ending my post, I would better discuss the parameter that can be accessed in controller.

The most important data that you want to get in your controller would definitely be the one submitted through form, either using get or post method.

This is very simple. Write

$data = $this->data;

this will give you entire data submitted. You can have individual fields as

$first_name = $this->data['User']['first_name'];

I assumed that you have field name ‘first_name’ in your form while ‘User’ is the name of the form.

Another very handy attribute would be

$posted_data = $this->params;

This not only return the data submitted via form, but also additional data like controller name, action name, plugin etc.

If you want to get specific data like controller name, write

$controller_name = $this->params['controller'];


and similarly

$action_name = $this->params['action'];


If you want to get query string, write

$query_string = $this->params['pass'];

and url can be get as

$url = $this->params['url'];

this will return an array of url and query string.

At the end I would like to discuss a very important method cake provide us in the controller is set(). This method is used as assign() method of the zend framework.

If you want to assign something like variable or array of variables to the view template, simply write

$this->set('color','blue');

this variable then can be accessed in view template as

$color = $color;

You can also write

$this->set(array('color'=>'blue','font'=>'arial'));

While assigning variable to the view template.

Two other important method are

redirect() and flash();

redirect can be used as

$this->redirect(array('controller'=>'orders','view'=>'thanks'));

and flash as

$this->flash ($message, $url, $pause);

Where message is what you want to be displayed, url is very your page should be redirected after the message is displayed and $pause is an integer indicating how long you want your message to be displayed.

One Response to “CakePhp controller with example”

  1. Josh January 11, 2009 at 1:54 pm #

    First of all congratulation for such a great site. I learned a lot reading here today. I will make sure i visit this site more often so i can learn more.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: