2012-04-02 28 views
2

我們已經在我們的Application.cfc下面的代碼:ColdFusion的「套路不能多次聲明」

<cffunction name="onError" returnType="void" output="false"> 
    <cfargument name="exception" required="true"> 
    <cfargument name="eventname" type="string" required="true"> 
    <cfset cfcatch = exception> 
    <cfinclude template="standalone/errors/error.cfm"> 
</cffunction> 

內,我們有這樣的代碼(我沒有寫吧)error.cfm頁:

<cfscript> 
     function GetCurrentURL() { 
      var theURL = "http"; 
      if (cgi.https EQ "on") theURL = "#TheURL#s"; 
      theURL = theURL & "://#cgi.server_name#"; 
      if(cgi.server_port neq 80) theURL = theURL & ":#cgi.server_port#"; 
      theURL = theURL & "#cgi.path_info#"; 
      if(len(cgi.query_string)) theURL = theURL & "?#cgi.query_string#"; 
      return theURL; 
     } 
</cfscript> 

這是一個腳本的所有部分,它將有關錯誤的詳細信息放在一起並將其記錄到數據庫中。

發生錯誤時,我們收到消息「例程GetCurrentURL已在不同模板中聲明瞭兩次」。但是,我以幾種不同的方式搜索了整個代碼庫,發現「GetCurrentURL」只用了兩次,兩次都在error.cfm中。第一次是聲明,第二次是實際使用。所以我不確定CF爲什麼說「在不同的模板中」。

我的下一個想法是,這個問題是一個遞歸調用,這是error.cfm示數,並自稱,所以我嘗試這兩個變化,其中任何一個應該已經解決了這個問題,並揭露真正的錯誤:

<cfif StructKeyExists(variables,"GetCurrentURL") IS "NO"> 
    <cfscript> 
      function GetCurrentURL() { 
       var theURL = "http"; 
       if (cgi.https EQ "on") theURL = "#TheURL#s"; 
       theURL = theURL & "://#cgi.server_name#"; 
       if(cgi.server_port neq 80) theURL = theURL & ":#cgi.server_port#"; 
       theURL = theURL & "#cgi.path_info#"; 
       if(len(cgi.query_string)) theURL = theURL & "?#cgi.query_string#"; 
       return theURL; 
      } 
    </cfscript> 
</cfif> 

和:

<cfscript> 
    if (!StructKeyExists(variables,"GetCurrentURL")) { 
      function GetCurrentURL() { 
       var theURL = "http"; 
       if (cgi.https EQ "on") theURL = "#TheURL#s"; 
       theURL = theURL & "://#cgi.server_name#"; 
       if(cgi.server_port neq 80) theURL = theURL & ":#cgi.server_port#"; 
       theURL = theURL & "#cgi.path_info#"; 
       if(len(cgi.query_string)) theURL = theURL & "?#cgi.query_string#"; 
       return theURL; 
      } 
    } 
</cfscript> 

但是都沒有成功。我也嘗試在函數調用之前將此添加到頁面中:

<cfoutput>"#StructKeyExists(variables,"GetCurrentURL")#"</cfoutput> 

它導致在屏幕上打印單詞「是」。這表明上述內容應該起作用,因爲if語句的內容顯然會評估爲「是」,因此if語句將評估爲false,因此函數將不會被聲明,因此我將保持我的理智。但由於某種原因,這個問題依然存在。

有關可能發生什麼或如何解決下一個問題的任何想法?我被困在這一點上。

+0

你確定error.cfm沒有被包含在elswhere?它只包含在onError()中嗎? – 2012-04-02 18:15:19

+0

是的,它只在onError()中。 – Nicholas 2012-04-02 19:37:47

回答

3

ColdFusion在將它編譯成字節碼時仍然會看到函數聲明。您可以使用cfinclude包括函數聲明:

<cfif StructKeyExists(variables,"GetCurrentURL") IS "NO"> 
<cfinclude template="udf.cfm" /> 
</cfif> 

然後在udf.cfm把你的函數聲明。這應該按照你的想法工作,並防止CF引發錯誤。

+0

哦,是的!爲什麼我沒有看到?解析聲明是不夠的。我敢打賭你是對的肖恩......很好! – 2012-04-02 19:25:28

+0

我最終通過將聲明移至Application.cfc來解決此問題,但涉及到一些負面影響。本地初步測試表明您的解決方案似乎工作得更好,因此我將努力實現QAed並投入生產。非常感謝! :) – Nicholas 2012-04-02 19:35:51

-1

另一種解決方法是在定義之前從範圍中刪除函數。例如...

<cfset StructDelete(variables,'myFunction')> 
<cffunction name="myFunction">...</cffunction> 
+0

歡迎來到SO Marius,你能描述哪裏,如何以及爲什麼刪除OP的功能可以解決問題? – CPHPython 2017-01-19 13:31:36