Cakephp Helpers in Views and $this

2019-07-04 04:23发布

I'm trying to determine what the best standard is for using helpers in views whether is should be

echo $form->input();

or

echo $this->Form->input();

In the CakePHP manual ver 1.2 the Helper class is accessed by the helper object directly, whereas in the 1.3 book the helper object is accessed through the View.

Does this matter?

Leo

1条回答
做自己的国王
2楼-- · 2019-07-04 05:22

It really only matters because of the possibility of having a collision that will "wipe out" your access to the helper. Say I had a model named Form and decided to do something like this in my view after getting many records.

foreach ($forms as $form) {
    echo $form['Form']['name'] . '<br/>';
}

See what happened there? I accidentally just overwrote the $form variable, basically losing my FormHelper.

The standard is to now access all helpers via $this in the view.

查看更多
登录 后发表回答