2016-02-26 115 views
0

我的用戶需要確認他們的電子郵件地址才能訪問該應用程序。ZF2 - BootStrap重定向

我有一個特定的路由,如果他們登錄並且他們的電子郵件沒有被確認:「customer/register-landing」,這將發送一封電子郵件,視圖將解釋他們需要做什麼。

爲簡潔起見,我使用了bootstrap。

這是我迄今爲止最後一點,我努力工作了(再直接參與)

//I run console related queries and this breaks if run 
if ($e->getRequest() instanceof \ZF\ContentNegotiation\Request) 
     { 
      //Get a user object 
      $authService = $sm->get(AuthorizationService::class); 
      $userObject = $authService->getIdentity(); 

      if (!$userObject instanceof User) { 
       return; 
      } 

      if ($userObject->getIsEmailConfirmed() == 1) { 
       return; 
      } 

      //So we have a logged in user who needs to confirm their email 
      $redirect = $em->attach(MvcEvent::EVENT_DISPATCH, 
       function($e){ 
        $route = $e->getRouteMatch(); 

        if ($route->getMatchedRouteName() != 'customer/register-landing') 
        { 
         //Redirect to the route: customer/register-landing 
        } 


       } 
      ); 

     } 

什麼我需要做重新定向到實際的頁面?我周圍一看,我發現這個代碼:

   $em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) { 
        $controller = $e->getTarget(); 
        $controller->plugin('redirect')->toRoute('customer/register-landing'); 

       }, 100); 

然而,當我將它添加到類它不工作:

$redirect = $em->attach(MvcEvent::EVENT_DISPATCH, 
        function($e){ 
         $route = $e->getRouteMatch(); 

         if ($route->getMatchedRouteName() != 'customer/register-landing') 
         { 
               $em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) { 
         $controller = $e->getTarget(); 
         $controller->plugin('redirect')->toRoute('customer/register-landing'); 

        }, 100); 
         } 


        } 
       ); 

什麼是做這種正確的方法是什麼?

回答

1

我用下一個代碼解決了這個問題。如果你需要的所有代碼轉到https://github.com/Gimalca/piderapido/blob/master/module/Admin/Module.php

類模塊{

public function onBootstrap(MvcEvent $e) { 

} 

public function init(ModuleManager $moduleManager) { 
    $moduleName = $moduleManager->getEvent()->getModuleName(); 
    if ($moduleName == 'Admin') { 
     $events = $moduleManager->getEventManager(); 
     $sharedEvents = $events->getSharedManager(); 
     // This define modules need Login 
     $sharedEvents->attach(array(__NAMESPACE__, 'Admin', 'Account'), 'dispatch', array($this, 'initAuth'), 100); 
    } 
} 

public function initAuth(MvcEvent $e) { 

    //This get router strings 
    $routerMatch = $e->getRouteMatch(); 
    $module = $routerMatch->getMatchedRouteName(); 
    $controller = $routerMatch->getParam('controller'); 
    $action = $routerMatch->getParam('action'); 

    //This get Authenticate Class 
    $app = $e->getApplication(); 
    $sm = $app->getServiceManager(); 
    $auth = $sm->get('Admin\Model\LoginAdmin'); 

    // This redirect all. but is login interface not 
    if ($controller != 'Admin\Controller\Login' && !$auth->isLoggedIn()) { 
     $controller = $e->getTarget(); 

     return $controller->redirect()->toRoute('admin',array('controller'=>'login','action' => 'index')); 
    } 


    if ($auth->isLoggedIn()) { 

     $viewModel = $e->getViewModel(); 
     $viewModel->userIdentity = $auth->getIdentity(); 
    } 
}