2012-05-11 31 views
5

मुझे एप्लीकेशन.cfc में मैपिंग सेट करने में समस्या है। हमारे पास विविध सर्वर (देव, क्यूएस, प्रोड) प्रत्येक अलग-अलग पैथ के साथ है। मैं विन्यास फाइल के माध्यम से सर्वर विशिष्ट पैच और चर सेट करना चाहता हूँ। एप्लिकेशन स्टार्ट पर आप आईएनआई फ़ाइल पढ़ते हैं और अपना सिस्टम सेटअप करते हैं। http://www.raymondcamden.com/index.cfm/2005/8/26/ColdFusion-101-Config-Files-AGoGo यह ठीक काम करता है। एक सामान्य CFM फ़ाइल मेंमैं बाहरी संपत्ति फ़ाइल से एप्लिकेशन.cfc में मैपिंग कैसे शामिल कर सकता हूं?

<!--- in Application.cfc ---> 
<cfset this.mappings['/components'] = "D:\Inetpub\wwwroot\myApp\components"> 

समवेयर आई के माध्यम से एक सीएफसी नामित परीक्षण instatiate:

Normaly आप मैपिंग Applcation.cfc में इस तरह सेट

<cfset t = createObject("component", "components.test")> 

मैं केवल मैपिंग सेट करना चाहते हैं एक बार पर onApplicationsStart

<cffunction 
    name="OnApplicationStart" 
    access="public" 
    returntype="boolean" 
    output="false" 
    hint="Fires when the application is first created."> 

    <!---create structure to hold configuration settings---> 
    <cfset ini = structNew()> 
    <cfset ini.iniFile = expandPath("./ApplicationProperties.ini")> 
    <cfset application.ini = ini> 

    <!--- read ini file ---> 
    <cfset sections = getProfileSections(application.ini.iniFile)> 

    <cfloop index="key" list="#sections.mappings#"> 
     <cfset this.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)> 
    </cfloop> 

लेकिन यह काम नहीं करता है क्योंकि यह। मैपिंग खाली है और अगला अनुरोध है। :(

को OnRequestStart

<!--- read ini file ---> 
    <cfset sections = getProfileSections(application.ini.iniFile)> 

    <cfloop index="key" list="#sections.mappings#"> 
     <cfset this.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)> 
    </cfloop> 

मुझे लगता है कि घटक नहीं पाया जा सकता है कोई त्रुटि मिलती है इस लाना। यह अजीब है।

आवेदन गुंजाइश में struct लाना

<cfloop index="key" list="#sections.mappings#"> 
     <cfset APPLICATION.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)> 
    </cfloop> 

मेरे घटक को कैसे कॉल करें?

<cfset t = createObject("component", "application.components.test")> 

काम नहीं करता है।

तो मेरे पास 3 लक्ष्य हैं।

  1. sourcecode में आसान उपयोग ApplicationStart पर INI फ़ाइल
  2. से सभी pathes और मैपिंग पढ़ने उन्हें पढ़ एक बार।

उत्तर

7

मैपिंग्स ऑनप्लिकेशंस स्टार्ट() पर सेट नहीं किया जा सकता है, उन्हें Application.cfc के छद्म कन्स्ट्रक्टर में सेट किया जाना चाहिए, और उन्हें प्रत्येक अनुरोध पर सेट किया जाना चाहिए।

यह भी ध्यान रखना महत्वपूर्ण है कि इस बिंदु पर एप्लिकेशन स्कोप उपलब्ध नहीं है, इसलिए यदि आपको कुछ भी कैश करने की आवश्यकता है तो आपको सर्वर स्कोप का उपयोग करने की आवश्यकता होगी। आप अपने मैपिंग स्ट्रक्चर को सर्वर स्कोप पर कैश कर सकते हैं और बस इसे प्रत्येक सेट में जोड़ सकते हैं।

<cfcomponent> 
    <cfset this.name = "myapp" /> 

    <!--- not cached so create mappings ---> 
    <cfif NOT structKeyExists(server, "#this.name#_mappings")> 
    <cfset iniFile = getDirectoryFromPath(getCurrentTemplatePath()) & "/ApplicationProperties.ini" /> 
    <cfset sections = getProfileSections(iniFile) /> 
    <cfset mappings = structnew() /> 
    <cfloop index="key" list="#sections.mappings#"> 
     <cfset mappings[key] = getProfileString(iniFile, "mappings", key)> 
    </cfloop> 
    <cfset server["#this.name#_mappings"] = mappings /> 
    </cfif> 

    <!--- assign mappings from cached struct in server scope ---> 
    <cfset this.mappings = server["#this.name#_mappings"] /> 

    <cffunction name="onApplicationStart"> 
    <!--- other stuff here ---> 
    </cffunction> 

</cfcomponent> 

आप webroot में आप INI फ़ाइल रखने के लिए करना चाहते हैं, आप इसे एक .cfm टेम्पलेट बनाने के लिए और एक < cfabort के साथ शुरू कर देना चाहिए>। यह वही काम करेगा लेकिन पढ़ने योग्य नहीं होगा

ApplicationProperties.ini।cfm

<cfabort> 
[mappings] 
/foo=c:/bar/foo 
+0

बहुत बहुत धन्यवाद यह मुझे सही दिशा में एक बड़ा कदम लाता है। लेकिन इस पंक्ति के कारण मुझे वेब्रॉट में नहीं होने वाले पृष्ठ को कॉल करते समय "त्रुटि नहीं मिली" प्राप्त होती है। – inog

+0

मैं इसे हार्डकोड नहीं करना चाहता हूं। इस चिकन या अंडा दुविधा के लिए कोई विचार? – inog

+0

Thats क्योंकि वर्तमान फ़ाइल स्थान के सापेक्ष इसकी कॉलिंग विस्तारपथ()। आपको एक पूर्ण पथ का उपयोग करना होगा, मैंने यह जवाब –