UbuntuでXAMPPをいれてphpMyAdminを動かすまで
http://blog.goo.ne.jp/xmldtp/e/7d58d41c33be01cb28e92e1e022dc886
のつづき。CakePHPでBakeするまで。
■手順
●0.前提
XAMPPは入れた。
Apache,DB(MySQL),PHPは動作しているものとする。
MySQLのデータベース test に、以下のuserテーブル(項目id,name)がある
--------------------- ここから --------------------
●1.ダウンロードしてくる
http://cakephp.jp/から
●2.解凍して、フォルダの名前を変える
myappとか
●3.そのフォルダをapacheのhtdocs(/opt/lampp/htdocs/)の下にいれる。
ただし、htdocsにコピー権限がない場合
sudo chmod 777
して、コピーできるようにする。コピーするmyappも読み書きできるように
●4.この段階で、ブラウザで確認
http://localhost/myapp
ブラウザから、Cake PHPが確認できればOK
→この段階では、DBに繋がっていないので、DBのエラーが出るならOK
そうでなく、ファイルの権限のエラーの場合は、権限付与をする
また Notice (1024):エラーは、表示されたapp/config/core.phpを適当に直す
/**
* A random string used in security hashing methods.
*/
Configure::write('Security.salt', 'abcdefg12345');
/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
Configure::write('Security.cipherSeed', '1234567890');
|
なかんじ
参考
http://gecko.hp2.jp/2010/05/12/cakephp%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab%e3%81%97%e3%81%9f%e3%82%89notice-1024%e3%81%8c%e5%87%ba%e3%81%9f%e3%80%82/
●5.コマンドプロンプトを立ち上げて、
コマンドプロンプトから、上記4で作成したフォルダの下の
(例では、/opt/lampp/htdoc/myappの下の)
app/config/database.php.defaultをコピー、
コピーしたファイル名をdatabase.phpとする
●6.コピーしたdatabase.phpを開き、以下のようにDATABASE_CONFIGを修正して保存する
(ユーザー名lampp,パスワードなし、DB名test、プリフィックスなしの場合)
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'lampp',
'password' => '',
'database' => 'test',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'lampp',
'password' => '',
'database' => 'test',
'prefix' => '',
//'encoding' => 'utf8',
);
}
|
つまり、ユーザー名、パスワード、データベース名を修正する。
●7.この段階で、ブラウザで確認
http://localhost/myapp
ブラウザから、Cake PHPが確認でき、DBに繋がっていればOK
→つながっていなかったら、myapp/app/config/database.phpを確認
●8.myapp/app/Consoleに行き、
※Xampp for Linux(Lampp)の場合
cake.phpをviで開き、1行目を以下のように書き換える
下のコマンドをコマンドラインから打つ
cake bake
すると、メニューが出る
●8-1.はじめに、モデルを作るので、mを入力
→プライマリーキーなどを聞いてくる。適当に答えて作成する
たとえば、こんなかんじ
Welcome to CakePHP v2.2.2 Console
---------------------------------------------------------------
App : app
Path: /opt/lampp/htdocs/myapp/app/
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
> M
---------------------------------------------------------------
Bake Model
Path: /opt/lampp/htdocs/myapp/app/Model/
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Models based on your current database:
1. User
Enter a number from the list above,
type in the name of another model, or 'q' to exit
[q] > 1
Would you like to supply validation criteria
for the fields in your model? (y/n)
[y] > y
Field: id
Type: integer
---------------------------------------------------------------
Please select one of the following validation options:
---------------------------------------------------------------
1. alphanumeric 18. mimetype
2. between 19. minlength
3. blank 20. money
4. boolean 21. multiple
5. cc 22. naturalnumber
6. comparison 23. notempty
7. custom 24. numeric
8. date 25. phone
9. datetime 26. postal
10. decimal 27. range
11. email 28. ssn
12. equalto 29. time
13. extension 30. uploaderror
14. inlist 31. url
15. ip 32. userdefined
16. luhn 33. uuid
17. maxlength
34 - Do not do any validation on this field.
---------------------------------------------------------------
... or enter in a valid regex validation string.
[34] > 1
Would you like to add another validation rule? (y/n)
[n] > n
Field: name
Type: string
---------------------------------------------------------------
Please select one of the following validation options:
---------------------------------------------------------------
1. alphanumeric 18. mimetype
2. between 19. minlength
3. blank 20. money
4. boolean 21. multiple
5. cc 22. naturalnumber
6. comparison 23. notempty
7. custom 24. numeric
8. date 25. phone
9. datetime 26. postal
10. decimal 27. range
11. email 28. ssn
12. equalto 29. time
13. extension 30. uploaderror
14. inlist 31. url
15. ip 32. userdefined
16. luhn 33. uuid
17. maxlength
34 - Do not do any validation on this field.
---------------------------------------------------------------
... or enter in a valid regex validation string.
[23] > 34
Would you like to define model associations
(hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > n
---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name: User
DB Table: `test`.`user`
Validation: Array
(
[id] => Array
(
[alphanumeric] => alphanumeric
)
)
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Baking model class for User...
Creating file /opt/lampp/htdocs/myapp/app/Model/User.php
Wrote `/opt/lampp/htdocs/myapp/app/Model/User.php`
PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
|
●8-2.次に、コントローラーを作るので、cを入力
→クラスメソッドを作るかなど聞いてくる。適当に答えて作成する
たとえば、こんなかんじ
---------------------------------------------------------------
Bake Controller
Path: /opt/lampp/htdocs/myapp/app/Controller/
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Controllers based on your current database:
---------------------------------------------------------------
1. Users
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 1
---------------------------------------------------------------
Baking UsersController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] > y
Would you like to use dynamic scaffolding? (y/n)
[n] > y
---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:
Users
public $scaffold;
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Baking controller class for Users...
Creating file /opt/lampp/htdocs/myapp/app/Controller/UsersController.php
Wrote `/opt/lampp/htdocs/myapp/app/Controller/UsersController.php`
PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
|
●8-3.そうしたら、ビューを作る。vを入力
→どんなビューを作るか聞いてくる。適当に答えて作成する
たとえば、こんなかんじ
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
> V
---------------------------------------------------------------
Bake View
Path: /opt/lampp/htdocs/myapp/app/View/
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Controllers based on your current database:
---------------------------------------------------------------
1. Users
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 1
Would you like bake to build your views interactively?
Warning: Choosing no will overwrite Users views if it exist. (y/n)
[n] > y
Would you like to create some CRUD views
(index, add, view, edit) for this controller?
NOTE: Before doing so, you'll need to create your controller
and model classes (including associated models). (y/n)
[y] > y
Would you like to create the views for admin routing? (y/n)
[n] > y
Baking `index` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/index.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/index.ctp`
Baking `view` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/view.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/view.ctp`
Baking `add` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/add.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/add.ctp`
Baking `edit` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/edit.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/edit.ctp`
---------------------------------------------------------------
You need to enable Configure::write('Routing.prefixes',array('admin')) in /app/Config/core.php to use prefix routing.
What would you like the prefix route to be?
Example: www.example.com/admin/controller
Enter a routing prefix:
[admin] >
Baking `admin_index` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/admin_index.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/admin_index.ctp`
Baking `admin_view` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/admin_view.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/admin_view.ctp`
Baking `admin_add` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/admin_add.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/admin_add.ctp`
Baking `admin_edit` view file...
Creating file /opt/lampp/htdocs/myapp/app/View/Users/admin_edit.ctp
Wrote `/opt/lampp/htdocs/myapp/app/View/Users/admin_edit.ctp`
---------------------------------------------------------------
View Scaffolding Complete.
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
> Q
|
●9.この段階で、ブラウザで確認
http://localhost/myapp/users
●10.レイアウトの変更など
全体にかかわること・・・view/layout
CSS・・・app/webroot/css
個々の画面・・・それぞれのviewに対してできるctpファイル
※8以降に関しては、いろいろなサイトに出ている。