Action.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\rest;
  8. use yii\base\InvalidConfigException;
  9. use yii\db\ActiveRecordInterface;
  10. use yii\web\NotFoundHttpException;
  11. /**
  12. * Action is the base class for action classes that implement RESTful API.
  13. *
  14. * For more details and usage information on Action, see the [guide article on rest controllers](guide:rest-controllers).
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class Action extends \yii\base\Action
  20. {
  21. /**
  22. * @var string class name of the model which will be handled by this action.
  23. * The model class must implement [[ActiveRecordInterface]].
  24. * This property must be set.
  25. */
  26. public $modelClass;
  27. /**
  28. * @var callable|null a PHP callable that will be called to return the model corresponding
  29. * to the specified primary key value. If not set, [[findModel()]] will be used instead.
  30. * The signature of the callable should be:
  31. *
  32. * ```php
  33. * function ($id, $action) {
  34. * // $id is the primary key value. If composite primary key, the key values
  35. * // will be separated by comma.
  36. * // $action is the action object currently running
  37. * }
  38. * ```
  39. *
  40. * The callable should return the model found, or throw an exception if not found.
  41. */
  42. public $findModel;
  43. /**
  44. * @var callable|null a PHP callable that will be called when running an action to determine
  45. * if the current user has the permission to execute the action. If not set, the access
  46. * check will not be performed. The signature of the callable should be as follows,
  47. *
  48. * ```php
  49. * function ($action, $model = null) {
  50. * // $model is the requested model instance.
  51. * // If null, it means no specific model (e.g. IndexAction)
  52. * }
  53. * ```
  54. */
  55. public $checkAccess;
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function init()
  60. {
  61. if ($this->modelClass === null) {
  62. throw new InvalidConfigException(get_class($this) . '::$modelClass must be set.');
  63. }
  64. }
  65. /**
  66. * Returns the data model based on the primary key given.
  67. * If the data model is not found, a 404 HTTP exception will be raised.
  68. * @param string $id the ID of the model to be loaded. If the model has a composite primary key,
  69. * the ID must be a string of the primary key values separated by commas.
  70. * The order of the primary key values should follow that returned by the `primaryKey()` method
  71. * of the model.
  72. * @return ActiveRecordInterface the model found
  73. * @throws NotFoundHttpException if the model cannot be found
  74. */
  75. public function findModel($id)
  76. {
  77. if ($this->findModel !== null) {
  78. return call_user_func($this->findModel, $id, $this);
  79. }
  80. /* @var $modelClass ActiveRecordInterface */
  81. $modelClass = $this->modelClass;
  82. $keys = $modelClass::primaryKey();
  83. if (count($keys) > 1) {
  84. $values = explode(',', $id);
  85. if (count($keys) === count($values)) {
  86. $model = $modelClass::findOne(array_combine($keys, $values));
  87. }
  88. } elseif ($id !== null) {
  89. $model = $modelClass::findOne($id);
  90. }
  91. if (isset($model)) {
  92. return $model;
  93. }
  94. throw new NotFoundHttpException("Object not found: $id");
  95. }
  96. }