Простая авторизация для сайта
- добавляем в composer.json - "mihaildev/yii2-user": "*"
- замена стандартной компоненты
'components' => [
'user' => [
'class' => 'mihaildev\user\Component',
'enableAutoLogin' => true,
'loginUrl' => ['/site/login'], //'loginUrl' => ['/user/login'],
'userList' => [
'1' => [// id value
'id' => '1',
'username' => 'admin',
'password' => 'admin',
]
],
'authKeySalt' => 'SomeSecreteValue'
],
- добавление в контроллер действий
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions()
{
return [
'login' => [
'class' => 'mihaildev\user\action\LogIn',
//'template' => 'login' // default: @mihaildev/user/views/login.php,
//'defaultUrl' => ['/site/index'],
],
'logout' => 'mihaildev\user\action\LogOut',
];
}
или добавляем контроллер mihaildev\user\Controller
'controllerMap' => [
'user' => [
'class' => 'mihaildev\user\Controller',
//'template' => 'login' // default: @mihaildev/user/views/login.php,
//'defaultUrl' => ['/site/index'],
]