Blessy
Blessy

Reputation: 520

yii2.0 gives 403 error while running http://localhost/~name/yii2hello/backend/web/index.php?r=site/say

I'm new to yii. With much difficulty i set up yii2.0 advanced and tried to run the simple hello program.

I added the following code in backend/Controllers/SiteController.php

public function actionSay($message = 'Hello')
{
    return $this->render('say',['message' => $message]);
}

Then, I created say.php file in backend/views/site/ with the following code

<?php
    use yii\helpers\Html;
?>
<?= Html::encode($message) ?>

I typed ../yii2hello/backend/web/index.php?r=site/say&message=Hello+World in browser to access the page.

I got the 403 error "you are not allowed to perform this action."

Can anyone tell me where is the problem ?

Upvotes: 2

Views: 1987

Answers (1)

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

This error happens when you don't define permissions to access this action.
You have to add the permission to your action like the following:

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['say'],
                    'allow' => true,
                ],
            ],
        ]
    ];
}

Upvotes: 5

Related Questions