2013-04-03 66 views
4

我们在我们的控制器中使用Zend Framework 2并使用toRoute来重定向到各个位置,例如$this->redirect()->toRoute('home');ZF2 toRoute with https

无论如何有这个重定向到https而不是http使用这种方法或替代方法?

谢谢!

+0

如果你看一下'Redirect'控制器插件的[toRoute](https://github.com/zendframework/zf2/blob/master/library/Zend /Mvc/Controller/Plugin/Redirect.php#L36-L52)方法,你可以看到它需要一个'$ options'数组并且调用'Url'控制器插件的[fromRoute](https://github.com/zendframework /zf2/blob/master/library/Zend/Mvc/Controller/Plugin/Url.php#L33)方法来构建URL。我怀疑你可以传递一个选项来做到这一点(可能与'url'视图助手相同)。我现在没有时间去调查,所以我没有做太多的挖掘工作。也许它可以帮助你 – Andy0708 2013-04-03 20:50:32

回答

1

为了在您的路线中使用https您需要使用Zend\Mvc\Router\Http\Scheme路由器。指定此类路由的配置与其他路由没有太大区别。您需要将路由类型指定为Scheme,并在module.config.php的路由器配置中添加选项'scheme' => 'https'

下面是一个例子:

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

如果你有路线routename如上配置,这样的:$this->redirect()->toRoute('routename');会工作。请参考this参考ZF2的手册。

希望这有助于:)

斯托扬

+0

我的方案路线的问题是,它基本上是一个字面上的路线,似乎非常不方便。 http://framework.zend.com/manual/2.2/en/modules/zend.mvc.routing.html#zend-mvc-router-http-scheme下面的文章似乎提供了一个更灵活的解决方案,而不必忍受Scheme的局限性:http://stackoverflow.com/questions/18141140/zf2-and-force-https-for-specific-routes。 – 2014-02-24 18:35:52