2013-04-03 49 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