Coldfusion で、現在のパス フォルダーの上にあるコンポーネントを初期化するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2525785

  •  22-09-2019
  •  | 
  •  

質問

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

/
/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 にディレクトリを上に移動できるかどうかでした。プロジェクト。

答えが「いいえ」の場合は、マッピングかアプリケーション設定かに関係なく、ベスト プラクティスが何かを理解する必要があります。

役に立ちましたか?

解決

私たちは、CFの管理者でマッピングを使用してこれを扱います。通常、すべてのコンポーネントは、WWWのルート上にある一つのディレクトリに行きます。あなたのケースでは、あなたが行うことができるようになる/へのマッピングを追加することができます:

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

他のヒント

あなたのフォルダ構造のルートでのApplication.cfcを持っている場合、あなたはこのようなものを使用することができます:

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

と "local.bin.myComponent"

を介してアクセス

大変な週の終わりなので、次のコードを何らかの方法で強化できる可能性が非常に高いですが、一般的にはこのアプローチが機能するはずです。

<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 があることを前提として、このような例を追加しました (例:「../Application.cfc」(index.cfm から見る場合):

<cfcomponent output="false">

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

</cfcomponent>

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

ちょうどルートからのフルパスを使用します。

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

item.cfcがで[サイトルート]

/ libに/カート/ - これはあなたのコード内のどこからでも動作します。

私はこの同じ問題を持っていたし、これが私の解決策でした。それはかなりまっすぐ進むのですが、それは私にヒットすることは、数時間を要しました。うまくいけば、これは誰かにいくつかの時間を節約します。

私は

で開始しました
<bean id="ColdBooksConnectionService" class="myservice.model.service.ConnectionService" />
常に

とは、私は完全なパス

を書いたので、それは、利用できなかったというエラーが発生しました
<bean id="ColdBooksConnectionService" class="/CFIDE.administrator.myservice.model.service.ConnectionService" />

との問題が解決されます。

希望このことができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top