首页 > 解决方案 > 转动重定向ON和OFF?

问题描述

我有一个带有 200 多个<cflocation>标签的旧式 ColdFusion 系统,并且希望能够轻松地从我的Application.cfm.

除了必须向每个文件添加条件语句之外,最好的方法是什么?

想到扩展<cflocation>标签以首先检查true/false标志的想法出现了,但我以前从未做过任何高级的事情,也不知道从哪里开始。

标签: coldfusioncfmllucee

解决方案


您不能在 CF 中扩展/继承本机标签。您可以使用自定义标记,然后将所有出现的<cflocation(native)搜索替换为<cf_location(custom),其中包含您的重定向标志。

应用程序.cfm

<cfset APPLICATION.allowRedirects = false>

location.cfm(本机的包装器cflocation

<!--- if the variable doesn't exist for some reason, process the regular cflocation --->
<cfif (
    (not isDefined("APPLICATION.allowRedirects")) or
    (isBoolean(APPLICATION.allowRedirects) and APPLICATION.allowRedirects)
)>
    <cflocation attributeCollection="#ATTRIBUTES#">
    <!--- cflocation ends the request automatically --->
</cfif>

<!--- print debug info --->
<cfif structKeyExists(ATTRIBUTES, "url")>
    <cfoutput>Attempted to redirect to: #encodeForHtml(ATTRIBUTES["url"])#</cfoutput>
</cfif>

<!--- abort the request without redirecting --->
<cfabort>

另一种方法是<cfmodule>. 搜索替换所有出现的<cflocationwith <cfmodule template="/mytags/location.cfm"。这里的优势:您不需要location.cfm通过模板路径customTagPaths进行注册。cfmodule


更简单:只需一次搜索替换所有<cflocation>标签并将它们包装在条件中。您不必手动检查 200 个文件。

(这适用于 Sublime Text,但任何适当的编辑器都可以为您执行此操作。)
查找:((<cflocation[^>]+>)启用正则表达式!)
替换:(<cfif APPLICATION.allowRedirects>\1</cfif>
\1反向引用,一些编辑器使用$1


推荐阅读