画像のアップロードでプラグインを探していたら、
代わりにFriendsOfCake/cakephp-uploadを使う。こちらも良さそう。チュートリアル形式のドキュメントもあるし。
Contents
ComposerでFriendsOfCake/cakephp-uploadをインストール
1 | composer require josegonzalez/cakephp-upload |
プラグインとしてロード
コマンドラインで
1 | bin/cake plugin load Josegonzalez/Upload |
とするか、直接config/bootstrap.phpの最後に以下を追記する。
1 | Plugin::load('Josegonzalez/Upload'); |
テーブル作成
bakeの形式は
1 | bin/cake bake migration Create<テーブル名> <カラム名>:<型>[?][\[<長さ>\]][:<インデックスタイプ>][:<インデックス名>] … created modified |
picturesテーブルであれば
1 | bin/cake bake migration CreatePictures title:text photo:text dir:text created modified |
等。
で、テーブルを作成する。
1 | bin/cake migrations migrate |
直接SQLを発行しても良いが、複数サイトを開発していると色々なバージョンで運用することもあり、migrationの仕組みを利用していればバージョン管理の負担が随分と軽減される。
bakeしてソースコードを生成
1 | bin/cake bake all pictures |
Behaviorの登録
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class PicturesTable extends Table { /** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->setTable('pictures'); $this->setDisplayField('title'); $this->setPrimaryKey('id'); // START: IMPORTANT PART HERE $this->addBehavior('Josegonzalez/Upload.Upload', [ 'photo' => [], ]); // END: IMPORTANT PART ABOVE } |
チュートリアル形式のドキュメントは
1 | 'photo', |
となっているが、間違いのようだ。
Templateの編集
bakeで生成されたテンプレートを以下の様に変更する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php /** * @var \App\View\AppView $this */ ?> <nav class="large-3 medium-4 columns" id="actions-sidebar"> <ul class="side-nav"> <li class="heading"><?= __('Actions') ?></li> <li><?= $this->Html->link(__('List Pictures'), ['action' => 'index']) ?></li> </ul> </nav> <div class="pictures form large-9 medium-8 columns content"> <?= $this->Form->create($picture, ['type' => 'file']) ?> <fieldset> <legend><?= __('Add Picture') ?></legend> <?php echo $this->Form->control('title'); echo $this->Form->control('photo', ['type' => 'file']); echo $this->Form->control('dir'); ?> </fieldset> <?= $this->Form->button(__('Submit')) ?> <?= $this->Form->end() ?> </div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php /** * @var \App\View\AppView $this */ ?> <nav class="large-3 medium-4 columns" id="actions-sidebar"> <ul class="side-nav"> <li class="heading"><?= __('Actions') ?></li> <li><?= $this->Form->postLink( __('Delete'), ['action' => 'delete', $picture->id], ['confirm' => __('Are you sure you want to delete # {0}?', $picture->id)] ) ?></li> <li><?= $this->Html->link(__('List Pictures'), ['action' => 'index']) ?></li> </ul> </nav> <div class="pictures form large-9 medium-8 columns content"> <?= $this->Form->create($picture, ['type' => 'file']) ?> <fieldset> <legend><?= __('Edit Picture') ?></legend> <?php echo $this->Form->control('title'); echo $this->Form->control('photo', ['type' => 'file']); echo $this->Form->control('dir'); ?> </fieldset> <?= $this->Form->button(__('Submit')) ?> <?= $this->Form->end() ?> </div> |
またまた、チュートリアル形式のドキュメントでは’file’の部分が
1 | <?= $this->Form->create($user, ['type' => file]) ?> |
となっているが、シングルクオーテーションの付け忘れだろう。
ディレクトリおよびパーミッションの設定
デフォルトではwebroot/files/<モデル名>/<フィールド名>/のディレクトリにアップロードされるので、mkdirでディレクトリを作成しておく。
注意点としては、<モデル名>は本当にモデル名なので先頭は大文字になる。また、<フィールド名>のディレクトリはWebサーバが動作するowner OR groupの書き込み権限が必要になる。(Redhat系はapache、Ubuntu系などはwww-data等)
以上で基本的に動作するが、色々とカスタマイズ出来るようになっており、AWS S3にアップロードしたい場合は、上記チュートリアル形式のドキュメントで、デフォルト動作の変更やValidation等詳しい説明はCakePHP Upload Documentationに書かれている。