扩展和扩展覆盖ZfcUser行为

时间:2014-10-17 12:01:31

标签: php zend-framework2 user-registration zfcuser

我正在使用ZfcUser和ht-user-registration以及scn-social-auth,我希望实现2个相当小的增强功能来扩展我的实现,但到目前为止我没有取得多大成功:

  1. ht-user-registration在注册后向新用户发送电子邮件,并拒绝他们访问,直到他们激活了他们的帐户为止,这样做很好。我想要做的是扩展这个是在注册后重定向用户,以便将它们发送到一个页面,告诉他们检查他们的电子邮件而不是登录页面,这是ZfcUser的默认行为。我尝试将一个事件监听器添加到模块引导程序中,如下所示:

    $response = $e->getResponse();
    // indicate that we intend to redirect after register action
    // set the redirection location to the home page
    $response->getHeaders()->addHeaderLine('Location', 'home');
    $response->setStatusCode(302);
    $response->sendHeaders();
    
    $em = \Zend\EventManager\StaticEventManager::getInstance();
    $em->attach('ZfcUser\Service\User', 'register', function($event) use ($response) {
        // don't allow anything else to process this event
        $event->stopPropagation();
        // return the redirect response
        return $response;
    });
    

    这被正确调用,但重定向从未发生,用户仍然在登录页面结束。我还需要做些什么来执行重定向吗?或者可能有更好的方法来实现这一目标?

  2. 我想添加布局变量,以便我可以在ZfcUser页面的布局模板中修改页面标题和导航。为了做到这一点,我从ZfcUser中重写了UserController,如下所示:

    class UserController extends \ZfcUser\Controller\UserController
    {
        public function loginAction()
        {
            $this->layout()->setVariables(array(
                'view_title'         => 'Reports Login',
            ));
            return parent::loginAction();
        }
    }
    

    然后在配置中覆盖zfcuser的invokable,如下所示:

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

    ),

    框架尝试在正确的位置实例化我的UserController,但失败并出现InvalidArgumentException:'您必须提供可调用的redirectCallback'我可以看到需要构建基本控制器,但没有传递给我的重写版本 - 任何线索为什么不呢?我无法找到一个有用的例子来帮助你。

    也许有更简单的方法将布局变量注入另一个模块的控制器动作?

    [编辑]我发现了一种简单但不太优雅的方法。由于我的模块会覆盖登录和注册的视图,因此可以在视图中设置布局变量,因此我可以在每个视图的顶部添加一个衬垫来设置布局变量,例如:< /强>

    <?php $this->layout()->setVariable('view_title', 'Register for an account'); ?>
    

    这感觉不正确,但有效。如果有更好的解决方案,我想知道。

0 个答案:

没有答案