如何使用控制器操作方法动态添加帮助程序或组件

发布于 2024-10-01 16:35:36 字数 275 浏览 1 评论 0原文

我不想按如下方式添加它,因为我在某些操作方法中只需要它们一次

(所以不要无用地加载内存) <代码>

class UsersController extends AppController {
    var $name = 'Users';
    var $helpers = array('Html', 'Session');
    var $components = array('Session', 'Email');

i don't want to add it as below cause i needed them only once in certain action method

(so do not useless load the memory)

class UsersController extends AppController {
    var $name = 'Users';
    var $helpers = array('Html', 'Session');
    var $components = array('Session', 'Email');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪心 2024-10-08 16:35:36
class UsersController extends AppController {
  public function method_name() {
    $this->helpers[] = 'MyHelper'
  }
}

有关此内容的更多信息,请文档

希望有帮助。

class UsersController extends AppController {
  public function method_name() {
    $this->helpers[] = 'MyHelper'
  }
}

More on this in the documentation.

Hope that helps.

梦里寻她 2024-10-08 16:35:36

您可以使用上面提到的 Rob 加载帮助程序

$this->helpers[] = 'MyHelper';

,但这对于控制器不起作用,因为它们有需要调用才能工作的初始化和启动方法。

我在网上遇到了一些用于在控制器操作内加载组件的代码: ComponentLoaderComponent

是的,它是一个组件,但它不是很大,因此将它包含在控制器中应该不成问题。

或者您可以研究它以了解组件加载的工作原理,然后编写您自己的控制器操作来执行相同的操作。

You can load helpers using

$this->helpers[] = 'MyHelper';

as Rob mentioned above, but this won't work for controllers because they have their initialize and startup methods that need to be called in order for them to work.

I've come across a bit of code on the web for loading components inside of a controller action: ComponentLoaderComponent

Yes, it is a component but it isn't very big so it shouldn't be a problem to include it in your controllers.

Either that or you can just study it to see how the component loading works and then write your own controller action to do the same.

转瞬即逝 2024-10-08 16:35:36

我使用一个组件来动态添加助手和组件:

$this->Common->addHelper('Tools.Datetime');
$this->Common->addHelper(array('Text', 'Number', ...));
$this->Common->addComponent('RequestHandler');
$this->Common->addLib(array('MarkupLib'=>array('type'=>'php'), ...));

完整的代码可以在我刚刚打开的 cakephp 增强票中看到:
http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/1277

或者使用 php 标记:
http://www.dereuromark.de/2010/ 11/10/loading-classes-on-the-fly/

它还修复了 mtnorthrop 发布的解决方案中的一些小问题。
现在可以使用插件和传递选项。玩得开心。

I use a component for adding helpers and components on the fly:

$this->Common->addHelper('Tools.Datetime');
$this->Common->addHelper(array('Text', 'Number', ...));
$this->Common->addComponent('RequestHandler');
$this->Common->addLib(array('MarkupLib'=>array('type'=>'php'), ...));

etc

The complete code to this can be seen in the cakephp enhancement ticket I just opened:
http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/1277

Or with php markup:
http://www.dereuromark.de/2010/11/10/loading-classes-on-the-fly/

It also fixes some minor problems with the solution posted by mtnorthrop.
Plugins as well as passed options are now possible. Have fun.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文