Action.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 divide a complex controller into
  13. * smaller actions in separate class files.
  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. * ~~~
  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 <[email protected]>
  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 the controller that owns this action
  42. */
  43. public $controller;
  44. /**
  45. * Constructor.
  46. * @param string $id the ID of this action
  47. * @param Controller $controller the controller that owns this action
  48. * @param array $config name-value pairs that will be used to initialize the object properties
  49. */
  50. public function __construct($id, $controller, $config = [])
  51. {
  52. $this->id = $id;
  53. $this->controller = $controller;
  54. parent::__construct($config);
  55. }
  56. /**
  57. * Returns the unique ID of this action among the whole application.
  58. * @return string the unique ID of this action among the whole application.
  59. */
  60. public function getUniqueId()
  61. {
  62. return $this->controller->getUniqueId() . '/' . $this->id;
  63. }
  64. /**
  65. * Runs this action with the specified parameters.
  66. * This method is mainly invoked by the controller.
  67. * @param array $params the parameters to be bound to the action's run() method.
  68. * @return mixed the result of the action
  69. * @throws InvalidConfigException if the action class does not have a run() method
  70. */
  71. public function runWithParams($params)
  72. {
  73. if (!method_exists($this, 'run')) {
  74. throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
  75. }
  76. $args = $this->controller->bindActionParams($this, $params);
  77. Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
  78. if (Yii::$app->requestedParams === null) {
  79. Yii::$app->requestedParams = $args;
  80. }
  81. return call_user_func_array([$this, 'run'], $args);
  82. }
  83. }