ZF2 toRoute with https

2019-04-10 09:22发布

问题:

We're using Zend Framework 2 and use toRoute within our controllers to redirect to various locations, for example $this->redirect()->toRoute('home');.

Is there anyway to have this redirect to https instead of http using this method or an alternative method?

Thank you!

回答1:

In order to use https in your route you need to use the Zend\Mvc\Router\Http\Scheme router. Specifying the configuration for such route is not very different from the other routes. You need to specify the route type as Scheme and add an option 'scheme' => 'https' in your router configuration in module.config.php.

Here is an example:

return array(
    'router' => array(
        'routes' => array(
            'routename' => array(
                'type' => 'Scheme', // <- This is important
                'options' => array(
                    'route' => '/url',
                    'scheme' => 'https', // <- and this.
                    'defaults' => array(
                        '__NAMESPACE__' => 'MdlNamespace\Controller',
                        'controller' => 'Index',
                        'action' => 'someAction',
                    ),
                ),
            ),
            // the rest of the routes
        ),
    ),
    // the rest of the module config
);

If you have the route routename configured like above, this: $this->redirect()->toRoute('routename'); will work.

See this for reference to the ZF2's manual.

Hope this helps :)

Stoyan