本文介绍了将数据从CakePHP组件传递到助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在组件和助手之间共享数据。我将我自己制作的支付服务formdata生成器转换为CakePHP插件,我想能够从控制器(使用组件)填写付款数据,并使用助手打印出数据。 p>

到目前为止,我尝试过的所有东西都觉得有点太黑了,所以让我问你:有没有任何优雅的方式从数据传递组件到助手?






编辑:



这种特殊情况通过在组件初始化期间将原始formadata类实例添加到ClassRegistry。这样,帮助者也可以使用ClassRegistry访问实例。



但是,这只适用于对象,所以问题仍然存在。


$ b $ b

是的,与向助手传递任何数据的方式相同。在你看来。



在你的组件中,我会做类似下面的事情。 beforeRender()操作是一个。

  public function beforeRender(Controller $ controller){
$ yourVars ='some data'
$ goHere ='other stuff';

$ controller-> set(compact('yourVars','goHere'));
}

然后在您的视图中,您可以将数据传递给您的助手, 。

  //视图或布局* .ctp文件
$ this-> YourHelper-> yourMethod yourVars);
$ this-> YourHelper-> otherMethod($ goHere);


I need to share data between a component and helper. I'm converting my self-made payment service formdata generator to a CakePHP plugin and I'd like to be able to fill in the payment data from the controller(using a component) and use a helper to print out the data.

Everything I've tried so far have felt a little too hacky, so let me ask you: Is there any elegant way to pass data from a component to a helper?


edit:

I solved this particular situation by adding the original formadata class instance to ClassRegistry during the component initialization. This way the helper too can access the instance using ClassRegistry.

However, this only works for objects, so the question remains open.

解决方案

Yes, the same way you pass any data to the helper. In your view.

Inside your component I would do something like the following. The beforeRender() action is a CakePHP component callback.

public function beforeRender(Controller $controller) {
    $yourVars = 'some data';
    $goHere = 'other stuff';

    $controller->set(compact('yourVars', 'goHere'));
}

Then in your view you can pass the data off to your helpers just like normal.

// view or layout *.ctp file
$this->YourHelper->yourMethod($yourVars);
$this->YourHelper->otherMethod($goHere);

这篇关于将数据从CakePHP组件传递到助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:23