Tags

, , ,

Kali ini kita akan iseng-iseng membuat command sederhana. Berikut codenya

namespace app\commands;

use yii\helpers\Console;

/**
 * Description of InspireController
 *
 * @author Misbahul D Munir <misbahuldmunir@gmail.com>
 * @since 1.0
 */
class InspireController extends \yii\console\Controller
{
    protected $quotes = [
        'When there is no desire, all things are at peace. - Laozi',
        'Simplicity is the ultimate sophistication. - Leonardo da Vinci',
        'Simplicity is the essence of happiness. - Cedric Bledsoe',
        'Smile, breathe, and go slowly. - Thich Nhat Hanh',
        'Simplicity is an acquired taste. - Katharine Gerould',
        'Well begun is half done. - Aristotle',
        'He who is contented is rich. - Laozi',
        'Very little is needed to make a happy life. - Marcus Antoninus',
    ];

    public function actionIndex()
    {
        $quote = $this->quotes[array_rand($this->quotes)];
        $this->stdout("\n$quote\n\n", Console::FG_YELLOW, Console::BOLD);
    }
}

Simpan dengan nama file InspireController.php. Ubah folder dan namespace sesuai dengan struktur aplikasi. Untuk template basic, namespacenya adalah app\commands. Untuk template advanced, namespacenya adalah console\controllers.

Cara menjalankan command tersebut adalah, dari command line

$ php yii inspire
Advertisements