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,118 times
1
![](../../users/profiles/6240346.webp)
Mutahir Shah
- 31
- 5
-
This 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.
![](../../users/profiles/1288189.webp)
Phemelo Khetho
- 200
- 2
- 13
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19533149) – Rob Apr 24 '18 at 11:22
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/19533149) – EM-Creations Apr 24 '18 at 12:31