dpi74187 2018-10-10 08:19
浏览 108
已采纳

问:Yii2集成了Rbac和按钮识别功能

I'm developing a website in php with Yii2 and I have a problem with Rbac issue. I've followed the offical guide, I run the migrations and now I have in my db the four default tables which define my roles and permissions. Now I don't know how to integrate these roles in my project, I mean I would like to have some views only visible to users with specific permissions but can't understand the way to implement this.

I have also a problem with login, I don't know how to discriminate a button click.

login (view):

<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button', 'value' => 'login']) ?>
        <?= Html::submitButton('Register', ['class' => 'btn btn-primary', 'name' => 'register-button', 'value' => 'register']) ?>
    </div>
</div>

SiteController:

public function actionLogin()
{
    if (!Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    $model = new LoginForm();
    if (isset($_POST['submit']) && $_POST['submit']=='login') {
        return $this->goBack();
    }
    if (isset($_POST['submit']) && $_POST['submit']=='register') {

        return $this->render('register');
    }
    return $this->render('login', [
        'model' => $model,
    ]);
}

I just need to render in a different views the user after the right button click. If Login button is clicked I want to be redirected in login view, if Register button is clicked, I want to be redirected in register view.

展开全部

  • 写回答

1条回答 默认 最新

  • duanbangzhou7809 2018-10-11 03:48
    关注

    This seems to be a two-in-one question.

    First, RBAC.

    This is explained very well in the docs. You can use AccessControl to only allow certain actions to be accessed by a role or permission. If you need to show some content in a view based on a role or permission, use if(Yii::$app->user->can('permission_or_role)) echo "I can"; (docs).

    Second, buttons

    Check this link, the name of the button must be the name you check for in the controller (not login-button/register-button and check submit).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部