2

http://mydomain.com/sitemap.xmlで次の XML を表示するように CFWheels を構成するにはどうすればよいですか?

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

      <-- I'll add the <url> tags dynamically here later -->
</urlset>

ファイルから「sitemap.xml」を削除しましたweb.config

controllerこの後、 andの作成についてどうすればよいかわかりませんview。「views」フォルダーに「sitemap.xml」フォルダーを作成し、「index.cfm」ファイルを追加してから、上記の XML を追加する必要がありますか?

「controllers」フォルダに「sitemap.xml.cfc」ファイルを作成する必要がありますか? そして、コントローラーファイルには何を含める必要がありますか?

それはこのように見えるべきですか?

<cfcomponent extends="Controller" output="false">
<cfscript>  
  function init(){
    // Let CFWheels know what type of output this controller can 'provide'
    provides("xml");
  }

  function index(){

  }
</cfscript>
</cfcomponent>

routes.cfm にエントリを追加する必要がありますか?

4

2 に答える 2

2

コントローラーのセットアップ

コントローラーのindex()メソッドは次のようになります。に保存されていcontrollers/Sitemap.cfcます。

function init() {
    // Grab data about URLs from model or build an array of structs to pass to the view
    urls = model("page").findAll(); // This line is just an example

    // Call `renderWith()` to instruct Wheels that this requires a special content-type
    renderWith(urls);
}

ビューの設定

次に、ビューでviews/sitemap/index.xml.cfm必要な XML を生成できます。

<cfoutput>

<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    #includePartial(partial="url.xml", query=urls)#
</urlset>

</cfoutput>

views/sitemap/_url.xml.cfm次に、クエリまたは配列内の単一のアイテムを表すパーシャル at を実装できます。クエリ以外のものを使用している場合はお知らせください。上記の例を変更できます。

<cfoutput>

<url>
    <loc>#arguments.uri#</loc>
    <loc>#arguments.updatedAt#</loc>
</url>

</cfoutput>

このようなパーシャルを使用すると、クエリ列または構造体キーがスコープに配置されることに注意してください。これが、架空の例で参照しargumentsている理由です。arguments.uriarguments.updatedAt

URL経由でアクセスする

サーバーの URL 書き換え機能によっては、URL が目的の動作をするようにするには、いくつかのことを試す必要がある場合があります。

このようなことができるかもしれませんconfig/routes.cfm(ただし、これは Apache でしかテストしていません):

<cfset addRoute(pattern="sitemap.[format]", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap", controller="sitemap", action="index")>

次に、URLをロードできますhttp://www.example.com/sitemap.xml

それでもうまくいかない場合は、これを試してください:

<cfset addRoute(pattern="sitemap.xml", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap", controller="sitemap", action="index")>

ここでも、URL をロードできます。http://www.example.com/sitemap.xml

最後に、それが機能しない場合は、余分な行を削除してconfig/routes.cfmこの URL をロードします (これは、関係なく常に機能することは間違いありません)。

`http://www.example.com/sitemap?format=xml`.
于 2012-01-31T19:17:24.077 に答える
1

最初に、完全な URL 書き換えを行うように Web サーバーを構成する必要があります (まだ行っていない場合)。そうすれば、URL に index.cfm を含める必要がなくなります (http://mydomain.com/index.cfm/foo/bar は http://mydomain.com/foo/bar になります)

配置したら、config/routes.cfm を次のように変更します。

<cfset addRoute(name="sitemap",
        pattern="/sitemap.xml",
        controller="sitemap",
        action="list") />

次に、ここに XML コードを追加できます。

/views/sitemap/list.cfm

そして、オプションでここにコントローラーがあります:

/controllers/Sitemap.cfc (list という名前の関数を含む)

編集

上記はうまく機能しないため、CFWheels に付属するストック書き換えルールを調べたところ、大きな問題に気付きました。

RewriteCond %{REQUEST_URI} !^.*/(flex2gateway|jrunscripts|cfide|cfformgateway|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|sitemap.xml|rewrite.cfm|favicon.ico)($|/.*$) [NC]

「sitemap.xml」に注意してください。これをリストから削除すると、次のようになります。

RewriteCond %{REQUEST_URI} !^.*/(flex2gateway|jrunscripts|cfide|cfformgateway|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|rewrite.cfm|favicon.ico)($|/.*$) [NC]

Web サーバーの再起動/リロードが必要になる場合があります。しかし、それはそれを行う必要があります。

編集

最後のアイデア - /sitemap.xml へのリクエストを /sitemap にリダイレクトする書き換えルールを Web サーバーに追加できます。

于 2012-01-30T22:52:00.827 に答える