I have a method name getImage() in AppController
and i want to call it in my view.ctp file
how can i do that or is it possible to call a method in AppController in my view
If it’s a static function, you could use AppController::getImage()
from your view. But more likely, what you will want to do is to set
the result of getImage
from the view
function in whichever controller is handling the action, and use that in the view. Like,
$this->set('image', $this->getImage());
in your controller action, and then reference $image
in your view.ctp
.
the problem is that the data that will be pass from the getImage() method are came from a for loop in my view
foreach ($crmPoProducts as $crmPoProduct):
$img = AppController::getImage($crmPoProduct->id);
endforeach;
But it does' nt work
Is there a compelling reason why the getImage
function needs to be in AppController
?
getImage() method gets the product id in another site via api
i create the getImage method to implement DRY into my code
thats why the method must be in AppController in order to be called simultaneously
Does it use $this
at all? If it does, could it be changed to not use it? If you can eliminate all usage of $this
in the getImage
function, you could make it static, and then AppController::getImage
would work.
Static functions like this are a bit of a hack, though; you could alternately consider moving it to a completely separate class that just does this one thing. Maybe a view cell would be a good fit?