Created
April 11, 2014 06:07
-
-
Save satomixx/10443297 to your computer and use it in GitHub Desktop.
[CakePHP 2.x] shell(シェル)からComponent(コンポーネント)を呼び出す ref: http://qiita.com/tsumekoara/items/d302cc0eeec3adc18456
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('Shell', 'Console'); | |
class AppShell extends Shell { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object(OnakahettaShell) { | |
uses => array() | |
. | |
. | |
Tamagoyaki => object(TamagoyakiComponent) {} <-いた!! | |
. | |
. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object(TamagoyakiComponent) { | |
components => array() | |
settings => array() | |
[protected] _Collection => object(ComponentCollection) {} | |
[protected] _componentMap => array() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('ComponentCollection', 'Controller'); //これが大事 | |
App::uses('TamagoyakiComponent', 'Controller/Component'); | |
// ↑このHogeComponentとapp/Controller/Component以下の呼び出したいファイルの『クラスの名前』が一致しているか確認。(まあ基本はクラスとファイル名は一致させる) | |
/** | |
* こういうクラス ちなみにAppShellがShellを継承している | |
*/ | |
class OnakahettaShell extends AppShell { | |
/** | |
* TamagoyakiComponent.phpの呼び出し | |
*/ | |
public function startup() { | |
$collection = new ComponentCollection(); //これが大事です。 | |
$this->Hoge = new TamagoyakiComponent($collection); //コンポーネントをインスタンス化 | |
parent::startup(); | |
} | |
/** | |
* メイン処理 | |
*/ | |
public function main() { | |
$monya = $this->Tamagoyaki->__fuga(); | |
//↑こんな感じで使えます◎ | |
Debugger::log($this); // 出力内容は下に記載 | |
Debugger::log($this->Tamagoyaki); //出力内容は下に |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment