Hi developers I am new to YII , I have installed the YII2 framework and want a RBAC I have installed mdmsoft/yii2-admin module , but I do not know how to create RULE class, where to create it and then how to use. When I create a role in admin section it say , enter a Class Name. I don't know how to create and use the RULE feature of YII. I have attached the screen shot.
Asked
Active
Viewed 1,375 times
1

Mutahir Shah
- 41
- 6
-
1This video tutorial about yii2-admin module will help you: https://www.youtube.com/watch?v=vLb8YATO-HU&t=1s – Kostas Mitsarakis Dec 03 '16 at 21:37
-
okay thanks for your reply. – Mutahir Shah Dec 19 '16 at 10:44
1 Answers
0
If you are using an Advanced template here are the steps;
- Create a directory under
frontend
and rename itrbac
- Create a file under this new directory, say,
AuthorRule.php
. Here is a sample file given in the official docs;
namespace app\rbac; use yii\rbac\Rule; use app\models\Post; /** * Checks if authorID matches user passed via params */ class AuthorRule extends Rule { public $name = 'isAuthor'; /** * @param string|int $user the user ID. * @param Item $item the role or permission that this rule is associated with * @param array $params parameters passed to ManagerInterface::checkAccess(). * @return bool a value indicating whether the rule permits the role or permission it is associated with. */ public function execute($user, $item, $params) { return isset($params['post']) ? $params['post']->createdBy == $user : false; } }
- Next step is to navigate to
http://localhost/path/to/index.php?r=admin/rule
and create a new rule with class name\app\rbac\AuthorRule
- Finally you can add the new rule to
roles
andpermissions
depending on your needs.
You can read the official docs for more information on rules; the official docs.

Phemelo Khetho
- 231
- 1
- 4
- 14