Action.php 3.4 KB

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