dpepbjp126917 2014-10-17 12:01
浏览 8

扩展和覆盖ZfcUser行为

I'm using ZfcUser with ht-user-registration and scn-social-auth and there are 2 fairly minor enhancements I want to achieve to extend my implementation, but I'm not having much success so far:

  1. ht-user-registration sends new users an email after registering and denies them access until they've activated their account, this is working fine. What I'd like to do to extend this is redirect the user after registration so that they are sent to a page telling them to check their e-mail rather than to the login page, which is the default behaviour of ZfcUser. I have tried adding an event listener to the module bootstrap that looks like this:

    $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;
    });
    

    This is called correctly but the redirect never happens and the user still ends up at the login page. Is there something else I need to do to execute the redirect? Or maybe there's a better way entirely to achieve this?

  2. I'd like to add layout variables so that I can modify page titles and navigation in my layout template for the ZfcUser pages. In order to do this I made an override of the UserController from ZfcUser like this :

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

    And then overrode the invokable for zfcuser in the config like this:

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

    ),

    The framework tries to instantiate my UserController at the right point but fails with an InvalidArgumentException: 'You must supply a callable redirectCallback' which I can see is required to construct the base controller but doesn't get passed to my overridden version - any clues why not? I can't find a working example of this to help.

    Maybe there's a much easier way to inject layout variables into another module's controller actions?

    [EDIT] I have found a simple but not very elegant way of doing this. Since my module overrides the views for both login and registration then it's possible to set layout variables within the view, thus I was able to add a one liner to the top of each view to set the layout variable, e.g.:

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

    This doesn't feel correct, but works. If there's a better solution I'd like to know.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度