Action.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * Action is the base class for all controller action classes.
  11. *
  12. * Action provides a way to reuse action method code. An action method in an Action
  13. * class can be used in multiple controllers or in different projects.
  14. *
  15. * Derived classes must implement a method named `run()`. This method
  16. * will be invoked by the controller when the action is requested.
  17. * The `run()` method can have parameters which will be filled up
  18. * with user input values automatically according to their names.
  19. * For example, if the `run()` method is declared as follows:
  20. *
  21. * ```php
  22. * public function run($id, $type = 'book') { ... }
  23. * ```
  24. *
  25. * And the parameters provided for the action are: `['id' => 1]`.
  26. * Then the `run()` method will be invoked as `run(1)` automatically.
  27. *
  28. * For more details and usage information on Action, see the [guide article on actions](guide:structure-controllers).
  29. *
  30. * @property string $uniqueId The unique ID of this action among the whole application. This property is
  31. * read-only.
  32. *
  33. * @author Qiang Xue <qiang.xue@gmail.com>
  34. * @since 2.0
  35. */
  36. class Action extends Component
  37. {
  38. /**
  39. * @var string ID of the action
  40. */
  41. public $id;
  42. /**
  43. * @var Controller|\yii\web\Controller|\yii\console\Controller the controller that owns this action
  44. */
  45. public $controller;
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $id the ID of this action
  50. * @param Controller $controller the controller that owns this action
  51. * @param array $config name-value pairs that will be used to initialize the object properties
  52. */
  53. public function __construct($id, $controller, $config = [])
  54. {
  55. $this->id = $id;
  56. $this->controller = $controller;
  57. parent::__construct($config);
  58. }
  59. /**
  60. * Returns the unique ID of this action among the whole application.
  61. *
  62. * @return string the unique ID of this action among the whole application.
  63. */
  64. public function getUniqueId()
  65. {
  66. return $this->controller->getUniqueId() . '/' . $this->id;
  67. }
  68. /**
  69. * Runs this action with the specified parameters.
  70. * This method is mainly invoked by the controller.
  71. *
  72. * @param array $params the parameters to be bound to the action's run() method.
  73. * @return mixed the result of the action
  74. * @throws InvalidConfigException if the action class does not have a run() method
  75. */
  76. public function runWithParams($params)
  77. {
  78. if (!method_exists($this, 'run')) {
  79. throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
  80. }
  81. $args = $this->controller->bindActionParams($this, $params);
  82. Yii::debug('Running action: ' . get_class($this) . '::run(), invoked by ' . get_class($this->controller), __METHOD__);
  83. if (Yii::$app->requestedParams === null) {
  84. Yii::$app->requestedParams = $args;
  85. }
  86. if ($this->beforeRun()) {
  87. $result = call_user_func_array([$this, 'run'], $args);
  88. $this->afterRun();
  89. return $result;
  90. }
  91. return null;
  92. }
  93. /**
  94. * This method is called right before `run()` is executed.
  95. * You may override this method to do preparation work for the action run.
  96. * If the method returns false, it will cancel the action.
  97. *
  98. * @return bool whether to run the action.
  99. */
  100. protected function beforeRun()
  101. {
  102. return true;
  103. }
  104. /**
  105. * This method is called right after `run()` is executed.
  106. * You may override this method to do post-processing work for the action run.
  107. */
  108. protected function afterRun()
  109. {
  110. }
  111. }