How to block an action or controller without using AccessControl in Yii2?

507 views Asked by At

I am using yii2-basic. I created a controller with init() method in it. This init() method will check some cookies and deny access to all actions if the condition doesn't meet. But I don't know how to do it, could anyone help me? Sorry for bad English.

1

There are 1 answers

0
jmwierzbicki On BEST ANSWER

You can try Using yii\web\CookieCollection - Refeer to http://www.yiiframework.com/doc-2.0/yii-web-cookiecollection.html

Example: to save cookie:

$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
    'name' => 'nameOfCookie',
    'value' => 'oreo',
]));

to retrieve:

  $cookie = $cookies->getValue('nameOfCookie', 'biscuit');
  if($cookie == 'oreo')  throw new \yii\web\ForbiddenHttpException('Insufficient privileges to access this area.');

Enjoy Yii2!