Comments: Introduction

By mustan9 on 6/7/08

1 - Abstracted controllers

While we have Components that handle common business logic between controllers. There is sometimes the need for an abstract controller to share common attributes of a controller across several other controllers. I'm not sure if this is officially supported, but it's handy.

For example; If you have a controller called DocumentsController that handles most index, edit and delete actions but you have several other controllers like BookletsController, RecipesController and TextbooksController that all work on documents, then making DocumentsController abstract is handy.

First to create an abstract controller you should use the abstract keyword in PHP. This prevents the dispatcher from being allowed to instantiate the controller by itself.

abstract class DocumentsController extends AppController

{

}

Since this is an abstract class. All the required variables/actions are optional.

To use your abstracted controller you need to import the controller. For example;

App::import( 'Controller', 'DocumentsController' );

class RecipesController extends DocumentsController

{

var $name = 'Recipes';

}

Now changes made in DocumentsController results in changes for all your other classes that use it.

Since most parent classes will use a different model name that matches it's name. You can access the model indirectly. For example, if you add the index method to DocumentsController like this.

function index()

{

$model = $this->uses[0];

$records = $this->paginate($model);

$this->set('records',$records);

}

Now, you have a standard view variable called records that will paginate your record even if the model name changes.

Or do model finds like this;

$records = $this->$model->findAll();

By mustan9 on 6/7/08

2 - Trying to post above comment again. (the website corrupted the contents).

While we have Components that handle common business logic between controllers. There is sometimes the need for an abstract controller to share common attributes of a controller across several other controllers. I'm not sure if this is officially supported, but it's handy.

For example; If you have a controller called DocumentsController that handles most index, edit and delete actions but you have several other controllers like BookletsController, RecipesController and TextbooksController that all work on documents, then making DocumentsController abstract is handy.

First to create an abstract controller you should use the abstract keyword in PHP. This prevents the dispatcher from being allowed to instantiate the controller by itself.

abstract class DocumentsController extends AppController

{

}

Since this is an abstract class. All the required variables/actions are optional.

To use your abstracted controller you need to import the controller. For example;

App::import( 'Controller', 'DocumentsController' );

class RecipesController extends DocumentsController

{

var $name = 'Recipes';

}

Now changes made in DocumentsController results in changes for all your other classes that use it.

Since most parent classes will use a different model name that matches it's name. You can access the model indirectly. For example, if you add the index method to DocumentsController like this.

function index()

{

$model = $this->uses[0];

$records = $this->paginate($model);

$this->set('records',$records);

}

Now, you have a standard view variable called records that will paginate your record even if the model name changes.

Or do model finds like this;

$records = $this->$model->findAll();

By mustan9 on 6/7/08

3 - no doesn't work.

That's too bad. It was a good comment :)

Here's what it cut off (I'll try removing special characters that might be causing problems, but it's a bug in the comment system).

For example; If you have a controller called DocumentsController that handles most index, edit and delete actions but you have several other controllers like BookletsController, RecipesController and TextbooksController that all work on documents, then making DocumentsController abstract is handy.

First to create an abstract controller you should use the abstract keyword in PHP. This prevents the dispatcher from being allowed to instantiate the controller by itself.

abstract class DocumentsController extends AppController

Since this is an abstract class. All the required variables or actions are optional.

To use your abstracted controller you need to import the controller. For example;

App::import( 'Controller', 'DocumentsController' );

class RecipesController extends DocumentsController

Now changes made in DocumentsController results in changes for all your other classes that use it.

Since most parent classes will use a different model name that matches it's name. You can access the model indirectly. For example, if you add the index method to DocumentsController like this.

function index()

{

$model = $this- uses[0];

$records = $this- paginate($model);

$this->set('records',$records);

}

Now, you have a standard view variable called records that will paginate your record even if the model name changes.

Or do model finds like this;

$records = $this- $model- findAll();

By UnMonkey on 11/7/08

4 - Starting Point

You can copy an empty appController with Cake file headers intact from

/cake/cake/libs/controller/app_controller.php

to

/cake/app/app_controller.php

and implement your common code at the latter location. The rest of the framework will find it there automatically.