【Windows7】XAMPPでCakePHP 3.7の動作環境を構築する

# はじめに
CakePHP3が提供している機能や気になっているライブラリを試すために、自宅のPCにCakePHP3の動作環境を構築することにしました。
ただ、ミドルウェアなど諸々の導入にあまり時間をかけたくなかったので、今回はXAMPPを利用して動作環境を構築します。

# 達成目標
1.XAMPPをインストールする。
2.ComposerでCakePHP 3.7をインストールし、正常に動作する環境を構築する。

# 動作環境
| | バージョン |
| :--- | :--- |
| OS | Windows7 (64bit) |

# 環境構築手順
## XAMPPインストール手順
### インストーラ入手
[Apache Friends](https://www.apachefriends.org/jp/index.html)にアクセスし、｢Windows 向け XAMPP｣をクリックしてインストーラをダウンロード。 
_なお、今回ダウンロードしたインストーラは｢xampp-windows-x64-7.3.5-0-VC15-installer.exe｣ですが、別バージョンをダウンロードした場合や、ダウンロード時期によって変わることがある点に注意してください。_
![SnapCrab_NoName_2019-5-11_17-47-0_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/8e00142d-a404-a104-09d5-6cf582a3099a.png)
![SnapCrab_xampp-windows-x64-735-0-VC15-installerexe を開く_2019-5-11_17-47-18_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/dfa71654-96f3-a488-22fb-e69b2b2f57ef.png)

### インストール
ダウンロードしたインストーラを起動し、XAMPPをインストール。
_インストール内容については各環境に準拠してください。_
![SnapCrab_Setup_2019-5-11_18-13-24_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/0f997ca9-6690-490d-3848-8f3f0144e64f.png)
![SnapCrab_Setup_2019-5-11_18-13-37_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/48209b59-425f-eda9-673c-20a2e629e241.png)
![SnapCrab_Setup_2019-5-11_18-14-0_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/48973660-9b75-1024-4cd0-45ab26527a6e.png)
![SnapCrab_Setup_2019-5-11_18-14-15_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/f8d9d7da-538f-b592-21a0-4d30c4a1eafb.png)
![SnapCrab_Setup_2019-5-11_18-14-39_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/0356da65-2544-b3e8-886c-4012eb7e6344.png)
![SnapCrab_Setup_2019-5-11_18-14-51_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/ed343d30-e201-0b78-acb9-82405c630497.png)
![SnapCrab_Setup_2019-5-11_18-23-1_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/c9995dea-114c-5a56-466a-444f16b3a302.png)

### 動作確認
XAMPPコントロールパネルを起動し、ApacheとMySQLが正常に動作するか確認。
![SnapCrab_Language_2019-5-11_18-23-18_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/7df7bf78-b4a3-82f7-3f5c-612561e3962a.png)
![SnapCrab_XAMPP Control Panel v323   [ Compiled Mar 7th 2019 ]_2019-5-11_18-27-37_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/699f9df0-c99d-2d8a-93f3-899adcca849a.png)
![SnapCrab_NoName_2019-5-11_18-26-49_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/c07cabce-18e1-ec22-8e4b-9f3cebaaf859.png "http://localhost")
![SnapCrab_NoName_2019-5-11_18-27-49_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/23c5c404-2d4f-e8e7-13f0-cc5ce5cf040d.png "http://localhost/phpmyadmin")

## Composerインストール手順
### インストーラ入手
[コチラ](https://getcomposer.org/doc/00-intro.md#installation-windows)から、Composerのインストーラをダウンロード。
![SnapCrab_NoName_2019-5-12_9-33-26_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/3f89dc65-29ba-97e1-0c82-63bb83c89bba.png)
![SnapCrab_Composer-Setupexe を開く_2019-5-12_9-33-58_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/5fd60ea6-0280-80a1-3abe-ce5f6f0b1900.png)

### インストール
ダウンロードしたインストーラを起動し、Composerをインストール。
_インストール内容については各環境に準拠してください。_
![SnapCrab_Composer Setup_2019-5-12_9-35-51_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/18b2b5bd-830d-921b-0234-1ea3ace5aa5e.png)
![SnapCrab_Composer Setup_2019-5-12_9-36-3_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/c21f7c98-5cc8-6584-af26-6e60157454c5.png)
![SnapCrab_Composer Setup_2019-5-12_9-36-13_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/af20859c-de81-b10f-bdf8-d91782b0fba3.png)
![SnapCrab_Composer Setup_2019-5-12_9-36-48_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/e822fec8-53ba-5438-0181-72edf60cbd45.png)
![SnapCrab_Composer Setup_2019-5-12_9-37-1_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/62180aaa-71d6-8673-c8da-56782a1496ce.png)
![SnapCrab_Composer Setup_2019-5-12_9-37-19_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/00072092-f2cf-ce45-2546-aaa8192af80c.png)
![SnapCrab_Composer Setup_2019-5-12_9-37-53_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/d282d4d3-0a65-13c9-e7b9-6e2fc9b3e905.png)

### 動作確認
```sh:git-bash
$ composer -V
Composer version 1.8.5 2019-04-09 17:46:47
```

## CakePHPインストール手順
### 前準備
[Cookbook](https://book.cakephp.org/3.0/ja/installation.html#id2)を参考に、CakePHP 3.7のシステム要件を満たすようにPHP拡張を有効化する。

```sh:git-bash
$ vi /c/xampp/php/php.ini
911行目付近の｢;extension=intl｣のコメントアウトを解除
```

### インストール
_なお、Apacheのドキュメントルートは変更していないことを仮定しています。_

```sh:git-bash
$ cd /c/xampp/htdocs

$ composer self-update && composer create-project --prefer-dist cakephp/app cakephp3
You are already using composer version 1.8.5 (stable channel).
Installing cakephp/app (3.7.1)
  - Installing cakephp/app (3.7.1): Loading from cache
Created project in cakephp3
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 77 installs, 0 updates, 0 removals
  - Installing cakephp/plugin-installer (1.1.0): Downloading (100%)
  - Installing aura/intl (3.0.0): Downloading (100%)
  - Installing psr/http-message (1.0.1): Downloading (100%)
  - Installing zendframework/zend-diactoros (1.8.6): Downloading (100%)
  - Installing psr/simple-cache (1.0.1): Downloading (100%)
  - Installing psr/log (1.1.0): Downloading (100%)
  - Installing cakephp/chronos (1.2.5): Downloading (100%)
  - Installing cakephp/cakephp (3.7.7): Downloading (100%)
  - Installing symfony/polyfill-ctype (v1.11.0): Downloading (100%)
  - Installing symfony/yaml (v4.2.8): Downloading (100%)
  - Installing symfony/filesystem (v4.2.8): Downloading (100%)
  - Installing symfony/config (v4.2.8): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.11.0): Downloading (100%)
  - Installing symfony/contracts (v1.1.0): Downloading (100%)
  - Installing symfony/console (v4.2.8): Downloading (100%)
  - Installing robmorgan/phinx (0.10.7): Downloading (100%)
  - Installing cakephp/migrations (2.1.1): Downloading (100%)
  - Installing mobiledetect/mobiledetectlib (2.8.33): Downloading (100%)
  - Installing twig/twig (v1.40.1): Downloading (100%)
  - Installing umpirsky/twig-php-function (v0.1): Downloading (100%)
  - Installing jasny/twig-extensions (v1.2.0): Downloading (100%)
  - Installing asm89/twig-cache-extension (1.3.2): Downloading (100%)
  - Installing aptoma/twig-markdown (2.0.0): Downloading (100%)
  - Installing ajgl/breakpoint-twig-extension (0.3.4): Downloading (100%)
  - Installing wyrihaximus/twig-view (4.3.8): Downloading (100%)
  - Installing cakephp/bake (1.9.6): Downloading (100%)
  - Installing squizlabs/php_codesniffer (3.4.2): Downloading (100%)
  - Installing cakephp/cakephp-codesniffer (3.1.1): Downloading (100%)
  - Installing jdorn/sql-formatter (v1.2.17): Downloading (100%)
  - Installing symfony/process (v4.2.8): Downloading (100%)
  - Installing symfony/finder (v4.2.8): Downloading (100%)
  - Installing seld/phar-utils (1.0.1): Downloading (100%)
  - Installing seld/jsonlint (1.7.1): Downloading (100%)
  - Installing justinrainbow/json-schema (5.2.8): Downloading (100%)
  - Installing composer/xdebug-handler (1.3.2): Downloading (100%)
  - Installing composer/spdx-licenses (1.5.1): Downloading (100%)
  - Installing composer/semver (1.5.0): Downloading (100%)
  - Installing composer/ca-bundle (1.1.4): Downloading (100%)
  - Installing composer/composer (1.8.5): Downloading (100%)
  - Installing cakephp/debug_kit (3.18.0): Downloading (100%)
  - Installing m1/env (2.1.2): Downloading (100%)
  - Installing josegonzalez/dotenv (3.2.0): Downloading (100%)
  - Installing sebastian/version (2.0.1): Downloading (100%)
  - Installing sebastian/resource-operations (1.0.0): Downloading (100%)
  - Installing sebastian/recursion-context (3.0.0): Downloading (100%)
  - Installing sebastian/object-reflector (1.1.1): Downloading (100%)
  - Installing sebastian/object-enumerator (3.0.3): Downloading (100%)
  - Installing sebastian/global-state (2.0.0): Downloading (100%)
  - Installing sebastian/exporter (3.1.0): Downloading (100%)
  - Installing sebastian/environment (3.1.0): Downloading (100%)
  - Installing sebastian/diff (2.0.1): Downloading (100%)
  - Installing sebastian/comparator (2.1.3): Downloading (100%)
  - Installing doctrine/instantiator (1.2.0): Downloading (100%)
  - Installing phpunit/php-text-template (1.2.1): Downloading (100%)
  - Installing phpunit/phpunit-mock-objects (5.0.10): Downloading (100%)
  - Installing phpunit/php-timer (1.0.9): Downloading (100%)
  - Installing phpunit/php-file-iterator (1.4.5): Downloading (100%)
  - Installing theseer/tokenizer (1.1.2): Downloading (100%)
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Downloading (100%)
  - Installing phpunit/php-token-stream (2.0.2): Downloading (100%)
  - Installing phpunit/php-code-coverage (5.3.2): Downloading (100%)
  - Installing webmozart/assert (1.4.0): Downloading (100%)
  - Installing phpdocumentor/reflection-common (1.0.1): Downloading (100%)
  - Installing phpdocumentor/type-resolver (0.4.0): Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (4.3.1): Downloading (100%)
  - Installing phpspec/prophecy (1.8.0): Downloading (100%)
  - Installing phar-io/version (1.0.1): Downloading (100%)
  - Installing phar-io/manifest (1.0.1): Downloading (100%)
  - Installing myclabs/deep-copy (1.9.1): Downloading (100%)
  - Installing phpunit/phpunit (6.5.14): Downloading (100%)
  - Installing jakub-onderka/php-console-color (v0.2): Downloading (100%)
  - Installing jakub-onderka/php-console-highlighter (v0.4): Downloading (100%)
  - Installing dnoegel/php-xdg-base-dir (0.1): Downloading (100%)
  - Installing nikic/php-parser (v4.2.1): Downloading (100%)
  - Installing symfony/polyfill-php72 (v1.11.0): Downloading (100%)
  - Installing symfony/var-dumper (v4.2.8): Downloading (100%)
  - Installing psy/psysh (v0.9.9): Downloading (100%)
cakephp/app suggests installing markstory/asset_compress (An asset compression plugin which provides file concatenation and a flexible filter system for preprocessing and minification.)
cakephp/app suggests installing dereuromark/cakephp-ide-helper (After baking your code, this keeps your annotations in sync with the code evolving from there on for maximum IDE and PHPStan compatibility.)
cakephp/cakephp suggests installing lib-ICU (The intl PHP library, to use Text::transliterate() or Text::slug())
symfony/contracts suggests installing psr/cache (When using the Cache contracts)
symfony/contracts suggests installing psr/container (When using the Service contracts)
symfony/contracts suggests installing symfony/cache-contracts-implementation
symfony/contracts suggests installing symfony/event-dispatcher-implementation
symfony/contracts suggests installing symfony/http-client-contracts-implementation
symfony/contracts suggests installing symfony/service-contracts-implementation
symfony/contracts suggests installing symfony/translation-contracts-implementation
symfony/console suggests installing symfony/event-dispatcher
symfony/console suggests installing symfony/lock
asm89/twig-cache-extension suggests installing psr/cache-implementation (To make use of PSR-6 cache implementation via PsrCacheAdapter.)
aptoma/twig-markdown suggests installing michelf/php-markdown (Original Markdown engine with MarkdownExtra.)
aptoma/twig-markdown suggests installing knplabs/github-api (Needed for using GitHub's Markdown engine provided through their API.)
ajgl/breakpoint-twig-extension suggests installing ext-xdebug (The Xdebug extension is required for the breakpoint to work)
ajgl/breakpoint-twig-extension suggests installing symfony/framework-bundle (The framework bundle to integrate the extension into Symfony)
ajgl/breakpoint-twig-extension suggests installing symfony/twig-bundle (The twig bundle to integrate the extension into Symfony)
m1/env suggests installing m1/vars (For loading of configs)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.5.5)
phpunit/phpunit suggests installing phpunit/php-invoker (^1.1)
phpunit/phpunit suggests installing ext-xdebug (*)
psy/psysh suggests installing ext-pcntl (Enabling the PCNTL extension makes PsySH a lot happier :))
psy/psysh suggests installing ext-posix (If you have PCNTL, you'll want the POSIX extension as well.)
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
psy/psysh suggests installing hoa/console (A pure PHP readline implementation. You'll want this if your PHP install doesn't already support readline or libedit.)
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files
> Cake\Composer\Installer\PluginInstaller::postAutoloadDump
> App\Console\Installer::postInstall
Created `config/app.php` file
Created `C:\xampp\htdocs\cakephp3/tmp/cache/views` directory
Set Folder Permissions ? (Default to Y) [Y,n]? y
Updated Security.salt value in config/app.php
```

### 動作確認
XAMPPコントロールパネルでApacheを起動し、インストール直後のアプリケーションにアクセス。
![SnapCrab_NoName_2019-5-12_11-3-40_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/4aa7bc52-0f96-743a-6aa2-ac369f2a8d81.png "http://localhost/cakephp3")
データベースへの接続ができていないため、これを修正。

### 問題解決
phpMyAdminを利用し、接続対象となるデータベースを作成。
_phpMyAminを利用したデータベースの作成方法についてはここでは割愛するので、[コチラ](https://qiita.com/RikaKawasaki/items/8786094311e34b5e4f30)などを参考にしてください。_
![SnapCrab_NoName_2019-5-12_11-41-40_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/1b20d4a9-3659-98c5-402b-89e4eece8ba6.png)


アプリケーションのデータベースに関する設定を変更。

```php:cakephp3/config/app.php
<?php
return [

    // 省略

    /**
     * Connection information used by the ORM to connect
     * to your application's datastores.
     *
     * ### Notes
     * - Drivers include Mysql Postgres Sqlite Sqlserver
     *   See vendor\cakephp\cakephp\src\Database\Driver for complete list
     * - Do not use periods in database name - it may lead to error.
     *   See https://github.com/cakephp/cakephp/issues/6471 for details.
     * - 'encoding' is recommended to be set to full UTF-8 4-Byte support.
     *   E.g set it to 'utf8mb4' in MariaDB and MySQL and 'utf8' for any
     *   other RDBMS.
     */
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',
            'username' => '***',  // 作成したユーザ
            'password' => '***',  // 作成したユーザのパスワード
            'database' => '***',  // 作成したデータベース
            /*
             * You do not need to set this flag to use full utf-8 encoding (internal default since CakePHP 3.6).
             */
            //'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,

            /**
             * Set identifier quoting to true if you are using reserved words or
             * special characters in your table or column names. Enabling this
             * setting will result in queries built using the Query Builder having
             * identifiers quoted when creating SQL. It should be noted that this
             * decreases performance because each query needs to be traversed and
             * manipulated before being executed.
             */
            'quoteIdentifiers' => false,

            /**
             * During development, if using MySQL < 5.6, uncommenting the
             * following line could boost the speed at which schema metadata is
             * fetched from the database. It can also be set directly with the
             * mysql configuration directive 'innodb_stats_on_metadata = 0'
             * which is the recommended value in production environments
             */
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],

            'url' => env('DATABASE_URL', null),
        ],

        /**
         * The test connection is used during the test suite.
         */
        'test' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            //'port' => 'non_standard_port_number',
            'username' => '***',  // 作成したユーザ
            'password' => '***',  // 作成したユーザのパスワード
            'database' => '***',  // 作成したデータベース
            //'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'quoteIdentifiers' => false,
            'log' => false,
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
            'url' => env('DATABASE_TEST_URL', null),
        ],
    ],

    // 省略
];
```

再度アプリケーションにアクセス。
![SnapCrab_NoName_2019-5-12_11-42-10_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/398732/1120938f-daa0-d1af-3102-7b5642e603c6.png "http://localhost/cakephp3")
先ほどまでエラーだった箇所が解消され、アプリケーションが正常に動作するようになりました。

# おわりに
今回構築した環境のバージョン情報は下表の通りです。

| 名前 | バージョン |
| :--- | :--- |
| Apache | 2.4.39 (Win64) |
| MySQL(MariaDB) | 15.1 Distrib 10.1.39-MariaDB, for Win64 (AMD64) |
| Composer | 1.8.5 |
| CakePHP | 3.7.7 |

実際にはここからアプリケーションごとに色々と設定を施していく必要がありますが、本題から反れるので割愛させていただきます。

XAMPPを利用すればPHPの動作環境があっという間に整うので、手軽にCakePHP3の動作環境を構築できて良いですね。

# 参考
* [今さら聞けない！XAMPPをインストールする方法【超初心者向け】](https://techacademy.jp/magazine/1722)
* [Composer を Windows にインストールする手順](https://weblabo.oscasierra.net/php-composer-windows-install/)