dongyue6199 2014-02-21 11:13
浏览 27
已采纳

如何在用户未登录zfcuser时排除某些路由?

So, i am new in Zend Framework, i installed ZfcUser and i want when the user is not logged to didn't access to some routes for example : /blog/article/add,

actually i use <?php if($this->zfcUserIdentity()) :?> to check if the user is logged but how can i redirect the user to my login route/user if is try to access to /blog/article/add, so plz if someone has any idea i will be very appreciative :)

  • 写回答

2条回答 默认 最新

  • dqz84361326 2014-02-21 12:18
    关注

    Simple Way:

    in controller:

    if (!$this->zfcUserAuthentication()->hasIdentity()) {
        return $this->redirect()->toRoute('route_to_login',array('param' => $param));
    }
    

    Some more: (after auth zfcuset will redirect you to previous controller)

    in module.config.php

    'controllers' => array(
        'invokables' => array(
            'zfcuser' => 'Application\Controller\UserController',
        ),
    ...
    ),
    

    next copy file vendor\zf-commons\zfc-user\src\ZfcUser\Controller\UserController.php to module Application (same folder as in module.config.php)

    delete all function except authenticateAction add: use Zend\Session\Container; and change return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute()); to

    $redirect_session = new Container('redirect');
            $redirect = $redirect_session->redirect;
            if($redirect!='')
            {
                unset($_SESSION['redirect']);
                return $this->redirect()->toRoute($redirect, array('lang' => $lang));
            }
            return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute(),array('param' => $param));
    

    and at last add in controller

    use Zend\Session\Container;
    ...
    if (!$this->zfcUserAuthentication()->hasIdentity()) {
                $redirect_session = new Container('redirect');
                $redirect_session->redirect = 'route_to_this_controller_action';
                return $this->redirect()->toRoute('route_to_login',array('param' => $param));
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分