0

在 Yii2 高级应用程序中,我试图只允许用户访问登录页面和关于页面。

所以我在 /common/config/web.php 配置文件中放入以下规则

    'as beforeRequest' => [  //if guest user access site so, redirect to login page.
    'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'actions' => ['login', 'error', 'request-password-reset', 'about'],
            'allow' => true,
        ],
        [
            'allow' => true,
            'roles' => ['@'],
        ],
    ],
], 

登录,错误和请求密码重置它工作正常,但不是关于页面。

我也试过'/page/about'和'page/about'但我没有运气。

任何想法如何解决或解决此问题。

谢谢

4

2 回答 2

0

它应该是“视图”而不是“关于”

很抱歉,我找到了答案,规则很好,但我正在使用页面组件和 PageController,它正在从以下控制器中的管理员(数据库)获取视图

class PageController extends Controller{
public function actionView($slug)
{
    $model = Page::find()->where(['slug'=>$slug, 'status'=>Page::STATUS_PUBLISHED])->one();
    if (!$model) {
        throw new NotFoundHttpException(Yii::t('frontend', 'Page not found'));
    }

    $viewFile = $model->view ?: 'view';
    return $this->render($viewFile, ['model'=>$model]);
}}

因此,在规则中,我需要对 PageController 呈现的所有视图使用“视图”

于 2017-01-25T13:07:20.700 回答
0

试试这个代码

'as beforeRequest' => [  //if guest user access site so, redirect to login page.


'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'actions' => ['login', 'error', 'request-password-reset', 'about'],
            'roles' => ['@']
            'allow' => true,
        ],
        [
            'allow' => true,
            'roles' => ['?'],
        ],
    ],
],

我希望它对你有用

于 2017-01-25T13:14:24.110 回答