cakephp load component on fly

11,829

There is literally a section in the CakePHP book called "Loading Components on the fly".

CakePHP 2.x:

http://book.cakephp.org/2.0/en/controllers/components.html#loading-components-on-the-fly

CakePHP 3.x:

http://book.cakephp.org/3.0/en/controllers/components.html#loading-components-on-the-fly

(and it's done different than how you're attempting)

Share:
11,829
Amit Kumar Sharma
Author by

Amit Kumar Sharma

Updated on November 24, 2022

Comments

  • Amit Kumar Sharma
    Amit Kumar Sharma over 1 year

    I want to load component on fly in cakePHP In this example, loading dynamically RequestHandler component for json response

    public function xyz() {
    
        $this->components[] ='RequestHandler';
        $this->Components->load('RequestHandler');
        $this->RequestHandler->initialize($this);
    
        $abc = array("hello","world");
        $this->set('abc', $abc);
        $this->set('_serialize', array( 'abc'));
    }
    

    error generated on initialize function shows undefined.

    AMENDMENT FOR MORE CLEAR PICTURE:

    public function xyz() {
        $this->components[] ='RequestHandler';
        $this->RequestHandler =  $this->Components->load('RequestHandler');
        $this->RequestHandler->initialize($this);
    
        $abc = array("hello","world");
        $this->set('abc', $abc);
        $this->set('_serialize', array( 'abc'));
    }
    

    I also tried

    public function xyz() {
    
        $this->RequestHandler =  $this->Components->load('RequestHandler');
        $abc = array("hello","world");
        $this->set('abc', $abc);
        $this->set('_serialize', array( 'abc'));
    }
    

    I can't use component like this #$this->RequestHandler->getTime(); because cakephp automatic handle json respone. When I hit just above code using http://disecake.localhost/resources/xyz.json

    {"code":500,"url":"/resources/xyz.json","name":"View file " /var/www/disecake/app/View/Resources/xyz.ctp" is missing."}

    When I use

    public $components = array( 'RequestHandler'); 
    in my cotroller than output
    {"abc":["hello","world"]}

    I think now question is more clear.

    • Elias Van Ootegem
      Elias Van Ootegem almost 11 years
      "error generated on initialize function shows undefined. Thanks" You're welcome. Please read the FAQ: no thanks in questions
    • Álvaro González
      Álvaro González almost 11 years
      @EliasVanOotegem - FAQ is no longer among us. We now have a "Help Center".
    • Álvaro González
      Álvaro González almost 11 years
      Can you please edit the question and use the clipboard to copy and paste the exact error message?
  • Amit Kumar Sharma
    Amit Kumar Sharma almost 11 years
    You are right about document. But my dear friend this document does not work as expected. In my case I want json response and I follow your given document instruction it shows related view "xyz.ctp" is not present. More over if I load RequestHandler component by define in top of controller it gives right output. I think you got my problem.
  • Amit Kumar Sharma
    Amit Kumar Sharma almost 11 years
    Please notice in your given document and my code that in your document OneTimer is used $this->OneTimer->getTime(); but in my case cakePHP use that component automatically for output.
  • Dave
    Dave almost 11 years
    @AmitKumarSharma Your question is how to "load component on fly", not "how to return JSON data". If you have trouble with JSON, please ask a new, more related question.
  • Amit Kumar Sharma
    Amit Kumar Sharma almost 11 years
    I can manually return data by using $this->response. But I want to do with component and that component need to be on fly because only one function of my controller will use this component
  • Dave
    Dave almost 11 years
    @AmitKumarSharma - right. Which is why you should follow the instructions in the CakePHP book per my answer/link.
  • Amit Kumar Sharma
    Amit Kumar Sharma almost 11 years
    I am following CakePHP book but I found nowhere how to load RequestHandler component on fly.
  • Dave
    Dave almost 11 years
    Hint: Read the link I sent, and change "OneTimer" to "RequestHandler". It's not likely that the book will ever list out exactly how to load every single component - usually they'll show you how and assume you can figure out how to rename it if you want a different component.
  • Dave
    Dave almost 11 years
    Honestly though, it sounds like you're loading it fine (now that you've revised the question). The error is likely something else that will require a new question.
  • Amit Kumar Sharma
    Amit Kumar Sharma almost 11 years
    Thanks for your effort! I can complete my task with different ways. But I just wanted to know how this specific problem can be solved.