17

次のようなフォルダー構造があるとします。

/
/bin/myComponent.cfc
/reports/index.cfm

index.cfm から myComponent.cfc を開始するにはどうすればよいですか?

myService = createObject("component", "bin.myComponent");

ドット構文を使用して、より深いフォルダーに移動する方法を知っていますが、フォルダーを上に移動し、別のフォルダーに下に移動するにはどうすればよいですか? スラッシュ構文を使用すると、次のようになります。

../bin/myComponent.cfc

しかし createObject() はそのようには機能しません。パスを壊さずにこのフォルダーを別のサーバーに移動できるように、相対パスを保持したいと思います。

アイデア?ありがとう!

編集:

私の例では、皆さんが提供した創造的な回答に対して十分な深さのフォルダー構造が表示されませんでした。これが私がすべきことです:

/[my project folder]/
/[my project folder]/bin/myComponent.cfc
/[my project folder]/reports/index.cfm

私の基本的な質問は、createObject("component","dot path") を使用して index.cfm から myComponent.cfc にディレクトリを移動できるかどうかでした。プロジェクト。

答えが「いいえ」の場合は、ベスト プラクティスが何であるかを把握する必要があります。それがマッピングであるか、アプリケーション設定であるかにかかわらずです。

4

6 に答える 6

17

フォルダ構造のルートにApplication.cfcがある場合は、次のようなものを使用できます。

<cfset this.mappings["/local"] = getDirectoryFromPath(getCurrentTemplatePath()) />

次に、「local.bin.myComponent」からアクセスします

于 2010-03-26T20:00:38.287 に答える
17

これは、cf アドミニストレータのマッピングを使用して処理します。通常、すべてのコンポーネントは、www ルートの上にある 1 つのディレクトリに配置されます。あなたの場合、 / にマッピングを追加すると、次のことが可能になります。

myService = createObject("component", "mymapping.bin.myComponent");
于 2010-03-26T18:47:27.230 に答える
4

ハードな 1 週間の終わりなので、次のコードを何らかの形で強化できる可能性はかなり高いですが、通常はこのアプローチでうまくいくはずです。

<cfscript>

    // this script is here http://XXXXXXX/test/paths/relative/reports/index.cfm
    // component is here http://XXXXXXX/test/paths/relative/bin/myComponent.cfc

    local = {};

    // initialize with dynamic mapping
    local.myComponentDynamic = createObject("component", "/bin/myComponent");

    // grab the current directory name
    local.parentPathExpanded = ExpandPath("../");
    local.scriptPathExpanded = ExpandPath(cgi.SCRIPT_NAME);
    local.thisDirectory = GetDirectoryFromPath(Replace(local.scriptPathExpanded, local.parentPathExpanded, ""));

    // build base path
    local.scriptPathDirectory = GetDirectoryFromPath(cgi.SCRIPT_NAME);
    local.basePath = Replace(local.scriptPathDirectory, local.thisDirectory, "");

    // this is relative path we already know
    local.relativePath = "bin/myComponent";

    // initialize with slash-syntax (path starting with /)
    local.myComponentSlash = createObject("component", local.basePath & local.relativePath);

    // convert path to the dot-syntax
    local.dottedPath = Replace(local.basePath & local.relativePath, "/", ".", "ALL");
    local.dottedPath = Right(local.dottedPath, Len(local.dottedPath)-1);

    // initialize with dot-syntax path
    local.myComponentDot = createObject("component", local.dottedPath);

</cfscript>
<cfdump var="#local#">

この例を読みやすく理解しやすくするために、プロセスを個別の変数に分割し、共通コンテナーをダンプしました。

ただし、Application.cfc で動的マッピングを使用できる場合は、それを使用してください。

編集:親フォルダーに Application.cfc があると仮定して、そのような例を追加しました (たとえば、index.cfm から見る場合は「../Application.cfc」)。

<cfcomponent output="false">

    <cfset this.mappings["/bin"] = getDirectoryFromPath(getCurrentTemplatePath()) & "bin/" />

</cfcomponent>

私の「パス変換」の例は、楽しいトリックであり、コードをいじっているだけであり、優れたアプリケーションの単純なアプローチではありません。

于 2010-03-26T21:11:13.640 に答える
3

ルートからのフルパスを使用するだけです

<cfset obj = createObject("component", "bin.cart.item")>

item.cfc は [website root]/lib/cart/ にあります - これはコードのどこからでも機能します。

于 2010-03-26T20:04:16.687 に答える
0

私はこれと同じ問題を抱えていましたが、これが私の解決策でした。それはかなり簡単ですが、それが私を襲うのに数時間かかりました. うまくいけば、これで誰かの時間を節約できます。

私はから始めました

<bean id="ColdBooksConnectionService" class="myservice.model.service.ConnectionService" />

利用できないというエラーが常に発生したため、フルパスを書き留めました

<bean id="ColdBooksConnectionService" class="/CFIDE.administrator.myservice.model.service.ConnectionService" />

問題は解決しました。

お役に立てれば。

于 2011-07-05T22:29:18.503 に答える