Passing data from CakePHP component to a helper

前端 未结 3 1882
萌比男神i
萌比男神i 2020-12-18 10:27

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

3条回答
  •  半阙折子戏
    2020-12-18 10:51

    Is there any elegant way to pass data from a component to a helper?

    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);
    

提交回复
热议问题