萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

在 Windows Azure 網站部署 CakePHP 應用程式

CakePHP 是採用 ActiveRecord、Association Data Mapping、Front Controller、MVC 等經典設計模式的快速開發框架,目標是讓各層次 PHP 開發者快速建構健壯 Web 應用,兼具彈性。

部署步驟

  1. 下載 CakePHP 2.2.1 並解壓縮
  2. 發布至 Windows Azure 網站:
    • 透過 FTP 上傳檔案,或
    • 使用 Git 部署
  3. 建立應用程式(本文以 CakePHP Blog Tutorial 為測試範例)

關鍵設定:IIS URL Rewrite (web.config)

Windows Azure 使用 IIS 裝載,不支援 Apache .htaccess,需以 web.config 定義重寫規則。以下為 CakePHP 官方範例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^$" ignoreCase="false" />
          <action type="Rewrite" url="/" />
        </rule>
        <rule name="Imported Rule 3" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <action type="Rewrite" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 4" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

說明

  • Rule 1:非實體目錄/檔案請求重寫至 index.php?url=...
  • Rule 2:根目錄請求重寫至 /
  • Rule 3:補齊前導斜線
  • Rule 4:備用規則,重寫至 index.php/...(某些情況下需要)

將此 web.config 放置於 CakePHP 專案根目錄即可正常路由。

copyright © 萬盛學電腦網 all rights reserved