雑記まみむメモ

雑記、メモ、技法、話題の騒動などを紹介します。

cakephpでシェルを動かす方法、とりあえず実行

Console/Command配下にシェルを作成する vi /var/www/cakephp/app/Console/Command/PrintShell.php

<?php
App::uses('AppController', 'Controller');
App::uses('SampleController', 'Controller');// コントローラーを呼び出す①

/**
 * print.shell
 *
 * @uses AppShell
 * @package app.Console.Command
 */
class PrintShell extends AppShell
{

    public function startup()
    {
        parent::startup();
    }

    public function exe()
    {
        echo "hello world";
    }

    public function sample()
    {
        // コントローラーを呼び出す②
        $sample = new SampleController();
        $sample->exe();
    }
}

SampleController.phpの作成

vi /var/www/cakephp/app/Controller/SampleController.php

<?php
App::uses('AppController', 'Controller');
class SampleController extends AppController {
    public $name = 'sample';

    public function exe(){
        echo "hello world";
    }
}

shell実行

shell内で"hello world"を出力

php /var/www/cakephp/app/Console/cake.php print exe
hello world

shell内で別のコントローラーを呼び出して"hello world"を出力

php /var/www/cakephp/app/Console/cake.php print sample
hello world