I am learning implementing restful web service using Yii2. In following controller, I am trying to override the ‘index’ action. But I encountered following error. Anyone can give me some clue? Thanks.
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: api\modules\v1\controllers\CountryController::checkAccess
<?php
namespace api\modules\v1\controllers;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;
class CountryController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Country';
public $serializer = [
'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'items',
];
//-- disable, override or add actions
//-- when overriding default action, make sure current controller has checkAccess() method implemented
public function actions()
{
$actions = parent::actions();
//-- override default actions with actions in this controller
$actions['index'] = [
'class' => $this->className(),
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
return $actions;
}
public function actionIndex()
{
return new ActiveDataProvider(['query' => Country::find()]);
}
public function checkAccess($action, $model = null, $params = [])
{
//--check if the user can access $action & $model
//--throw ForbiddenHttpException if access should be denied
return true;
}
}