tags:

views:

2002

answers:

2

I want to call a controller function from a view. Is that possible in Cake PHP?

+3  A: 

It is possible via the requestAction method, but it is not recommended due to a slight performance hit. Use it wisely.

As Xr noted in the comment, using requestAction often signifies design issues (MVC separation).

dr Hannibal Lecter
Not exactly massive (not anymore, at least): http://cakedc.com/developer/mark_story/2009/02/27/benchmarking-requestaction .Still, if you want to call a controller function, you probably have problems with how you separated functionality between the parts of the MVC model.
Xr
Thanks for pointing that out, I've completely missed that article.
dr Hannibal Lecter
A: 

Yeap use requestMethod with Caching

$out = Cache::read('savedincache');
if(empty($out)){
   $out = $this->requestAction('/articles/myfunction');
   Cache::write('savedincache', $out);
}

in /app/controllers/articles_controller.php

function myfunction(){
   $out = $this->Article->find('all');
   return $out
}
Aziz