Answer the question
In order to leave comments, you need to log in
Error when upgrading profile Setting unknown property: yii\filters\AccessRule::0?
Hello.
I am learning examples from the book YII2 for Beginners by Bill Keck.
Example from page 207.
There is a user profile, on the profile/view page, which has an Update button. If the user has User_type == "Paid" he is allowed to update the profile ( User_type is stored in the database, ). If "Free" then it should redirect to the upgrade/index.php page.
When a user has a paid profile ( Paid ), then everything works fine. At Free there is an error.
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\filters\AccessRule::0
in F:\OpenServer\domains\yii2build.lc\vendor\yiisoft\yii2\base\Component.php at line 197
public function actionUpdate()
{
PermissionHelpers::requireUpgradeTo("Paid");
if( $model = Profile::find()->where(["user_id" => Yii::$app->user->identity->id])->one()){
if( $model->load( Yii::$app->request->post()) && $model->save()){
return $this->redirect(["view", 'id' => $model->id]);
}
else{
return $this->render("update", [
"model" => $model,
]);
}
}
else{
throw new NotFoundHttpException("No such profile");
}
// $model = $this->findModel($id);
}
public static function requireUpgradeTo($user_type_name)
{
if (!ValueHelpers::userTypeMatch($user_type_name)) {
return Yii::$app->getResponse()->redirect(Url::to(['upgrade/index']));
}
}
public static function userTypeMatch($user_type_name)
{
$userHasUserTypeName = Yii::$app->user->identity->userType->user_type_name;
return $userHasUserTypeName == $user_type_name ? true : false;
}
class UpgradeController extends \yii\web\Controller
{
public function behaviors()
{
return[
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index'],
'rules' => [
'actions' => ['index'],
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action){
return PermissionHelpers::requireStatus('Active');
}
]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
]
]
];
}
public function actionIndex()
{
$name = Profile::find()->where(['user_id' => Yii::$app->user->identity->id])->one();
return $this->render('index', ['name' => $name]);
}
}
Answer the question
In order to leave comments, you need to log in
public function actionUpdate() {
PermissionHelpers::requireUpgradeTo("Paid");
}
you need to wrap this construction in if( PermissionHelpers::requireUpgradeTo("Paid")) { } public function behaviors()
{
return[
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index', 'update'],
'rules' => [
'actions' => ['index', 'update'],
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action){
return PermissionHelpers::requireStatus('Active') && PermissionHelpers::requireUpgradeTo("Paid") ;
}
]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
]
]
];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question