jgn*_*ser 7 php model-view-controller ssl zend-framework
我之前使用特定的安全文件夹(例如服务器上的https文件夹vs http文件夹)实现了安全页面.我已经开始使用Zend Framework,并希望部分应用程序(例如登录)使用https.我在google上搜索过,甚至在这里找不到任何解释如何处理这个问题的内容.我可以为特定控制器/操作设置https吗?谢谢.
Ger*_*sek 13
最干净的方法是为SSL配置提供.ini文件,您可以在其中为模型/控制器/操作级别启用SSL支持,如下所示:
假设您有一个模块/控制器/动作,如下所示:
SSLModule-> IndexController-> testAction
## ini file (can be config.ini also)
ssl.modules.SSLModule.require_ssl = true //-> entire module requires SSL
ssl.modules.SSLModule.Index.require_ssl = true //-> entire controller requires SSL
ssl.modules.SSLModule.Index.test.require_ssl = true //-> single action requires SSL
您可以通过配置或单独解析它,并在您的Bootstrap文件中,您可以包含一个控制器插件,就像我的一样.
还有很多其他方法可以做到这一点,但我认为你明白了!
class Application_Controllerplugins_Ssl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch ( Zend_Controller_Request_Abstract $request )
{
$shouldSecureUrl = false;
//get the config settings for SSL
$options = Application_ServiceManager::getConfig()->ssl;
//if config is empty, exit
if (!is_object($options))
return;
//simpler to use
$options = $options->toArray();
//only use it production environment
if ( APPLICATION_ENV == 'production' )
{
if (
( isset($options['modules'][$request->module]['require_ssl']) && $options['modules'][$request->module]['require_ssl'] ) ||
( isset($options['modules'][$request->module][$request->controller]['require_ssl']) && $options['modules'][$request->module][$request->controller]['require_ssl'] ) ||
( isset($options['modules'][$request->module][$request->controller][$request->action]['require_ssl']) && $options['modules'][$request->module][$request->controller][$request->action]['require_ssl'] )
)
{
$shouldSecureUrl = true;
}
if ( $shouldSecureUrl )
{
$this->_secureUrl($request);
}
}
}
protected function _secureUrl ( Zend_Controller_Request_Abstract $request )
{
$server = $request->getServer();
$hostname = $server['HTTP_HOST'];
if ( ! $request->isSecure() )
{
$url = Zend_Controller_Request_Http::SCHEME_HTTPS . "://" . $hostname .
$request->getPathInfo();
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->setGoToUrl($url);
$redirector->redirectAndExit();
}
}
}
我忘了提到:在你的引导程序中添加它:
$Zend_Controller_Front->registerPlugin( new Application_Controllerplugins_Ssl() );