2016/2/1 久米
| ファイル | コード例 | 備考 | |
| C | src>Controller> NekoChansController.php |
<?php
namespace App\Controller;
use App\Controller\AppController;
class NekoChansController extends AppController{
public function index(){
$nekoChans = $this->NekoChans->find();
$this->set('nekoChans', $nekoChans);
}
public function add(){
if($this->request->is('post')){
$nekoChan = $this->NekoChans->newEntity();
$this->NekoChans->patchEntity($nekoChan, $this->request->data);
if($this->NekoChans->save($nekoChan)){
return $this->redirect(['action'=>'index']);
}
}
}
} |
|
| M | src>Model>Table> NekoChansTable.php |
無ければデフォルトでオブジェクト生成してくれるので。今回は不要 | |
| V その1 |
src>Template>NekoChans> index.ctp |
前ページのindex.ctpと同じ | |
| V その2 |
src>Template>NekoChans> add.ctp |
<div>
<h3>猫ちゃんず追加</h3>
<?= $this->Form->create() ?>
<fieldset>
<?php
echo $this->Form->input('name', ['label'=>'名前']);
echo $this->Form->input('tokugi', ['label'=>'特技']);
echo $this->Form->input('age', ['label'=>'年齢']);
?>
</fieldset>
<?= $this->Form->button('追加') ?>
<?= $this->Form->end() ?>
</div> |
|
| アクセス | http://localhost/web3/caketest/neko-chans/add |
演習1 index.ctpに新規追加リンクを付けてみよ
http://ホスト名/アプリケーション/テーブル/アクション/引数1/引数2/・・
http://localhost/web3/caketest/neko-chans/edit/3
とするとNekoYohinsControllerのedit関数が実行される。この時引数$idに3が入って実行される。
| ファイル | コード例 | 備考 | |
| C | src>Controller> NekoChansController.php |
<?php
namespace App\Controller;
use App\Controller\AppController;
class NekoChansController extends AppController{
public function index(){
$nekoChans = $this->NekoChans->find();
$this->set('nekoChans', $nekoChans);
}
public function edit($id = null){
$nekoChan = $this->NekoChans->get($id);
$this->set('nekoChan', $nekoChan);
if($this->request->is(['post', 'put'])){
$this->NekoChans->patchEntity($nekoChan, $this->request->data);
if($this->NekoChans->save($nekoChan)){
return $this->redirect(['action'=>'index']);
}
}
}
} |
編集ボタンを押して 飛んでくるのは postかput なのでそれを判定 caketest>vendor> cakephp>cakephp> src>View>Helper> FormHelper.php |
| M | src>Model>Table> NekoChansTable.php |
無ければデフォルトでオブジェクト生成してくれるので。今回は不要 | |
| V その1 |
src>Template>NekoChans> index.ctp |
今までのやつと同じ | |
| V その2 |
src>Template>NekoChans> edit.ctp |
<div>
<h3>猫ちゃんず編集</h3>
<?= $this->Form->create($nekoChan) ?>
<fieldset>
<?php
echo $this->Form->input('name', ['label'=>'名前']);
echo $this->Form->input('tokugi', ['label'=>'特技']);
echo $this->Form->input('age', ['label'=>'年齢']);
?>
</fieldset>
<?= $this->Form->button('編集') ?>
<?= $this->Form->end() ?>
</div> |
create($nekoChan) で渡された値が 枠の中に入る |
| アクセス | http://localhost/web3/caketest/neko-chans/edit/1 |
演習2 index.ctpに編集リンクを付けてみよ
| ファイル | コード例 | 備考 | |
| C | src>Controller> NekoChansController.php |
<?php
namespace App\Controller;
use App\Controller\AppController;
class NekoChansController extends AppController{
public function index(){
$nekoChans = $this->NekoChans->find();
$this->set('nekoChans', $nekoChans);
}
public function delete($id = null){
$nekoChan = $this->NekoChans->get($id);
$this->set('nekoChan', $nekoChan);
if($this->request->is(['post', 'put'])){
if($this->NekoChans->delete($nekoChan)){
return $this->redirect(['action'=>'index']);
}
}
}
} |
|
| M | src>Model>Table> NekoChansTable.php |
無ければデフォルトでオブジェクト生成してくれるので。今回は不要 | |
| V その1 |
src>Template>NekoChans> index.ctp |
今までのやつと同じ | |
| V その2 |
src>Template>NekoChans> delete.ctp |
<div>
<h3>猫ちゃんず削除</h3>
<?= $this->Form->create($nekoChan) ?>
<fieldset>
<p>id: <?= h($nekoChan->id); ?></p>
<p>名前: <?= h($nekoChan->name); ?></p>
<p>特技: <?= h($nekoChan->tokugi); ?></p>
<p>年齢: <?= h($nekoChan->age); ?></p>
</fieldset>
<?= $this->Form->button('削除') ?>
<?= $this->Form->end() ?>
</div> |
|
| アクセス | http://localhost/web3/caketest/neko-chans/delete/5 |
演習3 index.ctpに削除リンクを付けてみよ
演習4 ネコカフェメニューの、追加、編集、削除を作ってみよ。