InlineAction.php 1.77 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\base;
Qiang Xue committed
9

Qiang Xue committed
10 11
use Yii;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * InlineAction represents an action that is defined as a controller method.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16
 * The name of the controller method is available via [[actionMethod]] which
 * is set by the [[controller]] who creates this action.
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
19
 * @since 2.0
Qiang Xue committed
20
 */
Qiang Xue committed
21
class InlineAction extends Action
Qiang Xue committed
22
{
23
    /**
24
     * @var string the controller method that this inline action is associated with
25 26
     */
    public $actionMethod;
Qiang Xue committed
27

28
    /**
29 30
     * @param string $id the ID of this action
     * @param Controller $controller the controller that owns this action
31
     * @param string $actionMethod the controller method that this inline action is associated with
32
     * @param array $config name-value pairs that will be used to initialize the object properties
33 34 35 36 37 38
     */
    public function __construct($id, $controller, $actionMethod, $config = [])
    {
        $this->actionMethod = $actionMethod;
        parent::__construct($id, $controller, $config);
    }
Qiang Xue committed
39

40 41 42
    /**
     * Runs this action with the specified parameters.
     * This method is mainly invoked by the controller.
43
     * @param array $params action parameters
44 45 46 47 48 49 50 51 52 53 54 55
     * @return mixed the result of the action
     */
    public function runWithParams($params)
    {
        $args = $this->controller->bindActionParams($this, $params);
        Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);
        if (Yii::$app->requestedParams === null) {
            Yii::$app->requestedParams = $args;
        }

        return call_user_func_array([$this->controller, $this->actionMethod], $args);
    }
Qiang Xue committed
56
}