InlineAction.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. use Yii;
  9. /**
  10. * InlineAction represents an action that is defined as a controller method.
  11. *
  12. * The name of the controller method is available via [[actionMethod]] which
  13. * is set by the [[controller]] who creates this action.
  14. *
  15. * For more details and usage information on InlineAction, see the [guide article on actions](guide:structure-controllers).
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class InlineAction extends Action
  21. {
  22. /**
  23. * @var string the controller method that this inline action is associated with
  24. */
  25. public $actionMethod;
  26. /**
  27. * @param string $id the ID of this action
  28. * @param Controller $controller the controller that owns this action
  29. * @param string $actionMethod the controller method that this inline action is associated with
  30. * @param array $config name-value pairs that will be used to initialize the object properties
  31. */
  32. public function __construct($id, $controller, $actionMethod, $config = [])
  33. {
  34. $this->actionMethod = $actionMethod;
  35. parent::__construct($id, $controller, $config);
  36. }
  37. /**
  38. * Runs this action with the specified parameters.
  39. * This method is mainly invoked by the controller.
  40. * @param array $params action parameters
  41. * @return mixed the result of the action
  42. */
  43. public function runWithParams($params)
  44. {
  45. $args = $this->controller->bindActionParams($this, $params);
  46. Yii::debug('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);
  47. if (Yii::$app->requestedParams === null) {
  48. Yii::$app->requestedParams = $args;
  49. }
  50. return call_user_func_array([$this->controller, $this->actionMethod], $args);
  51. }
  52. }