PHP code example of antonyz89 / yii2-rbac

1. Go to this page and download the library: Download antonyz89/yii2-rbac library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antonyz89 / yii2-rbac example snippets


return [
    'bootstrap' => ['rbac'],
    'modules' => [
        'rbac' => ['class' => 'antonyz89\rbac\Module'],
    ],
]

use antonyz89\rbac\components\AccessControl;

/**
 * ExampleController implements the CRUD actions for Example model.
 */
class ExampleController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class, /* add */
                /* optional fields */
                'rules' => [
                    [
                        'actions' => [
                            'create',
                            'update',
                            'view',
                            'index',
                        ],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
}



use yii\db\Migration;

/**
 * Class m200808_033148_add_rbac_profile_id_to_user_table
 */
class m200808_033148_add_rbac_profile_id_to_user_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->addColumn('{{%user}}', 'rbac_profile_id', $this->integer()->notNull()->after('id'));
        $this->createIndex('idx-user-rbac_profile_id', '{{%user}}', 'rbac_profile_id');
        $this->addForeignKey('fk-user-rbac_profile_id', '{{%user}}', 'rbac_profile_id', '{{%rbac_profile}}', 'id', 'CASCADE', 'CASCADE');
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropForeignKey('fk-user-rbac_profile_id', '{{%user}}');
        $this->dropIndex('idx-user-rbac_profile_id', '{{%user}}');
        $this->dropColumn('{{%user}}', 'rbac_profile_id');
    }
}

    /**
     * @return \yii\db\ActiveQuery|\antonyz89\rbac\models\query\RbacProfileQuery
     */
    public function getRbacProfile()
    {
        return $this->hasOne(\antonyz89\rbac\models\RbacProfile::class, ['id' => 'rbac_profile_id']);
    }

//rbac_profile_id
['rbac_profile_id', '_profile_id', 'exist', 'skipOnError' => true, 'targetClass' => RbacProfile::class, 'targetAttribute' => ['rbac_profile_id' => 'id']],




use antonyz89\rbac\widgets\Menu;