> ## Content Index
> Fetch the complete content index at: https://blog.hinablue.me/llms.txt
> Use this file to discover other available public pages before exploring further.

# [PHP note.] 微型專案 Part 3
- URL: https://blog.hinablue.me/php-note-micro-project-note-part-3/
- Published: 2011-12-12T02:16:00.000Z
- Updated: 2026-06-25T09:31:11.000Z
- Author: Hina Chen
- Tags: PHP

繼續接著上次的[微型專案 Par 2](https://blog.hinablue.me/entry/php-note-micro-project-note-part-2/)，今天接著說**擴充元件**與**設定檔案**的部份。當然啦，如果是大神路過的話，一樣是 `Alt+F4` 跳過就可以了喔（啾咪。

那我們就繼續了。

## 擴充元件

我們想到**擴充**通常都會是用 `plugins` 來作，或者是用 `components` 也可以，大同小異。使用的方式差不多，唯一的區別大概是，`Components` 通常會拿來跟 `Controller` 一起使用，而 `plugins` 則是比較獨立的元件（像是第三方開放的 SDK 等等）。

所以，我們這邊以 `Components` 為主，來聊聊怎麼在 `Controller` 裡面使用我們自己製作的元件。

首先呢，我們需要看一下資料夾結構，在既有的 controllers 資料夾中，我們在新增一個 components 的資料夾，來放我們自行製作的元件檔案。然後，我們的控制器就先以這種方式，把我們自行製作的 components 給載入。

```
<?php
class Api extends Controller {

    public $components = array('myFirstComponent');

    public function __construct() {
    }

    public function myController() {
        $this->myFirstComponent->hello();
    }
}

```

這樣還不夠，我們在自己的 Controller 加入關於 Components 的設定之後，要怎麼讓他**自己動起來**呢？這個時候，我們必須改寫原先的 `bootstrap.php` 來讓他自動載入。我們從[微型專案 Par 1](https://blog.hinablue.me/entry/php-note-micro-project-note-part-1/)的 Controller 載入的地方做一些修改。

```
<?php
    // 上面省略 5000 行

        if (file_exists($controllerPath)) {
            require_once $controllerPath;
            $this->controller = new $controllerName;

            if (property_exists($this->controller, 'components') && is_array($this->controller->components)) {
                foreach($this->controller->components as $component) {
                    $components_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . $component . '.php';
                    if (file_exists($components_file)) {
                        require_once $components_file;
                        $componentName = $component.'Component';
                        if (class_exists($componentName)) {
                            $this->controller->$component = new $componentName;
                        }
                    }
                }
            }
            $this->controller->startup();
        } else {
            die('Controller does not exists.');
        }

    // 底下省略 5000 行

```

上面做了什麼事情呢？

- 先檢測`Controller`裡面有沒有`components`這個屬性（使用`property_exists`），**必須是陣列**。 \* 如果有的話，把陣列拿出來**當作是檔案名稱**，去 components 資料夾找檔案。 \* 如果有檔案的話，把他拉進來，然後元件命名規則是 `元件名稱+Component`，例如：ApiComponent 這樣。 \* 把元件名稱當作是`Controller`的物件`new`起來。

那，關鍵的 Components 要怎麼寫呢？

```
<?php
class myFirstComponentComponent {

    public function __construct() {
    }

    public function hello() {
        echo 'Hello world';
    }
}

```

以上就是一個簡單的元件範例，請注意**元件的 Class 的命名方式**以及**元件的檔案名稱**之間的關係。這些命名規則你可以自行修改。

## 設定檔案

通常我們會有一些固定的變數，魔術數字等等。當然我們不希望把這些東西四處撒落在我們的程式碼裡面，所以，我們需要一個`Configure`來放這些設定的資訊。實作的方法其實也不會特別困難，就是多一個類別來幫我們處理這些事情而已。

通常我們就把設定檔案放在`config`資料夾中，然後利用檔案引入（`require`）的方式來把設定抓進來。

```
<?php

$config['MagicNumber']['1000'] = 'Hello world';

```

然後我們只要稍微改一下我們的 bootstrap 讓他去讀取設定檔案即可。

```
<?php

class Configure {

    public static $config;

    public function __construct() {

    }

    public function load($filename) {
        if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $filename . '.php')) {
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $filename . '.php';

            self::$config = $config;
        }
    }
}

```

這裡是使用`$config`來當作設定檔案的變數，所以在使用上請留意，不要讓這個變數與其他的地方混用了。

## 小結

我們一個簡單的控制器大抵上就接近完成了。至於還需要什麼其他的功能，就由大家去發揮創意囉！