2014-09-20 74 views

回答

0

搜索很多後,我找到了解決方案。只要更新你的應用程序的Module.php如下:

public function onBootstrap(MvcEvent $e) 
{ 
    $eventManager = $e->getApplication()->getEventManager(); 

    $moduleRouteListener = new ModuleRouteListener(); 
    $moduleRouteListener->attach($eventManager); 


    $eventManager->attach('route', array($this, 'doHttpsRedirect')); 

    // your code 
} 

,並添加功能doHttpsRedirect,如:

public function doHttpsRedirect(MvcEvent $e) 
    { 
     $sm = $e->getApplication()->getServiceManager(); 
     $uri = $e->getRequest()->getUri(); 
     $controller = $e->getRouteMatch()->getParam('controller'); 
     $action = $e->getRouteMatch()->getParam('action'); 
     $securedPages = array('Application\Controller\Login', 'Application\Controller\Register', 'Application\Controller\shopping'); 

     if (in_array($controller, $securedPages)) { 
      $scheme = $uri->getScheme(); 
      if ($scheme != 'https') { 
       $uri->setScheme('https'); 
       $response = $e->getResponse(); 
       $response->getHeaders()->addHeaderLine('Location', $uri); 
       $response->setStatusCode(302); 
       $response->sendHeaders(); 
       return $response; 
      } 
     } else if (!in_array($controller, $securedPages)) { 
      if ($_SERVER["HTTPS"] == "on") { 
       $uri->setScheme('http'); 
       $response = $e->getResponse(); 
       $response->getHeaders()->addHeaderLine('Location', $uri); 
       $response->setStatusCode(302); 
       $response->sendHeaders(); 
       return $response; 
      } 
     } 
    } 

這一切..!

相關問題