tags:

views:

135

answers:

3

Hi,

How do I call a function from a component from a view? (ctp file)
Is that even a good practice to do that?

Thanks,
Tee

+2  A: 

For most components you could use something like:

App::import('Component', 'YourComponent');
$theComponent = new YourComponent();
$theComponent->yourMethod();

However, components are meant to share functionality used by controllers, so usually you should avoid calling components from views.

dhofstet
+2  A: 

i suggest wrapping the component methods in a helper, and then using the usual route to access the helper.

Jonathan
A: 

My opinion is it is not good practice to do that. Think of a view as something designers would work with. You want to keep your code in the models and controllers if at a possible. If it has to do with reusable view content, consider moving the controller to an element or a helper, as that is what they are intended for.

If you provide more details, we can be more specific to how to implement something if you already have a code base we can reference.

cdburgess