Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Reich Consulting
- *
- * @author Brian Reich <breich@reich-consulting.net>
- * @copyright (C) 2013 Reich Consulting http://www.reich-consulting.net
- * @version 1.0
- */
- namespace ForceHttps
- {
- use Zend\ModuleManager\ModuleManager;
- use Zend\Mvc\MvcEvent;
- /**
- * ForceHttps Module for Zend Framework 2.0
- *
- * This is a drop-in module for Zend Framework 2.0 which forces all traffic
- * through HTTPS. In order to use this module you need to copy this code to
- * <project>/module/ForceHttps/Module.php, then add ''ForceHttps' to the
- * modules config in application.config.php.
- *
- * @namespace ForceHttps
- */
- class Module
- {
- /**
- * Adds listener to "route" event from Zend\Mvc\Application which
- * forces communication through https.
- *
- * @param ModuleManager $moduleManager The ModuleManager
- */
- public function init(ModuleManager $moduleManager)
- {
- $moduleManager->getEventManager()->getSharedManager()->attach(
- 'Zend\Mvc\Application', 'route',
- array($this, 'enforceHttps'), -10
- );
- }
- /**
- * Enforces https.
- *
- * @param MvcEvent $event The event generated from the request.
- *
- * @return \Zend\Http\Response Returns the modified response.
- */
- public function enforceHttps(MvcEvent $event)
- {
- // Get Request object and URI of request
- $request = $event->getRequest();
- $uri = $request->getUri();
- // If scheme isn't https, change URI and redirect.
- if( $uri->getScheme() !== 'https' )
- {
- $uri->setScheme( 'https' ); // Change scheme to https
- $uri->setPort(null); // Let server decide https port
- $response = $event->getResponse();
- $response->getHeaders()->addHeaderLine('Location', $uri->__toString() );
- return $response;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment