0

I'm using a third party library to format data in a specific way. I managed to create a component from that library doing the following:

App::uses('Component', 'Controller');
App::import('Vendor','csformat' ,array('file'=>'csformat'.DS.'csformat.php'));

class CSFormatComponent extends Component {

    public function startup(Controller $controller){
        $controller->CSF = new csfStartup(null);
        return $controller->CSF;
    }
}

Doing that allows me to access different classes provided by the library through my controller. But I realized that I'll be doing a lot of unnecessary $this->set($one, $two) to pass formatted data from controllers to views where essentially the library could be much more beneficial as a helper since I can just format my data on the views.

Any ideas how to create such helper?

Update:

Per Kai's recommendation from the comment below, I have created a bare-bones helper that App::imports the vendor library and included the helper where needed in my controllers therefore providing me access to the library in my views.

My issue now is that I don't want to be constantly instantiating the library's csfStartup class in every view.

Is there a way to have my helper readily provide an instance of that class when the helper is called? Similar to the way my component was working.

kshaaban
  • 1
  • 3
  • 1
    When it comes to loading Vendor libraries, App::import is practically just a wrapper around `include`. So you should be able to use `App::import` in the controller, and be able to access that class in the corresponding view. Alternatively, write a bare-bones helper that's just a wrapper for accessing your library, and move your `App::import` there. – Kai Oct 17 '14 at 23:02
  • Thanks for your comment, @Kai . I followed your recommendation and created a bare-bones helper. It's working fine, but I'm looking for away to have my helper have an instance of the `csfStartup(null)` ready when I call it rather than me having to create a new instance `$this->CSF = new csfStartup(null)` in every view. Any recommendations on how to construct the helper? – kshaaban Oct 17 '14 at 23:22

1 Answers1

0

I ended up creating the helper to work as I wanted and I'm posting the answer in-case someone else is looking to use a third-party class/library as a Helper in CakePHP.

savant on the #cakephp IRC channel set me on the right path to create the helper, and with some research on Helper.php API I ended up with:

App::uses('AppHelper', 'View/Helper');
App::import('Vendor','csformat' ,array('file'=>'csformat'.DS.'csformat.php')); 

class CSFHelper extends AppHelper{

    protected $_CSF = null;

    // Attach an instance of the class as an attribute
    public function __construct(View $View, $settings = array()){
        parent::__construct($View, $settings);
        $this->_CSF= new csfStartup(null);
    }

    // PHP magic methods to access the protected property
    public function __get($name) {
        return $this->_CSF->{$name};
    }

    public function __set($name, $value) {
        $this->_CSF->{$name} = $value;
    }

    // Route calls made from the Views to the underlying vendor library
    public function __call($name, $arguments) {
        $theCall = call_user_func_array(array($this->_CSF, $name), $arguments);
        return $theCall;
    }
}
kshaaban
  • 1
  • 3