How to load a component in console/shell

后端 未结 5 1663
耶瑟儿~
耶瑟儿~ 2020-12-20 17:22

In CakePHP, how do I load a component in a shell?

To use a component in a controller, you include an array of components in a property called $components. That doesn

5条回答
  •  半阙折子戏
    2020-12-20 18:13

    I assume that you have a component named YourComponent:

    - with cake 2., you can load your component like this

    App::uses('ComponentCollection', 'Controller');
    App::uses('YourComponent', 'Controller/Component');
    
    class YourShell extends AppShell {
    
        public function startup() {
            $collection = new ComponentCollection();
            $this->yourComponent = $collection->load('Your');
        }
    
        public function main() {
            $this->yourComponent->testMe();
        }
    }
    

    - with cake 3. you can load your component like this

    yourComponent = new YourComponent(new ComponentRegistry(), []);
        }
    
        public function main() {
            $pages = $this->yourComponent->testMe();
        }
    }
    

提交回复
热议问题