S
S
Snewer2016-10-20 23:14:17
Yii
Snewer, 2016-10-20 23:14:17

Why name behaviors in Yii2?

Hello!
Why are behaviors named? Example from yii\filters\AccessControl documentation:

public function behaviors()
{
    return [
        'access' => [
            'class' => \yii\filters\AccessControl::className(),
            'only' => ['create', 'update'],
            'rules' => [
                // deny all POST requests
                [
                    'allow' => false,
                    'verbs' => ['POST']
                ],
                // allow authenticated users
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                // everything else is denied
            ],
        ],
    ];
}

In the documentation I found information only that there are named, and actually not named (anonymous) behaviors and that's all. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-10-21
@Snewer

the use of named behaviors allows you to:
1. Detach the behavior from the component, if necessary, using the detachBehavior function
2. Work with the behavior in descendant classes (for example, change the behavior configuration, etc.)
3. Get the behavior object itself and, if necessary, with it work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question