Artisan
Artisan

Reputation: 4112

Using $helper in CakePHP 2

I have a question about declaring $helper explicitly. This is sample code from CakePHP Book.

<?php
class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    ..
}

In my code, I didn't have that declaration at all, but my app is still working, I can save data via my web form, I can also using $this->Html->link().

Do I really need that declaration, any disadvantages if I didn't?

Thanks to all.

Upvotes: 1

Views: 1963

Answers (1)

Khior
Khior

Reputation: 1254

The $helpers variable only needs to be declared when you are using a Helper other than 'HTML' and 'Form'. The core helpers 'Html' and 'Form' are loaded by default into the $helpers array, so the declaration is unnecessary if you only intend to use these.

If you want to add a custom helper, or use any other core helper, then you must declare the $helpers array. When you do this, you are overwriting the default helpers array, so you need to make sure to include the defaults again if you still intend to use them.

// Default. You do not need to declare this if you 
// only intend to use these helpers.
$helpers = array('HTML', 'Form'); 

// Add 'CustomHelper' to $helpers array. In this case
// HTML and Form must be declared.
$helpers = array('HTML', 'Form', 'Custom');

Upvotes: 1

Related Questions