m_shige1979のときどきITブログ

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

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

https://github.com/mshige1979

CakePHP 3.0.0 もくもく会(勉強会) #4に参加してもくもくしてきた

概要

イベント

CakePHP 3.0.0 もくもく会(勉強会) #4

日時

2014/04/16 (水) 19:00 - 21:30

料金

1,000円(場所代として)

場所

東京都中央区新川1-3-4 PAビル5F コワーキングスペース茅場町 Co-Edo(コエド)

人数

すくなめ…

事前準備

vagrantインストールして初期設定
git clone https://github.com/monsat/vagrant-lamp-sample -b cakephp3
cd vagrant-lamp-sample
vagrant up

これでおk


雰囲気

基本的にはもくもくする場所なので話すことがあることはないけど何も進展ないのもまずそうなので
ちょっといろいろやってみようってことになった。

私は今回始めてでしたが。・。。

bakeと使うことになった

けど私は始めてだったのでちょっと勝手に資料を漁りながらどんな感じになったのかソースを見ながら動かした

手順

SQLでテーブル作成、データ投入
CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT CURRENT_TIMESTAMP,
    modified DATETIME DEFAULT NULL
);

truncate table posts;

INSERT INTO posts (title,body) VALUES ('タイトル1', 'テスト記事1');
INSERT INTO posts (title,body) VALUES ('タイトル2', 'テスト記事2');
INSERT INTO posts (title,body) VALUES ('タイトル3', 'テスト記事3');
app\App\Controller\PostsController.phpを作成
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use App\Model\Entity;

class PostsController extends AppController {

	// ヘルパー追加
	public $helpers = array(
		'Html'		// HTMLヘルパー追加
		, 'Form'	// Formヘルパー追加
	);
	
	// Session コンポーネントを使うので追加
	public $components = array('Session');
	
	
	public function index() {
		$posts = TableRegistry::get('Posts');
		$this->set('posts', $posts->find('all'));
	}
	
	public function view($id = null) {
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}

		$posts = TableRegistry::get('Posts');
		$post = $posts->get($id);
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}
		$this->set('post', $post);
	}
	
	public function add() {
		
		// リクエストチェック
		if ($this->request->is('post')) {
			
			
			$posts = TableRegistry::get('Posts');
			
			$post = new \App\Model\Entity\Post($this->request->data('Posts'));
			
			// 値を保存する
			if ($posts->save($post)) {
				// 一時セッションにデータを保存
				$this->Session->setFlash(__('Your post has been updated.'));
				
				// 一覧画面へリダイレクト
				return $this->redirect(array('action' => 'index'));
			}
			
			// 保存失敗の場合
			$message = __('Unable to update your post.');
			$this->Session->setFlash($message);
			
		}
	}
	
}
App\Model\Entity\post.phpを作成
<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Post extends Entity {
}
App\Model\Table\PostsTable.phpを作成
<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

class PostsTable extends Table {
	
	public function validationDefault(Validator $validator) {
		$validator
			->add('title', 'notEmpty', [
				'rule' => 'notEmpty',
				'message' => 'You need to provide a title',
			])
			->add('body', 'notEmpty', [
				'rule' => 'notEmpty',
				'message' => 'A body is required',
			]);
		return $validator;
	}
	
}
app\App\Template\Posts\以下にadd.ctp、index.ctp、view.ctpを作成

add.ctp

<h1>Add Post</h1>
<?php
echo $this->Form->create('Posts');
echo $this->Form->input('Posts.title');
echo $this->Form->input('Posts.body', array('rows' => '3'));
echo $this->Form->submit('Save Post');
echo $this->Form->end();
?>

<br />
<br />
<a href="/posts">一覧へ戻る</a>

index.ctp

<h1>Blog Posts</h1>
<table>
	<tr>
		<th>Id</th>
		<th>Title</th>
		<th>Created</th>
	</tr>
	
	<?php foreach ($posts as $post): ?>
	<tr>
		<td><?php echo $post->id; ?></td>
		<td><?php echo $this->Html->link($post->title, array('controller' => 'posts', 'action' => 'view', $post->id)); ?></td>
		<td><?php echo $post->created->format('Y-m-d H:i:s'); ?></td>
	</tr>
	<?php endforeach; ?>
	<?php unset($post); ?>
</table>
<br />
<a href="/posts/add">
	<button>
		追加
	</button>
</a>

view.ctp

<a href="/posts">一覧へ戻る</a>
<br />
<br />

<h1><?php echo h($post->title); ?></h1>
<p><small>Created: <?php echo $post->created->format('Y-m-d H:i:s'); ?></small></p>
<p><?php echo h($post->body); ?></p>

<br />
<a href="/posts">一覧へ戻る</a>

なんとか動いた

なんかモデルとかでエンティティとかあるけどよくわからん

多分テーブルに対する1データに関連するなにかと思うけど…

最初に基本複数形の名前がからんでいる

名前は自由につけたいなあ






bakeも時間が開いたのでやってみた

テーブルを作成

今回はtagsテーブルとかいう名前になりました

bakeコマンドを実行
[vagrant@lamp-cakephp3-sample ~]$ sh /share/app/App/Console/cake bake controller tags

Welcome to CakePHP v3.0.0-dev2 Console
---------------------------------------------------------------
App : App
Path: /share/app/App/
---------------------------------------------------------------

Baking controller class for Tags...

Creating file /share/app/App/Controller/TagsController.php
Wrote `/share/app/App/Controller/TagsController.php`
Bake is detecting possible fixtures...

Baking test case for App\Controller\TagsController ...

Creating file /share/app/Test/TestCase/Controller/TagsControllerTest.php
Wrote `/share/app/Test/TestCase/Controller/TagsControllerTest.php`
[vagrant@lamp-cakephp3-sample ~]$ sh /share/app/App/Console/cake bake view tags

Welcome to CakePHP v3.0.0-dev2 Console
---------------------------------------------------------------
App : App
Path: /share/app/App/
---------------------------------------------------------------

Baking `index` view file...

Creating file /share/app/App/Template/Tags/index.ctp
Wrote `/share/app/App/Template/Tags/index.ctp`

Baking `view` view file...

Creating file /share/app/App/Template/Tags/view.ctp
Wrote `/share/app/App/Template/Tags/view.ctp`

Baking `add` view file...

Creating file /share/app/App/Template/Tags/add.ctp
Wrote `/share/app/App/Template/Tags/add.ctp`

Baking `edit` view file...

Creating file /share/app/App/Template/Tags/edit.ctp
Wrote `/share/app/App/Template/Tags/edit.ctp`
[vagrant@lamp-cakephp3-sample ~]$

mysql> select * from tags;
+----+--------+------------+---------+----------+
| id | slug   | name       | created | modified |
+----+--------+------------+---------+----------+
|  9 | aaaa   | sssss      | NULL    | NULL     |
| 10 | bbbbbb | vvvvvvvvvv | NULL    | NULL     |
+----+--------+------------+---------+----------+
2 rows in set (0.00 sec)

mysql>

なんかちゃんと動いているっぽい

f:id:m_shige1979:20140418212838j:plain

補足

ディレクトリ構成が結構変わっている
[vagrant@lamp-cakephp3-sample share]$ tree app -L 1 -d
app
|-- App
|-- Plugin
|-- Test
|-- tmp
|-- vendor
 -- webroot

6 directories
[vagrant@lamp-cakephp3-sample share]$

Viewとは別にTemplateが増えていたり、libが消えていたり結構な改変があるよう
リリースまでは時間がかかるけど早めに使えるようになりたいと思う