PDA

View Full Version : console application in yii/ساخت console Application در فریم ورک YII



kazemimorteza
چهارشنبه 01 مرداد 1393, 12:19 عصر
در مورد تفاوت web Application و console Application در فریم ورک yii و البته نحوه ایجاد یک console Application در فریم ورک محبوب یی (YII) راهنمایی بفرمایید.البته سوال اصلی قسمت دوم سوال هست.

MMSHFE
چهارشنبه 01 مرداد 1393, 13:07 عصر
Web Application یعنی یک سایت کامل و معمولی ولی Console Application یعنی یک اسکریپت که تحت محیط خط فرمان کار میکنه و بیشتر برای کارهایی مثل Cron و... استفاده میشه. وب سایت که نیاز به مثال نداره ولی برای برنامه کنسول یک مثال میزنم. این کد رو توی مسیر protected/commands/TestCommand.php ذخیره کنید:

<?php
class TestCommand extends CConsoleCommand
{
public function getHelp()
{

$description = "DESCRIPTION\n";
$description .= ' ' . "This is just a test command.\n";
return parent::getHelp() . $description;
}

public function actionIndex()
{
echo "This is the default action.\n";
}

public function actionHello($name)
{
echo "Hello {$name}!\n";
}
}
حالا برای اجرای اون میتونید توی محیط کنسول از دستورات زیر استفاده کنید (با فرض اینکه پروژه در مسیر C:\xampp\htdocs\site قرار داره) :

C:
CD \xampp\htdocs\site\protected

yiic help test
/* Output :
Usage: C:\xampp\htdocs\ts\protected\yiic.php test <action>
Actions:
index
hello --name=value
DESCRIPTION
This is just a test command.
*/

yiic test
// Output: This is the default action.

yiic test index
// Output: This is the default action

yiic test hello --name=PHP
// Output: Hello PHP!

yiic test hello --name="PHP Programmer"
// Output: Hello PHP Programmer!
اگه باز هم سؤالی بود در خدمتم.