PHPでcakephpプログラムを作ってみる

Composerのインストール

とりあえず、何か作ってみたいと思います。どうしたら良いですか?

いきなり、冒頭から丸投げ依存モードね。まあいいわ。凄いレベルでやるからついてきてね。コンソールから、raspberripiに接続してあるかな?さあ、いきますよ!
composerというパッケージ管理ソフトからインストールします

pi@raspberrypi:~ $ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer (version 1.9.3) successfully installed to: /home/pi/composer.phar
Use it: php composer.phar

pi@raspberrypi:~ $ sudo mv composer.phar /usr/local/bin/composer
pi@raspberrypi:~ $ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/

cakephp4のインストール

今回はPHPの世界でも有名なcakephp4をインストールしていきます。分かりやすいように、多少強引に説明しておきます。もっと良い方法があるんじゃないかと思えるようになったら、成長した証よ。
引き続き、コマンドでいくわよ。

pi@raspberrypi:~ $
pi@raspberrypi:~ $ cd /var/www
pi@raspberrypi:/var/www $ rm html/* -rf
pi@raspberrypi:/var/www $ composer create-project --prefer-dist cakephp/app:4.* html
Installing cakephp/app (4.0.3)
  - Installing cakephp/app (4.0.3): Loading from cache
    Failed to download cakephp/app from dist: Could not delete html/:
    Now trying to download from source
  - Installing cakephp/app (4.0.3): Cloning d03b21bd07 from cache
Created project in html
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 83 installs, 0 updates, 0 removals
  - Installing cakephp/plugin-installer (1.2.0): Downloading (100%)
  - Installing m1/env (2.1.2): Downloading (100%)
  - Installing josegonzalez/dotenv (3.2.0): Downloading (100%)
  - Installing psr/http-message (1.0.1): Downloading (100%)
  - Installing psr/http-server-handler (1.0.1): Downloading (100%)
  - Installing psr/http-factory (1.0.1): Downloading (100%)
  - Installing laminas/laminas-zendframework-bridge (1.0.1): Downloading (100%)
  - Installing laminas/laminas-diactoros (2.2.2): Downloading (100%)
  - Installing laminas/laminas-httphandlerrunner (1.1.0): Downloading (100%)
  - Installing psr/simple-cache (1.0.1): Downloading (100%)
  - Installing psr/log (1.1.2): Downloading (100%)
  - Installing psr/http-server-middleware (1.0.1): Downloading (100%)
  - Installing psr/http-client (1.0.0): Downloading (100%)
  - Installing composer/ca-bundle (1.2.6): Downloading (100%)
  - Installing cakephp/chronos (2.0.2): Downloading (100%)
  - Installing aura/intl (3.0.0): Downloading (100%)
  - Installing cakephp/cakephp (4.0.3): Downloading (100%)
  - Installing symfony/polyfill-ctype (v1.14.0): Downloading (100%)
  - Installing symfony/yaml (v5.0.4): Downloading (100%)
  - Installing symfony/filesystem (v5.0.4): Downloading (100%)
  - Installing symfony/config (v5.0.4): Downloading (100%)
  - Installing psr/container (1.0.0): Downloading (100%)
  - Installing symfony/service-contracts (v2.0.1): Downloading (100%)
  - Installing symfony/polyfill-php73 (v1.14.0): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.14.0): Downloading (100%)
  - Installing symfony/console (v5.0.4): Downloading (100%)
  - Installing robmorgan/phinx (dev-0.next 7b3ca6c): Downloading (100%)
  - Installing cakephp/migrations (3.0.0-beta2): Downloading (100%)
  - Installing mobiledetect/mobiledetectlib (2.8.34): Downloading (100%)
  - Installing twig/twig (v3.0.3): Downloading (100%)
  - Installing twig/markdown-extra (v3.0.3): Downloading (100%)
  - Installing jasny/twig-extensions (v1.3.0): Downloading (100%)
  - Installing wyrihaximus/twig-view (5.0.1): Downloading (100%)
  - Installing cakephp/bake (2.0.4): Downloading (100%)
  - Installing squizlabs/php_codesniffer (3.5.4): Downloading (100%)
  - Installing phpstan/phpdoc-parser (0.4.3): Downloading (100%)
  - Installing slevomat/coding-standard (6.1.5): Downloading (100%)
  - Installing cakephp/cakephp-codesniffer (4.0.1): Downloading (100%)
  - Installing jdorn/sql-formatter (v1.2.17): Downloading (100%)
  - Installing symfony/process (v5.0.4): Downloading (100%)
  - Installing symfony/finder (v5.0.4): Downloading (100%)
  - Installing composer/xdebug-handler (1.4.0): Downloading (100%)
  - Installing composer/spdx-licenses (1.5.3): Downloading (100%)
  - Installing justinrainbow/json-schema (5.2.9): Downloading (100%)
  - Installing seld/phar-utils (1.0.2): Downloading (100%)
  - Installing seld/jsonlint (1.7.2): Downloading (100%)
  - Installing composer/semver (1.5.1): Downloading (100%)
  - Installing composer/composer (dev-master 8fd70d2): Downloading (100%)
  - Installing cakephp/debug_kit (4.0.5): Downloading (100%)
  - Installing sebastian/version (2.0.1): Downloading (100%)
  - Installing sebastian/type (1.1.3): Downloading (100%)
  - Installing sebastian/resource-operations (2.0.1): 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 (3.0.0): Downloading (100%)
  - Installing sebastian/exporter (3.1.2): Downloading (100%)
  - Installing sebastian/environment (4.2.3): Downloading (100%)
  - Installing sebastian/diff (3.0.2): Downloading (100%)
  - Installing sebastian/comparator (3.0.2): Downloading (100%)
  - Installing phpunit/php-timer (2.1.2): Downloading (100%)
  - Installing phpunit/php-text-template (1.2.1): Downloading (100%)
  - Installing phpunit/php-file-iterator (2.0.2): Downloading (100%)
  - Installing theseer/tokenizer (1.1.3): Downloading (100%)
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Downloading (100%)
  - Installing phpunit/php-token-stream (3.1.1): Downloading (100%)
  - Installing phpunit/php-code-coverage (7.0.10): Downloading (100%)
  - Installing doctrine/instantiator (1.3.0): Downloading (100%)
  - Installing phpdocumentor/reflection-common (2.0.0): Downloading (100%)
  - Installing phpdocumentor/type-resolver (1.0.1): Downloading (100%)
  - Installing webmozart/assert (1.6.0): Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (5.0.0): Downloading (100%)
  - Installing phpspec/prophecy (v1.10.2): Downloading (100%)
  - Installing phar-io/version (2.0.1): Downloading (100%)
  - Installing phar-io/manifest (1.0.3): Downloading (100%)
  - Installing myclabs/deep-copy (1.9.5): Downloading (100%)
  - Installing phpunit/phpunit (8.5.2): 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 (v0.1.1): Downloading (100%)
  - Installing nikic/php-parser (v4.3.0): Downloading (100%)
  - Installing symfony/var-dumper (v5.0.4): Downloading (100%)
  - Installing psy/psysh (v0.9.12): 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/Psalm compatibility.)
cakephp/app suggests installing phpstan/phpstan (PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code.)
m1/env suggests installing m1/vars (For loading of configs)
cakephp/cakephp suggests installing ext-curl (To enable more efficient network calls in Http\Client.)
cakephp/cakephp suggests installing lib-ICU (The intl PHP library, to use Text::transliterate() or Text::slug())
cakephp/cakephp suggests installing paragonie/csp-builder (CSP builder, to use the CSP Middleware)
symfony/service-contracts suggests installing symfony/service-implementation
symfony/console suggests installing symfony/event-dispatcher
symfony/console suggests installing symfony/lock
cakephp/debug_kit suggests installing ext-pdo_sqlite (DebugKit needs to store panel data in a database. SQLite is simple and easy to use.)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.7.2)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0.0)
phpunit/phpunit suggests installing ext-soap (*)
phpunit/phpunit suggests installing ext-xdebug (*)
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.)
Writing lock file
Generating autoload files
> Cake\Composer\Installer\PluginInstaller::postAutoloadDump
Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]? Y
> App\Console\Installer::postInstall
Created `config/app_local.php` file
Created `/var/www/html/logs` directory
Created `/var/www/html/tmp/cache/views` directory
Set Folder Permissions ? (Default to Y) [Y,n]? Y
Permissions set on /var/www/html/tmp/cache
Permissions set on /var/www/html/tmp/cache/models
Permissions set on /var/www/html/tmp/cache/persistent
Permissions set on /var/www/html/tmp/cache/views
Permissions set on /var/www/html/tmp/sessions
Permissions set on /var/www/html/tmp/tests
Permissions set on /var/www/html/tmp
Permissions set on /var/www/html/logs
Updated Security.salt value in config/app_local.php
pi@raspberrypi:/var/www $

思わず、http://192.168.0.10/ とやってみる
※raspberrypiのIPアドレスが192.168.0.10の場合

上のように何かが表示されるはずです。でももうちょいですよ
config/app_local.phpファイルを編集してみます

//36行目あたり.....
    'Datasources' => [
        'default' => [
            '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' => 'yamada',
            'password' => 'tarou',
            'database' => 'food',

上記のように、データーベースの接続情報を修正してみましょう。
あと、実はデザインがおかしいです。mod_rewriteを有効化させましょう
a2enmod という命令を使います

pi@raspberrypi:~ $ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2
pi@raspberrypi:~ $ sudo systemctl restart apache2

Adminerのインストール

超初心者向けということで、今回はAdminerをインストールします

pi@raspberrypi:~ $ sudo wget http://www.adminer.org/latest.php -o /var/www/html/adminer.php

http://192.168.1.10/adminer.php
とアクセスすることで、データベースの中身を簡単に見れるようになりました。分かりやすいかも。

Githubにアップしよう

せっかくここまでやったので、保存しておきたいのですが。

そうね。せっかくなのでGitHubの使い方にもなれておきましょう。お友だちと共有するのにも役立ちそうですよ

まずは、インストール

pi@raspberrypi:~ $ sudo apt-get install git-flow
pi@raspberrypi:~ $ git flow
usage: git flow <subcommand>

Available subcommands are:
   init      Initialize a new git repo with support for the branching model.
   feature   Manage your feature branches.
   bugfix    Manage your bugfix branches.
   release   Manage your release branches.
   hotfix    Manage your hotfix branches.
   support   Manage your support branches.
   version   Shows version information.
   config    Manage your git-flow configuration.
   log       Show log deviating from base branch.

Try 'git flow <subcommand> help' for details.

こんなふうに出来たらインストールはOK。
まずは、バージョン管理に入れるための初期設定から。
今回は、/var/www/htmlに配置しているので、そのディレクトリでやります

pi@raspberrypi:~ $ cd /var/www/html
pi@raspberrypi:/var/www/html $ git init
Initialized empty Git repository in /var/www/html/.git/

すでに作成ずみのgithubのレポジトリにアップできるようにしましょう。まだない人は作成してね。

pi@raspberrypi:/var/www/html $ git config --global user.email "[email protected]"
pi@raspberrypi:/var/www/html $ git config --global user.name "myname"
pi@raspberrypi:/var/www/html $ git flow init
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [] feature/
Bugfix branches? [] bugfix/
Release branches? [] release/
Hotfix branches? [] hotfix/
Support branches? [] support/
Version tag prefix? [] 
Hooks and filters directory? [/var/www/html/.git/hooks]
pi@raspberrypi:/var/www/html $ git add .
pi@raspberrypi:/var/www/html $ git commit -m "my first commit"
pi@raspberrypi:/var/www/html $ git remote add origin [email protected]:my-githubaccout-name/repo-name.git
pi@raspberrypi:/var/www/html $ git push origin develop

あれ、権限がないので、最後の行でエラーが出ました。鍵認証でしたっけ?

そうね。では、githubにアップできるように設定していきましょう

githubに繋がる鍵を作成する

pi@raspberrypi:/var/www/html $ cd ~/.ssh
pi@raspberrypi:~/.ssh $ ls
authorized_keys  known_hosts
## ここでid_rsaなどがあれば、以下、別のものを指定する。たとえば、id_raspberrypiなど何でもOK
pi@raspberrypi:~/.ssh $ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/pi/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/pi/.ssh/id_rsa.
Your public key has been saved in /home/pi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LYxAHubEsGps6zbfY0ThPDovIZnLixhQBKB7dg8b7OU pi@raspberrypi
The key's randomart image is:
+---[RSA 2048]----+
|+.oo=            |
|o.o B E          |
|+* ..+           |
|+o+.o..          |
+----[SHA256]-----+
pi@raspberrypi:~/.ssh $ ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
pi@raspberrypi:~/.ssh $ cat id_rsa.pub
.............................................
.............................................

このid_rsa.pubのファイルの中身をgithubに登録します
githubの画面、右上の写真をクリックして出てくるメニューで、Settingをクリックします

上の図で、SSHのところにある、New SSH keyをクリックします。
フォームがでてくるので、先程コンソールで、 cat id_rsa.pub と打ち込んだときに表示されたものを登録します

再度、コンソールに戻って、もう一度pushしてみましょう

pi@raspberrypi:/var/www/html $ git push origin develop
Enumerating objects: 105, done.
Counting objects: 100% (105/105), done.
Delta compression using up to 4 threads
Compressing objects: 100% (90/90), done.
Writing objects: 100% (105/105), 271.62 KiB | 1.48 MiB/s, done.
Total 105 (delta 12), reused 0 (delta 0)
remote: Resolving deltas: 100% (12/12), done.
To github.com:hashimototakeshi/foodloss.git
 * [new branch]      develop -> develop

できました。

長くなってきたので、また次回
https://raspberrypi.fleuraison.net/archives/84

コメント

タイトルとURLをコピーしました