環境構築からWEBアプリ開発・スマホアプリ開発まで。ときには動画制作やゲームも。

supilog
すぴろぐ

CakePHP4で簡単 初めてのプロジェクト作成

PHPではじめて触れたフレームワークがわたしはCakePHPだった(と思う)。

当時の記憶はほぼないが、バージョンは2だったような・・・。それはさておき。4は触ったことがないので、所見で試してみます。

環境

今回の環境はこちら。

  • macOS
  • CakePHP4.x (PHP >= 7.2.0が必要)
  • PHP 7.4.13 

CakePHP4を使用するのは、単純に最新だったから。現場で使う際には、バージョンは吟味するのが吉。参照 : StrawberryCookbook

事前に必要なこと

  • PHPがインストール済みである
  • composerがインストール済みである

これらの手順が不明な場合は、各自検索を。

プロジェクト作成

プロジェクト作成

composer create-project --prefer-dist "cakephp/app:4.*" supilog
実行結果
Download composer.phar ...
All settings correct for using Composer
Downloading...

Composer (version 2.0.11) successfully installed to: /private/tmp/composer.phar
Use it: php ./composer.phar

Move composer.phar to /Users/supi/.anyenv/envs/phpenv/versions/7.4.13/composer
Creating a "cakephp/app:4.*" project at "./supilog"
Installing cakephp/app (4.2.1)
  - Installing cakephp/app (4.2.1): Extracting archive
Created project in /private/tmp/supilog
Loading composer repositories with package information
Updating dependencies
Lock file operations: 91 installs, 0 updates, 0 removals
  - Locking cakephp/bake (2.3.0)
  - Locking cakephp/cakephp (4.2.4)
  - Locking cakephp/cakephp-codesniffer (4.2.4)
  - Locking cakephp/chronos (2.1.1)
  - Locking cakephp/debug_kit (4.4.1)
  - Locking cakephp/migrations (3.0.0)
  - Locking cakephp/plugin-installer (1.3.1)
  - Locking cakephp/twig-view (1.2.0)
  - Locking composer/ca-bundle (1.2.9)
  - Locking composer/composer (2.0.11)
  - Locking composer/semver (3.2.4)
  - Locking composer/spdx-licenses (1.5.5)
  - Locking composer/xdebug-handler (1.4.5)
  - Locking dealerdirect/phpcodesniffer-composer-installer (v0.7.1)
  - Locking dnoegel/php-xdg-base-dir (v0.1.1)
  - Locking doctrine/instantiator (1.4.0)
  - Locking jasny/twig-extensions (v1.3.0)
  - Locking jdorn/sql-formatter (v1.2.17)
  - Locking josegonzalez/dotenv (3.2.0)
  - Locking justinrainbow/json-schema (5.2.10)
  - Locking laminas/laminas-diactoros (2.5.0)
  - Locking laminas/laminas-httphandlerrunner (1.3.0)
  - Locking laminas/laminas-zendframework-bridge (1.2.0)
  - Locking league/container (3.3.4)
  - Locking m1/env (2.2.0)
  - Locking mobiledetect/mobiledetectlib (2.8.37)
  - Locking myclabs/deep-copy (1.10.2)
  - Locking nikic/php-parser (v4.10.4)
  - Locking phar-io/manifest (2.0.1)
  - Locking phar-io/version (3.1.0)
  - Locking phpdocumentor/reflection-common (2.2.0)
  - Locking phpdocumentor/reflection-docblock (5.2.2)
  - Locking phpdocumentor/type-resolver (1.4.0)
  - Locking phpspec/prophecy (1.12.2)
  - Locking phpstan/phpdoc-parser (0.4.9)
  - Locking phpunit/php-code-coverage (9.2.5)
  - Locking phpunit/php-file-iterator (3.0.5)
  - Locking phpunit/php-invoker (3.1.1)
  - Locking phpunit/php-text-template (2.0.4)
  - Locking phpunit/php-timer (5.0.3)
  - Locking phpunit/phpunit (9.5.2)
  - Locking psr/container (1.0.0)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-factory (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking psr/http-server-handler (1.0.1)
  - Locking psr/http-server-middleware (1.0.1)
  - Locking psr/log (1.1.3)
  - Locking psr/simple-cache (1.0.1)
  - Locking psy/psysh (v0.10.6)
  - Locking react/promise (v2.8.0)
  - Locking robmorgan/phinx (0.12.5)
  - Locking sebastian/cli-parser (1.0.1)
  - Locking sebastian/code-unit (1.0.8)
  - Locking sebastian/code-unit-reverse-lookup (2.0.3)
  - Locking sebastian/comparator (4.0.6)
  - Locking sebastian/complexity (2.0.2)
  - Locking sebastian/diff (4.0.4)
  - Locking sebastian/environment (5.1.3)
  - Locking sebastian/exporter (4.0.3)
  - Locking sebastian/global-state (5.0.2)
  - Locking sebastian/lines-of-code (1.0.3)
  - Locking sebastian/object-enumerator (4.0.4)
  - Locking sebastian/object-reflector (2.0.4)
  - Locking sebastian/recursion-context (4.0.4)
  - Locking sebastian/resource-operations (3.0.3)
  - Locking sebastian/type (2.3.1)
  - Locking sebastian/version (3.0.2)
  - Locking seld/jsonlint (1.8.3)
  - Locking seld/phar-utils (1.1.1)
  - Locking slevomat/coding-standard (6.4.1)
  - Locking squizlabs/php_codesniffer (3.5.8)
  - Locking symfony/config (v5.2.3)
  - Locking symfony/console (v5.2.3)
  - Locking symfony/deprecation-contracts (v2.2.0)
  - Locking symfony/filesystem (v5.2.3)
  - Locking symfony/finder (v5.2.3)
  - Locking symfony/polyfill-ctype (v1.22.1)
  - Locking symfony/polyfill-intl-grapheme (v1.22.1)
  - Locking symfony/polyfill-intl-normalizer (v1.22.1)
  - Locking symfony/polyfill-mbstring (v1.22.1)
  - Locking symfony/polyfill-php73 (v1.22.1)
  - Locking symfony/polyfill-php80 (v1.22.1)
  - Locking symfony/process (v5.2.3)
  - Locking symfony/service-contracts (v2.2.0)
  - Locking symfony/string (v5.2.3)
  - Locking symfony/var-dumper (v5.2.3)
  - Locking theseer/tokenizer (1.2.0)
  - Locking twig/markdown-extra (v3.3.0)
  - Locking twig/twig (v3.3.0)
  - Locking webmozart/assert (1.9.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 91 installs, 0 updates, 0 removals
  - Downloading cakephp/plugin-installer (1.3.1)
  - Downloading squizlabs/php_codesniffer (3.5.8)
  - Downloading dealerdirect/phpcodesniffer-composer-installer (v0.7.1)
  - Downloading twig/twig (v3.3.0)
  - Downloading twig/markdown-extra (v3.3.0)
  - Downloading jasny/twig-extensions (v1.3.0)
  - Downloading psr/http-server-handler (1.0.1)
  - Downloading psr/http-server-middleware (1.0.1)
  - Downloading league/container (3.3.4)
  - Downloading psr/http-factory (1.0.1)
  - Downloading laminas/laminas-zendframework-bridge (1.2.0)
  - Downloading laminas/laminas-diactoros (2.5.0)
  - Downloading laminas/laminas-httphandlerrunner (1.3.0)
  - Downloading cakephp/chronos (2.1.1)
  - Downloading cakephp/cakephp (4.2.4)
  - Downloading cakephp/twig-view (1.2.0)
  - Downloading cakephp/bake (2.3.0)
  - Downloading phpstan/phpdoc-parser (0.4.9)
  - Downloading slevomat/coding-standard (6.4.1)
  - Downloading cakephp/cakephp-codesniffer (4.2.4)
  - Downloading jdorn/sql-formatter (v1.2.17)
  - Downloading symfony/finder (v5.2.3)
  - Downloading symfony/polyfill-intl-grapheme (v1.22.1)
  - Downloading symfony/console (v5.2.3)
  - Downloading composer/composer (2.0.11)
  - Downloading cakephp/debug_kit (4.4.1)
  - Downloading symfony/config (v5.2.3)
  - Downloading robmorgan/phinx (0.12.5)
  - Downloading cakephp/migrations (3.0.0)
  - Downloading m1/env (2.2.0)
  - Downloading josegonzalez/dotenv (3.2.0)
  - Downloading mobiledetect/mobiledetectlib (2.8.37)
  - Installing cakephp/plugin-installer (1.3.1): Extracting archive
  - Installing squizlabs/php_codesniffer (3.5.8): Extracting archive
  - Installing dealerdirect/phpcodesniffer-composer-installer (v0.7.1): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.22.1): Extracting archive
  - Installing symfony/polyfill-ctype (v1.22.1): Extracting archive
  - Installing twig/twig (v3.3.0): Extracting archive
  - Installing twig/markdown-extra (v3.3.0): Extracting archive
  - Installing jasny/twig-extensions (v1.3.0): Extracting archive
  - Installing psr/simple-cache (1.0.1): Extracting archive
  - Installing psr/log (1.1.3): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-server-handler (1.0.1): Extracting archive
  - Installing psr/http-server-middleware (1.0.1): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing psr/container (1.0.0): Extracting archive
  - Installing league/container (3.3.4): Extracting archive
  - Installing psr/http-factory (1.0.1): Extracting archive
  - Installing laminas/laminas-zendframework-bridge (1.2.0): Extracting archive
  - Installing laminas/laminas-diactoros (2.5.0): Extracting archive
  - Installing laminas/laminas-httphandlerrunner (1.3.0): Extracting archive
  - Installing composer/ca-bundle (1.2.9): Extracting archive
  - Installing cakephp/chronos (2.1.1): Extracting archive
  - Installing cakephp/cakephp (4.2.4): Extracting archive
  - Installing cakephp/twig-view (1.2.0): Extracting archive
  - Installing cakephp/bake (2.3.0): Extracting archive
  - Installing phpstan/phpdoc-parser (0.4.9): Extracting archive
  - Installing slevomat/coding-standard (6.4.1): Extracting archive
  - Installing cakephp/cakephp-codesniffer (4.2.4): Extracting archive
  - Installing jdorn/sql-formatter (v1.2.17): Extracting archive
  - Installing symfony/polyfill-php80 (v1.22.1): Extracting archive
  - Installing symfony/process (v5.2.3): Extracting archive
  - Installing symfony/finder (v5.2.3): Extracting archive
  - Installing symfony/filesystem (v5.2.3): Extracting archive
  - Installing symfony/polyfill-intl-normalizer (v1.22.1): Extracting archive
  - Installing symfony/polyfill-intl-grapheme (v1.22.1): Extracting archive
  - Installing symfony/string (v5.2.3): Extracting archive
  - Installing symfony/service-contracts (v2.2.0): Extracting archive
  - Installing symfony/polyfill-php73 (v1.22.1): Extracting archive
  - Installing symfony/console (v5.2.3): Extracting archive
  - Installing seld/phar-utils (1.1.1): Extracting archive
  - Installing seld/jsonlint (1.8.3): Extracting archive
  - Installing react/promise (v2.8.0): Extracting archive
  - Installing justinrainbow/json-schema (5.2.10): Extracting archive
  - Installing composer/xdebug-handler (1.4.5): Extracting archive
  - Installing composer/spdx-licenses (1.5.5): Extracting archive
  - Installing composer/semver (3.2.4): Extracting archive
  - Installing composer/composer (2.0.11): Extracting archive
  - Installing cakephp/debug_kit (4.4.1): Extracting archive
  - Installing symfony/deprecation-contracts (v2.2.0): Extracting archive
  - Installing symfony/config (v5.2.3): Extracting archive
  - Installing robmorgan/phinx (0.12.5): Extracting archive
  - Installing cakephp/migrations (3.0.0): Extracting archive
  - Installing m1/env (2.2.0): Extracting archive
  - Installing josegonzalez/dotenv (3.2.0): Extracting archive
  - Installing mobiledetect/mobiledetectlib (2.8.37): Extracting archive
  - Installing webmozart/assert (1.9.1): Extracting archive
  - Installing phpdocumentor/reflection-common (2.2.0): Extracting archive
  - Installing phpdocumentor/type-resolver (1.4.0): Extracting archive
  - Installing phpdocumentor/reflection-docblock (5.2.2): Extracting archive
  - Installing sebastian/version (3.0.2): Extracting archive
  - Installing sebastian/type (2.3.1): Extracting archive
  - Installing sebastian/resource-operations (3.0.3): Extracting archive
  - Installing sebastian/recursion-context (4.0.4): Extracting archive
  - Installing sebastian/object-reflector (2.0.4): Extracting archive
  - Installing sebastian/object-enumerator (4.0.4): Extracting archive
  - Installing sebastian/global-state (5.0.2): Extracting archive
  - Installing sebastian/exporter (4.0.3): Extracting archive
  - Installing sebastian/environment (5.1.3): Extracting archive
  - Installing sebastian/diff (4.0.4): Extracting archive
  - Installing sebastian/comparator (4.0.6): Extracting archive
  - Installing sebastian/code-unit (1.0.8): Extracting archive
  - Installing sebastian/cli-parser (1.0.1): Extracting archive
  - Installing phpunit/php-timer (5.0.3): Extracting archive
  - Installing phpunit/php-text-template (2.0.4): Extracting archive
  - Installing phpunit/php-invoker (3.1.1): Extracting archive
  - Installing phpunit/php-file-iterator (3.0.5): Extracting archive
  - Installing theseer/tokenizer (1.2.0): Extracting archive
  - Installing nikic/php-parser (v4.10.4): Extracting archive
  - Installing sebastian/lines-of-code (1.0.3): Extracting archive
  - Installing sebastian/complexity (2.0.2): Extracting archive
  - Installing sebastian/code-unit-reverse-lookup (2.0.3): Extracting archive
  - Installing phpunit/php-code-coverage (9.2.5): Extracting archive
  - Installing doctrine/instantiator (1.4.0): Extracting archive
  - Installing phpspec/prophecy (1.12.2): Extracting archive
  - Installing phar-io/version (3.1.0): Extracting archive
  - Installing phar-io/manifest (2.0.1): Extracting archive
  - Installing myclabs/deep-copy (1.10.2): Extracting archive
  - Installing phpunit/phpunit (9.5.2): Extracting archive
  - Installing symfony/var-dumper (v5.2.3): Extracting archive
  - Installing dnoegel/php-xdg-base-dir (v0.1.1): Extracting archive
  - Installing psy/psysh (v0.10.6): Extracting archive
19 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
54 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
PHP CodeSniffer Config installed_paths set to ../../cakephp/cakephp-codesniffer,../../slevomat/coding-standard
> App\Console\Installer::postInstall
Created `config/app_local.php` file
Created `/private/tmp/supilog/logs` directory
Created `/private/tmp/supilog/tmp/cache/views` directory
Set Folder Permissions ? (Default to Y) [Y,n]? Y
Permissions set on /private/tmp/supilog/tmp/cache
Permissions set on /private/tmp/supilog/tmp/cache/models
Permissions set on /private/tmp/supilog/tmp/cache/persistent
Permissions set on /private/tmp/supilog/tmp/cache/views
Permissions set on /private/tmp/supilog/tmp/sessions
Permissions set on /private/tmp/supilog/tmp/tests
Permissions set on /private/tmp/supilog/tmp
Permissions set on /private/tmp/supilog/logs
Updated Security.salt value in config/app_local.php

最後に「Set Folder Permissions ?」ときかれたので、Yを選択しておいた。

表示してみる

# 自分の作成したプロジェクトに移動
cd supilog

# 起動
bin/cake server
ブラウザでアクセス

http://localhost:8765/

にアクセスします。起動時に表示されたURLにしたがってください

さいごに

Laravelと同様にプロジェクト作成は想像以上に簡単だ。

実はプロジェクト作成時に「intl extension」が有効になっていない旨のエラーが出たが、macではデフォルトで無効になっているようだ。有効にする対応でも問題ないが、ちょうどphpenvで新バージョンを入れようとしていたので、そちらを行ったら問題なくインストールが出来た。

CakePHPさんありがとうございます。