views:

209

answers:

1

Is it possible to request data from an other controller in cakePHP?

For example, i created 2 folders in pages called search and update (both with a index.ctp) and a controller and model in the correct folders.

Both pages are using a different db source, and i wnat to display some data from the search controller into the view of the update page..

Is this possble?

Regards, Swen

A: 

It may help for you to back up a second on the principles of MVC / CakePHP.

Generally, your controllers should handle all aspects of the request -- dispatching, collecting data, etc. Your models are the points of control for your data layer. And, clearly, your views are the actual display logic and output.

If you have some data actions that are taking place in another controller, I recommend you migrate those methods into the relevant data model. That fits better with the separation of concerns model in Cake. In addition, however, you can add the additional model to the $uses variable in your second controller, which will allow you to access all the methods within it.

One note about $uses: make sure to include both the external (other) model as well as the current model. Say you're in OrangeController and you want to include the Apple model, you would (in OrangeController definition):

class OrangeController extends AppController {
 var $uses = array( 'Orange', 'Apple' );
 // other stuff
}
Travis Leleu