magento中生成https链接的简单方法

2023-05-16

有关magento中https的基础知识,请看 《magento中的启用https》

如果是在项目的后期才决定采用https,那么就要面临一个问题:大量的生成url的代码需要修改,这是一个很大的工作量。


我们先来总结一下magento下生成url的方法:

1. 从全局上看,有两个方法:

Mage::getUrl('xx/xxx/xxxx'  , array( '_secure' => true ))

Mage::getBaseUrl( Mage_Core_Model_Store::URL_TYPE_LINK, true )


其中Mage::getUrl()如下:

    public static function getUrl($route = '', $params = array())
    {
        return self::getModel('core/url')->getUrl($route, $params);
    }



2. 在controller中,有redirect方法:

    protected function _redirect($path, $arguments=array())
    {
        $this->getResponse()->setRedirect(Mage::getUrl($path, $arguments));
        return $this;
    }

3. 在template中,有 getUrl 方法:

    /**
     * Returns url model class name
     *
     * @return string
     */
    protected function _getUrlModelClass()
    {
        return 'core/url';
    }

    /**
     * Create and return url object
     *
     * @return Mage_Core_Model_Url
     */
    protected function _getUrlModel()
    {
        return Mage::getModel($this->_getUrlModelClass());
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = array())
    {
        return $this->_getUrlModel()->getUrl($route, $params);
    }

        由以上的1,2,3点可知,系统在生成url的方法绝大多数是调用Mage_Core_Model_Url::getUrl() 方法( 其中Mage::getBaseUrl()没法rewrite,只能一个个改,如果开发中允许覆盖核心代码的话,那就直接修改getBaseUrl ), 于是我们能得到一个简便的方法就是重写Mage_Core_Model_Url::getUrl() ,把使用https参数加进来,重写后的方法如下:


   /**
     * Build url by requested path and parameters
     *
     * @param   string|null $routePath
     * @param   array|null $routeParams
     * @return  string
     */
    public function getUrl($routePath = null, $routeParams = null)
    {

        /*
         use https
         if url show in web browser start with https, all urls in the webpage must be start with https,
         for example: if main page is https, but ajax request send in this page is http, then ajax request will fail
                      if main page is http, but ajax request send in this page is https, then ajax request will fail also
            
        */
		//use https
		if( isset($_SERVER['SERVER_PORT']) && 443==$_SERVER['SERVER_PORT'] ){
		    //if it has set $routeParams,then add  '_secure' => true
		    if( is_array($routeParams) ){
			if( !isset($routeParams['_secure']) ){
				$routeParams['_secure']=true;
			}
			
		    }else{       	
			//set $routeParams to use https
			$routeParams=array('_secure'=>true);
		    }
		}
     
        return parent::getUrl($routePath,$routeParams);
      
    }





这个功能已变成开源module,地址  https://github.com/newjueqi/usehttps

模块就是为了解决同一个页面中混杂着http和https的问题,特别是使用ajax时,如果当前的url是https,但request的url是http,那么ajax请求会失败。
如果当前页面是http,那么ajax的请求必须是http。
如果当前页面是https,那么ajax的请求必须是https。



【文章作者】曾健生

【作者邮箱】zengjiansheng1@126.com

【作者QQ】190678908

【作者博客】blog.csdn.net/newjueqi




本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

magento中生成https链接的简单方法 的相关文章

  • 如何使用 JKS 证书进行 NODE https 客户端请求

    我想在 NodeJS 应用程序中使用来自 JKS 密钥库的证书 var fs require fs var https require https var options hostname XXX com port 4443 path en
  • Nancyfx 通过 HTTPS 自托管

    我尝试以自托管模式启动 nancyfx 网络服务器 执行以下代码时一切正常 public static void Main string args var hostConfig new HostConfiguration UrlReserv
  • Magento Rest API - oAuth 错误

    我是第一次使用 Magento Rest API 我浏览了 Rest API 的教程http www magentocommerce com api rest并尝试了一个通过 API 获取产品的示例 callbackUrl http loc
  • Magento,翻译验证错误消息

    我已经成功创建了原型验证的新规则 现在我需要翻译错误消息 位置 Javascript 中的字符串 但是 我只能翻译所有消息 我的新自定义消息似乎无法翻译 我该如何改变这个 也许你需要一个jstranslator xml里面的文件etc fo
  • 如何获取magento中登录客户的订单列表

    我正在努力获取客户订购的订单号 名称 列表 我尝试过使用 Mage getModel sales order gt load order id 但对我不起作用 实际上 我正在开发帮助台模块并尝试将订单分配给票证 好的朋友 感谢您的提示 我通
  • htaccess 重定向非 www http 和 https

    我想要 http example com重定向至 http www example com https example com重定向至 https www example com 以及任何东西http whatever example co
  • 您常见的 Magento 配置错误有哪些? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 当建立 TLS / LDAP 或 TLS / HTTP 连接时,线路上会发生什么?

    我正在重新表述我的问题 希望能得到更好的答复 我问了类似的问题这里服务器故障 https serverfault com questions 178561 what are the exact protocol level differen
  • 仅适用于安全页面的安全回形针 URL

    我正在尝试找到使回形针网址安全的最佳方法 但仅限于安全页面 例如 显示存储在 S3 中的图像的主页是http mydomain com http mydomain com图像网址是http s3 amazonaws com mydomain
  • magento 删除管理菜单项

    在magento中 是否可以删除管理中的菜单项 我有一个新模块 并将评论和评级拉到一个新部分 我不反对将它们也留在目录部分 但如果可能 并且干净 我想将其从那里删除 将下一行添加到扩展 config xml 文件
  • 使用 https 的 Java Jersey RESTful Web 服务

    我是 Java EE 的新手 正在开发一个 RESTful API 其中每个 API 调用用户都会发送编码的凭据 我的问题是如何通过默认的 http 实现 https 协议并确保我的连接安全 我正在使用 Jersey Restful Web
  • 根据产品属性在 Magento 中创建购物车规则

    我在一个类别中有产品 针 有些以 100 支为一包出售 有些以 500 支为一包出售 盒子中的针数被设置为产品属性 我想根据购物车中的针总数应用购物车规则 F x 如果您购买 1000 2000 根针头 无论 500 100 包的组合如何
  • 使用普通用户和 https 的 gitea

    我正在尝试设置 gitea 以使用 https 和我从 LetsEncrypt 获得的证书 运行该服务作为普通用户 我已经让它与普通用户在端口 80 上使用 http 一起工作git并使用 iptables 将端口 80 重定向到端口 30
  • 如何禁用 HTTP 的 HSTS 标头?

    我已将以下内容插入到我网站的 htaccess 中 以便能够访问HSTS预加载列表 https hstspreload appspot com
  • 查询magento limit + order by rand()

    function getIdModelsSliderJuwels collection Mage getModel catalog product gt getCollection collection gt addAttributeToF
  • Django HTTPS 和 HTTP 会话

    我使用 Django 1 1 1 和 ssl 重定向中间件 通过 HTTPS 创建的会话数据 身份验证等 在站点的 HTTP 部分中不可用 无需将整个站点设置为 HTTPS 即可使其可用的最佳方法是什么 这是设计使然 您无法轻易更改 当通过
  • IIS 8 HTTPS/需要 SSL 导致超时错误

    尝试通过 IIS 8 通过 SSL 发布网站 但出现超时错误 任何帮助表示赞赏 采取的步骤 已验证该网站可以通过 HTTP 访问 http xxx xxx xxx xxx有效 此时使用 IP 地址 如果重要的话 IIS gt 服务器证书 g
  • Magento 中的子域 htaccess 问题

    public html www domain com public html subdomain subdomain domain com public html htaccess public html subdomain htacces
  • Magento - 将特定父类别的子类别列为链接

    我是 php 的初学者 并且一直试图将一个父类别的子类别作为链接调用 我得到了这个 它调出了 getName 但 getUrl 根本没有返回任何 URL 输出代码只是 li a href name of sub a li
  • 如何在android网络库(ION)中使用自签名SSL?

    使用此网络库 https github com koush ion https github com koush ion 由于当前状态是开发 我想使用自签名 SSL 证书 图书馆论坛有一些讨论 https github com koush

随机推荐

  • 配置magento后台翻译

    同事在magento中添加了若干翻译 xff0c 但总是没法再后台显示 xff0c 我帮忙查了很久 xff0c 终于找到问题所在了 xff0c 原来没有配置对应module的后台翻译文件 xff0c 所以在后台显示不出翻译 这里贡献一份前后
  • apache安装新模块的方法

    1 检查是否安装 a 编译安装http bin apachectl l grep mod expires b 如果是以DSO方式编译的 xff0c 则查看 ll modules grep expires 注意 xff0c 以上两种情况不能同
  • prototype.js中hide()和show()的一个注意事项

    在项目中遇到一个问题 xff0c 用prototype js的show xff08 xff09 方法要设置某个div为display block 但总是没法成功 xff0c 后来查了手册 xff1a http api prototypejs
  • doxygentoolkit.vim 用法

    http blog chinaunix net space php uid 61 20570759 amp do 61 blog amp id 61 1922274 早就安上了这个东西 xff0c 只是一直没研究它怎么用 因为 emacs
  • 一些vim知识的摘录

    h function list 看内置函数 b name variable local to a buffer w name variable local to a window g name global variable also in
  • 为什么 Vim 使用 HJKL 键作为方向键

    出处 xff1a http blog jobbole com 18650 导读 xff1a 关于这个问题 xff0c 以前网络上有一种说法 xff0c 手指放在键盘上输入时 xff0c HJKL 比方向键距离手指更近 xff0c 自然输入效
  • 蚂蚁变大象:浅谈常规网站是如何从小变大的(一)

    http zgwangbo blog 51cto com 4977613 849529 标签 xff1a 架构 web 原创作品 xff0c 允许转载 xff0c 转载时请务必以超链接形式标明文章 原始出处 作者信息和本声明 否则将追究法律
  • 【转】高效使用vim

    出处 xff1a http www cnblogs com hyddd archive 2010 04 08 1706863 html 英文出处 xff1a jmcpherson org editing html 翻译引用 xff1a ti
  • 浅谈HTTP中Get与Post的区别

    http www cnblogs com hyddd archive 2009 03 31 1426026 html Http定义了与服务器交互的不同方法 xff0c 最基本的方法有4种 xff0c 分别是GET xff0c POST xf
  • PID算法搞不懂?看这篇文章就够了。

    点击上方 大鱼机器人 xff0c 选择 置顶 星标公众号 福利干货 xff0c 第一时间送达 xff01 转自知乎 xff1a jason 原文链接 xff1a https zhuanlan zhihu com p 74131690 1 目
  • Http Message结构学习总结

    http www cnblogs com hyddd archive 2009 04 19 1438971 html 最近做的东西需要更深入地了解Http协议 xff0c 故死磕了一下RFC2616 xff0d HTTP 1 1协议 xff
  • 浅析数字证书

    出处 xff1a http www cnblogs com hyddd archive 2009 01 07 1371292 html hyddd原创 xff0c 转载请说明出处 gt 最近看会Session hijack的东西 xff0c
  • Cookie小记

    出处 xff1a http www cnblogs com hyddd archive 2008 12 26 1363229 html 最近在工作上经常看一些安全相关的东西 xff0c Cookie以前看过 xff0c 但了解不深 xff0
  • Session小记

    出处 xff1a http www cnblogs com hyddd archive 2008 12 29 1364646 html 看了一些Session的资料 xff0c 在这里再总结一下Session相关的知识 很多是从网上搜集的资
  • 点击<a href="#"/>后屏幕滚动问题

    问 xff1a 当 lt a href 61 34 34 gt 点击后屏幕会滚动到最上面 xff0c 有啥办法不让屏幕滚动 xff1f 答 xff1a href 61 34 javascript void 0 34 或 nclick 61
  • 内存管理知识

    原创作品 xff0c 允许转载 xff0c 转载时请务必以超链接形式标明文章 原始出处 作者信息和本声明 否则将追究法律责任 http xqtesting blog 51cto com 4626073 808548 一般的程序语言 xff0
  • 用户体验:别让我想,别让我停!

    http xqtesting blog 51cto com 4626073 813561 在交互设计中 xff0c 存在着几条普遍的法则令网页设计更有效 最重要的一条是 别让我思考 xff0c 越简洁越好 比如不要因为奇怪的表达方式强迫用户
  • MySQL慢查询的两种分析方案 slow sql

    http blog csdn net ylqmf article details 6541542 前一段日子 xff0c 我曾经设置了一次记录在MySQL数据库中对慢于1秒钟的SQL语句进行查询 想起来有几个十分设置的方法 xff0c 有几
  • 如何使用SQL Profiler 性能分析器

    http blog csdn net ylqmf article details 6541625 ysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况 分析器可以更好的展示出不良 SQL 的性能问题所在
  • magento中生成https链接的简单方法

    有关magento中https的基础知识 xff0c 请看 magento中的启用https 如果是在项目的后期才决定采用https xff0c 那么就要面临一个问题 xff1a 大量的生成url的代码需要修改 xff0c 这是一个很大的工