Controller.php 1.87 KB
Newer Older
Alexander Makarov committed
1 2
<?php
/**
Qiang Xue committed
3
 * Controller class file.
Alexander Makarov committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
Alexander Makarov committed
7 8 9 10 11
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console;

Qiang Xue committed
12
use yii\base\Action;
Qiang Xue committed
13 14
use yii\base\Exception;

Alexander Makarov committed
15
/**
Qiang Xue committed
16
 * Controller is the base class of console command classes.
Alexander Makarov committed
17
 *
Qiang Xue committed
18 19 20
 * A controller consists of one or several actions known as sub-commands.
 * Users call a console command by specifying the corresponding route which identifies a controller action.
 * The `yiic` program is used when calling a console command, like the following:
Alexander Makarov committed
21
 *
Qiang Xue committed
22 23 24
 * ~~~
 * yiic <route> [...options...]
 * ~~~
Alexander Makarov committed
25 26 27 28
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
29
class Controller extends \yii\base\Controller
Alexander Makarov committed
30
{
Qiang Xue committed
31 32 33 34 35 36 37 38 39 40 41 42
	/**
	 * This method is invoked when the request parameters do not satisfy the requirement of the specified action.
	 * The default implementation will throw an exception.
	 * @param Action $action the action being executed
	 * @param Exception $exception the exception about the invalid parameters
	 */
	public function invalidActionParams($action, $exception)
	{
		echo "Error: " . $exception->getMessage() . "\n";
		\Yii::$application->end(1);
	}

Qiang Xue committed
43 44 45 46 47 48 49 50 51
	/**
	 * This method is invoked when extra parameters are provided to an action when it is executed.
	 * The default implementation does nothing.
	 * @param Action $action the action being executed
	 * @param array $expected the expected action parameters (name => value)
	 * @param array $actual the actual action parameters (name => value)
	 */
	public function extraActionParams($action, $expected, $actual)
	{
Qiang Xue committed
52
		unset($expected['args'], $actual['args']);
53

Qiang Xue committed
54 55
		$keys = array_diff(array_keys($actual), array_keys($expected));
		if (!empty($keys)) {
Qiang Xue committed
56
			echo "Error: " . \Yii::t('yii', 'Unknown parameters: {params}', array(
Qiang Xue committed
57
				'{params}' => implode(', ', $keys),
Qiang Xue committed
58 59
			)) . "\n";
			\Yii::$application->end(1);
Alexander Makarov committed
60 61 62
		}
	}
}