m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

cakephp2でプラグインを作成

作ったことがない

ので作成する

参考資料

プラグイン — CakePHP Cookbook 2.x ドキュメント
※公式だけどなんかいまいちしっくりきていない

環境構築

mkdir app1
cd app1/
git clone https://github.com/cakephp/cakephp.git -b 2.5.3 .
git submodule add https://github.com/cakephp/debug_kit.git app/Plugin/DebugKit

※あとはいろいろとデータベースとかcore.phpとかごにょごにょする

サンプルプラグイン

プラグイン

ContactManager

コントローラーなど

contacts

モデル用のtable

CREATE TABLE `contacts` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `data` varchar(256) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

サンプル

作成
sh app/Console/cake bake plugin ContactManager
sh app/Console/cake bake controller Contacts --plugin ContactManager
sh app/Console/cake bake model Contacts --plugin ContactManager
sh app/Console/cake bake view Contacts --plugin ContactManager

※最初にテーブルを作成しておくこと

app/Config/bootstrap.php
CakePlugin::load('ContactManager', array('bootstrap' => false, 'routes' => false));

※末尾に追加されている

プラグイン構成
.
├── Config
│   └── Schema
│       └── empty
├── Console
│   └── Command
│       └── Task
│           └── empty
├── Controller
│   ├── Component
│   │   └── empty
│   ├── ContactManagerAppController.php
│   └── ContactsController.php
├── Lib
│   └── empty
├── Model
│   ├── Behavior
│   │   └── empty
│   ├── ContactManagerAppModel.php
│   ├── Contact.php
│   └── Datasource
│       └── empty
├── Test
│   ├── Case
│   │   ├── Controller
│   │   │   ├── Component
│   │   │   │   └── empty
│   │   │   └── ContactsControllerTest.php
│   │   ├── Model
│   │   │   ├── Behavior
│   │   │   │   └── empty
│   │   │   └── ContactTest.php
│   │   └── View
│   │       └── Helper
│   │           └── empty
│   └── Fixture
│       ├── ContactFixture.php
│       └── empty
├── Vendor
│   └── empty
├── View
│   ├── Contacts
│   │   ├── add.ctp
│   │   ├── edit.ctp
│   │   ├── index.ctp
│   │   └── view.ctp
│   └── Helper
│       └── empty
└── webroot
    └── empty

25 directories, 24 files
$ 

アクセス

所感

プラグインを作成することでアプリケーションとは分離した機能を付与できるかも、
ビューだけやヘルパー、モデルだけのプラグインも作成できると思われる
毎回コントローラーなどを用意したものをプラグインとして用意することも考えておく必要がありそう。

気になっていること

debugkitってどうやって出力しているんだろう?
よくわからない…