20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public function run($args) { ... }<br />

where $args refers to the extra parameters given in the command line.<br />

Within a console command, we can use <strong>Yii</strong>::app() to access the console application<br />

instance, through which we can also access resources such as database connections (e.g.<br />

<strong>Yii</strong>::app()->db). As we can tell, the usage is very similar to what we can <strong>do</strong> in a Web<br />

application.<br />

Info: Starting from version <strong>1.1</strong>.1, we can also create global commands that are shared<br />

by all <strong>Yii</strong> applications on the same machine. To <strong>do</strong> so, define an environment variable<br />

named YII_CONSOLE_COMMANDS which should point to an existing directory. We<br />

can then put our global command class files under this directory.<br />

Console Command Action<br />

Note: The feature of console command action has been available since version <strong>1.1</strong>.5.<br />

A console command often needs to handle different command line parameters, some<br />

required, some optional. A console command may also need to provide several subcommands<br />

to handle different sub-tasks. These work can be simplified using console<br />

command actions.<br />

A console command action is a method in a console command class. The method name<br />

must be of the format action Xyz, where Xyz refers to the action name with the first letter in<br />

upper-case. For example, a method actionIndex defines an action named index.<br />

To execute a specific action, we use the following console command format:<br />

yiic --option1=value --option2=value2 ...<br />

The additional option-value pairs will be passed as named parameters to the action<br />

method. The value of a xyz option will be passed as the $xyz parameter of the action<br />

method. For example, if we define the following command class:<br />

class SitemapCommand extends CConsoleCommand<br />

{<br />

public function actionIndex($type, $limit=5) { ... }<br />

public function actionInit() { ... }<br />

}<br />

Then, the following console commands will all result in calling actionIndex('News', 5):

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!