Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * @property string $uniqueId The unique ID of this action among the whole application. This property is
  29. * read-only.
  30. *
  31. * @author Qiang Xue <qiang.xue@gmail.com>
  32. * @since 2.0
  33. */
  34. class Action extends Component
  35. {
  36. /**
  37. * @var string ID of the action
  38. */
  39. public $id;
  40. /**
  41. * @var Controller|\yii\web\Controller the controller that owns this action
  42. */
  43. public $controller;
  44. /**
  45. * Constructor.
  46. *
  47. * @param string $id the ID of this action
  48. * @param Controller $controller the controller that owns this action
  49. * @param array $config name-value pairs that will be used to initialize the object properties
  50. */
  51. public function __construct($id, $controller, $config = [])
  52. {
  53. $this->id = $id;
  54. $this->controller = $controller;
  55. parent::__construct($config);
  56. }
  57. /**
  58. * Returns the unique ID of this action among the whole application.
  59. *
  60. * @return string the unique ID of this action among the whole application.
  61. */
  62. public function getUniqueId()
  63. {
  64. return $this->controller->getUniqueId() . '/' . $this->id;
  65. }
  66. /**
  67. * Runs this action with the specified parameters.
  68. * This method is mainly invoked by the controller.
  69. *
  70. * @param array $params the parameters to be bound to the action's run() method.
  71. * @return mixed the result of the action
  72. * @throws InvalidConfigException if the action class does not have a run() method
  73. */
  74. public function runWithParams($params)
  75. {
  76. if (!method_exists($this, 'run')) {
  77. throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
  78. }
  79. $args = $this->controller->bindActionParams($this, $params);
  80. Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
  81. if (Yii::$app->requestedParams === null) {
  82. Yii::$app->requestedParams = $args;
  83. }
  84. if ($this->beforeRun()) {
  85. $result = call_user_func_array([$this, 'run'], $args);
  86. $this->afterRun();
  87. return $result;
  88. } else {
  89. return null;
  90. }
  91. }
  92. /**
  93. * This method is called right before `run()` is executed.
  94. * You may override this method to do preparation work for the action run.
  95. * If the method returns false, it will cancel the action.
  96. *
  97. * @return boolean whether to run the action.
  98. */
  99. protected function beforeRun()
  100. {
  101. return true;
  102. }
  103. /**
  104. * This method is called right after `run()` is executed.
  105. * You may override this method to do post-processing work for the action run.
  106. */
  107. protected function afterRun()
  108. {
  109. }
  110. }