cakePHPインストールの際につまづいたこと


①Apacheの設定

httpd.confを編集する

SRVROOTを変更

Define SRVROOT "C:\Program Files\Apache\Apache24"

とした。

LoadModule rewrite_module modules/mod_rewrite.so

コメントを外す

サーバーを変更

ServerName localhost:80

ディレクトリに/var/www/htmlを追加

<Directory /var/www/html>
    AllowOverride none
    Require all denied
</Directory>

ドキュメントルートを自分のアプリを作りたいパスに変更

DocumentRoot "C:\Users\-----\MyProjects"
<Directory "C:\Users\-----\MyProjects">

②php.iniの設定

PHPをインストールしたディレクトリ内のphp.iniで

extension=intl

をコメントから外し有効にする必要がある。

アプリの雛形を作成する

composer create-project --prefer-dist cakephp/app cakeSample

サーバー起動

bin\cake server # バックスラッシュ

このサイトが一番わかりやすい

Mysql

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqld --version

ログインする

\mysql -u root -p

Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

停止する

quit

databaseを作る

create database test_t;
use test_t

テーブルを作成する

create table test_t(num int, name varchar(25));
show tables;

カラム名はスネークケースが望ましい。
primary keyやauto_incrementを使用可能

テーブルの詳細

show columns from users

insert

insert into test_t values(1,'西山一郎');
select * from test_t;
+------+----------+
| num  | name     |
+------+----------+
|    1 | 西山一郎 |
+------+----------+
1 row in set (0.00 sec)

アップデート

update test_t set name="aaa" where num=1;

config/app.phpのデータベース設定を変更

user_name = "root"
passward = "************"

モデルの雛形を作成

personsテーブルを作成した後に作業ディレクトリで

bin\cake bake model persons

エラー対処

CakePHP is NOT able to connect to the database.
Connection to database could not be established: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

となりデータベースに接続できないときはhttps://improve-future.com/mysql-php-the-server-requested-authentication-method-unknown-to-the-client.htmlを参考に

mysql> select User, Plugin from mysql.user;
+------------------+-----------------------+
| User             | Plugin                |
+------------------+-----------------------+
| mysql.infoschema | caching_sha2_password |
| mysql.session    | caching_sha2_password |
| mysql.sys        | caching_sha2_password |
| root             | caching_sha2_password |
| user-name        | caching_sha2_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)

caching_sha2_passwordをnative_passwordに変更する必要がある

root@localhost [mysql]> alter user 'admin'@'%' identified WITH mysql_native_password by 'password';
root@localhost [mysql]> SELECT user, host, plugin FROM user;

これで変更できる。