0

所以我看到了关于如何在 Yii 1 中设置默认路由的帖子,其中初始页面是登录页面,但没有关于如何在 Yii 2 中执行此操作的帖子。

我需要的是所有用户首先登录,然后才能使用 CRUD 功能,其中一些用户能够比其他用户做更多的事情。

供您参考,我使用的是基本模板。

4

2 回答 2

2

在 basic\config\web.php 中尝试

在组件中添加 'loginUrl' => ['user/login'],到用户

'components' => [
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => '',
    ],
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
    'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => true,
        'loginUrl' => ['user/login'],
    ],

// ...

]

于 2015-06-23T21:44:55.100 回答
1

只是想让解决方案在这里可见。发现于https://www.yiiframework.com/forum/index.php/topic/54255-newbies-question-to-yii2-how-can-i-force-user-to-login/由 vishwasrao 和类似的帖子在这里Yii2 全局过滤器/行为强制用户首先通过 jagsler 进行身份验证。

对于基本模板,在 config/web.php 中,添加以下“作为访问”部分:

 'components' => [ ... ],
 'as access' => [
    'class' => \yii\filters\AccessControl::className(),//AccessControl::className(),
    'rules' => [
        [
            'actions' => ['login', 'error'],
            'allow' => true,
        ],
        [
            'actions' => ['logout', 'index'], // add all actions to take guest to login page
            'allow' => true,
            'roles' => ['@'],
        ],
    ],
 ],
 'params' => $params,

希望这可以帮助任何仍在寻找这个的人。

于 2018-07-31T08:25:25.090 回答