入門 - kouji6309/SingleMVC GitHub Wiki

系統需求

為求更好的性能、更廣泛的功能,SingleMVC 需要以下環境:

Apache 或 IIS (啟用 URL Rewrite)
PHP 7.0+  
MySQL 5.5.3+

其中,PHP 需要開啟以下 Extension

Multibyte String
PDO MySQL
OpenSSL
cURL

目錄結構

SingleMVC 支援多種目錄結構,分別為:

單一檔案

加入框架後,把所有東西都寫在 index.php

網站根目錄
 ├ [tests] (PHPUnit 目錄)
 ├ [vendor] (Composer 目錄)
 ├ .htaccess (或 web.config)
 ├ index.php
 └ SingleMVC.php

分離檔案

SingleMVC.php 更名為 index.php,並建立 source 目錄與子目錄

網站根目錄
 ├ source
 │  ├ 3rd
 │  ├ controllers
 │  ├ helper
 │  ├ lang
 │  ├ models
 │  ├ views
 │  └ config.php
 ├ [tests] (PHPUnit 目錄)
 ├ [vendor] (Composer 目錄)
 ├ .htaccess (或 web.config)
 └ index.php

共用框架

SingleMVC.php 放在其他地方讓多個 index.php 使用

某個地方
 └ SingleMVC.php

網站根目錄
 ├ A應用程式
 │  ├ [tests] (PHPUnit 目錄)
 │  ├ [vendor] (Composer 目錄)
 │  ├ .htaccess (或 web.config)
 │  └ index.php
 └ B應用程式
    ├ [tests] (PHPUnit 目錄)
    ├ [vendor] (Composer 目錄)
    ├ .htaccess (或 web.config)
    └ index.php

私有檔案

除了 index.php 放在網站目錄,其餘 source 放到網站目錄以外。(注意:這種用法需要在 index.php 中定義常數 ROOTsource 的目錄)

某個地方
 └ SingleMVC.php

另一個地方
 └ A應用程式
    ├ source
    │ ├ controllers
    │ ├ models
    │ ├ views
    │ └ config.php
    ├ [tests] (PHPUnit 目錄)
    └ [vendor] (Composer 目錄)

網站根目錄
 ├ .htaccess (或 web.config)
 └ index.php

以上結構可以混用,也就是說,你可以把一部分程式放在 index.php 中,一部分放在 source 底下,SingleMVC 都可以正確讀取。


安裝

非常簡單,只要選定一種目錄結構,然後把 SingleMVC.php 複製到那就可以囉。

為了讓路由可以正常運作,你必須要設定 URL Rewrite,下列提供 Apache 與 IIS 的範本。

Apache:檔案 .htaccess 修改或加入以下設定

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ ./index.php?/$1 [QSA,PT,L]
</IfModule>

IIS:檔案 web.config 加入 rewrite 區塊

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

下一步

教學來使用 SingleMVC。

⚠️ **GitHub.com Fallback** ⚠️