それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

CakePHP 3.0.5を入れてパパっとAdminページを作りたい

RDBMS すら面倒くさい。

めったにRDBMSなんて使わないんだ。データの格納なら、json + jq や xml + xpath 、 ファイル+grep で殆どの場合で用が足りてしまうので、個人でRDBMS使うときってほとんどない。。。

サーバーまたぐならredis が高速で便利だし。

RDBMSを使うときにはAdminページがほしい。

テーブルのカンタンなメンテナンスでphpmyadmin とか入れるけど、ぱぱっとやりたい時にやっぱり不便。

そこでcakephpでメンテすることにした。

cakephp で既存DBの管理ページをさくっと

適当な作業ディレクトリ作って

takuya@atom:~/public_html$ mkdir yubin
takuya@atom:~/public_html$ cd yubin/

composer.phar もってきて

takuya@atom:~/public_html/yubin$ curl -s https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /home/takuya/public_html/yubin/composer.phar
Use it: php composer.phar
takuya@atom:~/public_html/yubin$ ls
composer.phar

cakephp を持ってきて

takuya@atom:~/public_html/yubin$ php composer.phar create-project --prefer-dist cakephp/app 
Installing cakephp/app (3.0.5)
  - Installing cakephp/app (3.0.5)
    Downloading: 100%

  1 "require": {
Created project in tracking
Loading composer repositories with package information
Installing dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - cakephp/cakephp 3.0.x-dev requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.1.x-dev requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.1.0-beta2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.1.0-beta requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.1.0-RC1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.9 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.8 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.7 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.6 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.5 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.4 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.3 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.13 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.12 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.11 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.10 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-beta3 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-beta2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-beta1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-alpha2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-alpha1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-RC2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0-RC1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.0.0 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - Installation request for cakephp/cakephp ~3.0 -> satisfiable by cakephp/cakephp[3.0.0, 3.0.0-RC1, 3.0.0-RC2, 3.0.0-alpha1, 3.0.0-alpha2, 3.0.0-beta1, 3.0.0-beta2, 3.0.0-beta3, 3.0.1, 3.0.10, 3.0.11, 3.0.12, 3.0.13, 3.0.2, 3.0.3, 3.0.4, 3.0.5, 3.0.6, 3.0.7, 3.0.8, 3.0.9, 3.1.0-RC1, 3.1.0-beta, 3.1.0-beta2, 3.1.x-dev, 3.0.x-dev].

エラーが出た。 php の extension intl がないって

intl ? internationlization かな?

とりあえず、aptで探して入れておく

http://php.net/manual/en/intl.installation.php
takuya@atom:~/public_html/yubin$ sudo aptitude install php5-intl

getting started にはパーミッションが云々書いているけど、ウチはsuphp とか suexec 環境なのでスルー

うちは、debian のLTSなので、ちょっと古いからphp一式をアップグレード

この解決方法を受け入れますか? [Y/n/q/?]n
以下のアクションでこれらの依存関係の問題は解決されます:

     以下のパッケージを更新する:
1)     libapache2-mod-php5 [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]
2)     php5-cgi [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]
3)     php5-cli [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]
4)     php5-curl [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]
5)     php5-gd [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]
6)     php5-mcrypt [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]
7)     php5-mysql [5.4.36-0+deb7u3 (now) -> 5.4.44-0+deb7u1 (oldstable)]

この解決方法を受け入れますか? [Y/n/q/?]y
以下の新規パッケージがインストールされます:
  php5-intl

インストールおわったので、再度トライ

takuya@atom:~/public_html/yubin$ php composer.phar create-project --prefer-dist cakephp/app 
takuya@atom:~/public_html/yubin$ php composer.phar create-project --prefer-dist cakephp/app
Installing cakephp/app (3.0.5)
  - Installing cakephp/app (3.0.5)
    Loading from cache

Created project in /home/takuya/public_html/yubin/app
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing aura/installer-default (1.0.0)
    Downloading: 100%

  - Installing cakephp/plugin-installer (0.0.12)
    Downloading: 100%

  - Installing psr/log (1.0.0)
    Downloading: 100%

  - Installing nesbot/carbon (1.13.0)
    Downloading: 100%
(以下略

とりあえず、インストールおわったので git init && git add . &&  git commit  しておく

git init && git add . &&  git commit -m "CakePHPの初期状態"

サーバーを起動

takuya@atom:~/public_html/yubin$ app/bin/cake server -H 192.168.2.9 -p 5673

Welcome to CakePHP v3.0.13 Console
---------------------------------------------------------------
App : src
Path: /home/takuya/public_html/yubin/app/src/
DocumentRoot: /home/takuya/public_html/yubin/app/webroot
---------------------------------------------------------------
built-in server is running in http://192.168.2.9:5673/
You can exit with `CTRL-C`
[Wed Sep  9 04:52:10 2015] 192.168.2.130:54804 [200]: /css/base.css
[Wed Sep  9 04:52:10 2015] 192.168.2.130:54805 [200]: /css/cake.css
[Wed Sep  9 04:52:11 2015] 192.168.2.130:54807 [200]: /favicon.ico

f:id:takuya_1st:20151003224027p:plain

これで、cakephp のインスト確認オッケー

で、serverとか面倒なので、mod_rewriteに頼る

RewriteBaseを入れておく。

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase    /~takuya/yubin/
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

データベースを使うぞっと。

使い捨てにはsqlite3が管理が楽なので、SQLiteを使う。

sudo aptitude install php5-sqlite
cd CAKE_ROOT
mkdir db

適宜、Sqliteに書き換えてっと

    /**
     * Connection information used by the ORM to connect
     * to your application's datastores.
     * Drivers include Mysql Postgres Sqlite Sqlserver
     * See vendor\cakephp\cakephp\src\Database\Driver for complete list
     */
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Sqlite',
            'database' => dirname(__FILE__).'/../db/my_database',
            /**
             * 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' => 'nonstandard_port_number',
            #'username' => 'my_app',
            #'password' => 'secret',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'log' => false,

            /**
             * Set identifier quoting to true if you are

index.phpでDB使えたのを確認して、

f:id:takuya_1st:20151003224039p:plain

あとは、データベースにテーブル作って

sqlite3 >>CREATE TABLE packets (
  id integer primary key autoincrement ,
  code  char(15) unique not null ,
  status  char(255),
  created datetime,
  modified datetime
);

そして、bake all する

takuya@atom:~/public_html/yubin$ bin/cake bake all packets

Welcome to CakePHP v3.0.13 Console
---------------------------------------------------------------
App : src
Path: /home/takuya/public_html/yubin/src/
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
One moment while associations are detected.

Baking table class for Packets...

f:id:takuya_1st:20151003224034p:plain

これで、カンタンにマスタメンテナンスの出来上がりっと。

参考サイト

Installation — CakePHP Cookbook 3.x documentation