php - 如何在 Zend MVC 中实现 SSL

标签 php model-view-controller zend-framework ssl

我之前通过使用特定的安全文件夹(例如服务器上的 https 文件夹与 http 文件夹)实现了安全页面。我已经开始使用 Zend Framework 并希望应用程序的某些部分(例如登录)使用 https。我在谷歌上搜索过,甚至在这里搜索过,但找不到任何解释如何处理这个问题的内容。我可以为特定的 Controller /操作提供 https 吗?谢谢。

最佳答案

最干净的方法是为 SSL 配置创建一个 .ini 文件,您可以在其中为模型/ Controller /操作级别启用 SSL 支持,如下所示:

假设您有一个像这样的模块/ Controller /操作:
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 文件中,您可以包含一个 Controller 插件,就像我这里的一样。

还有很多其他方法可以做到这一点,但我想您明白了!


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

我忘了提:将它添加到你的 Bootstrap 中:


$Zend_Controller_Front->registerPlugin( new Application_Controllerplugins_Ssl() );

关于php - 如何在 Zend MVC 中实现 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777930/

相关文章:

php - 在 Magento 中,如何在产品页面上显示商品运费?

javascript - window.location和$ location.path有什么区别?

asp.net-mvc - 使用 VB.net 2008 开始 ASP.NET MVC

php - 用于多复选框的 zend 表单从标签中删除输入

javascript - 为什么Jquery ajax要给String添加斜杠?

iphone - 何时 Split View和 Controller

php - 如何在我的 utf-8 编码页面上正确显示 utf 编码字符?

php - 零在PHP中被视为空

php - 我可以通过写入标准输出来调试我的 Laravel 迁移吗?如何?

php - 如何在我现有的 Django Web 应用程序之上集成 wordpress 前端