dongren1353 2016-03-04 19:23
浏览 132
已采纳

如何只访问一个特定的页面YII2(高级模板)?

I have a special case for the user in my project. if user's subscription is expired (I check this in login action), he will be redirected to his profile to edit some choices. How can I prevent him from access any page except his profile. This is my code in login action

 if($subPaymentType == 'free'){
                    $subHours = $data[0]['sub_hours'];

                    $minutes = $subHours * 60 * 60;
                    $start_time = date('d-m-Y H:i:s', $startDate);
                    $endDate = $minutes + strtotime($start_time);
                    $endDate = date('d-m-Y H:i:s', $endDate);

                    if(strtotime(date('d-m-Y:')) < strtotime($endDate)){
                        $model->login();
                    }else{
                        $model->login();
                        //User can access this only page only
                        return $this->redirect(['user/view/?id='.Yii::$app->user->id]);

                    }

                }
  • 写回答

1条回答 默认 最新

  • dreljie602951 2016-03-04 19:58
    关注

    You can use use AccessControl in your controller

    yii\filters\AccessControl;
    
    
    
    class YourSiteController extends Controller
    {
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' => ['login','profile'],
                            'allow' => true,
                            'roles' => ['*'],
                        ],
                        // allow authenticated users
                        [
                            'allow' => true,
                            'roles' => ['@'],
                        ],   
                    ],
                ],         
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['post'],
                    ],
                ],
            ];
        }
    

    see this guide for more http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html

    and in the related actionView

    public function actionView($id)
    {
       if ($id != Yii::$app->user->id){
         // not allowed  ... perform the action you need in this case 
       } else {
          return $this->render('view', [
              'model' => $this->findModel($id),
          ]);
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题