<body> <header class="row"> <h1><?=$title ?></h1> </header> <div class="row"> <table> <form method="post" action="/mycakeapp/hello/form"> <input type="hidden" name="_csrfToken" value="<?= $this->request->getParam('_csrfToken') ?>"> <tr><th>name</th><td><input type="text" name="name"></td></tr> <tr><th>mail</th><td><input type="text" name="mail"></td></tr> <tr><th>age</th><td><input type="number" name="age"></td></tr> <tr><th></th><button>Click</button></tr> </form> </table> </div> </body>
<body> <header class="row"> <h1><?=$title ?></h1> </header> <div class="row"> <p><?=$message ?></p> </div> </body>
HelloController
class HelloController extends AppController { public function index(){ $this->viewBuilder()->autoLayout(false); $this->set('title', 'Hello!'); } public function form(){ $this->viewBuilder()->autoLayout(false); $name = $this->request->data['name']; $mail = $this->request->data['mail']; $age = $this->request->data['age']; $res = "こんにちは、" . $name . "(" . $age . ")さん。メールアドレスは、" . $mail . " ですね?"; $values = [ "title" => "Result", "message" => $res ]; $this->set($values); } }
$this->request->data[“name”] でデータを取り出している
### エスケープ処理
<body> <header class="row"> <h1><?=h($title) ?></h1> </header> <div class="row"> <p><?=h($message) ?></p> </div> </body>
### フォームヘルパー
<table> <?=$this->Form->create(null, ["type"=>"post", "url"=>["controller"=>"Hello", "action"=>"index"]])?> <input type="hidden" name="_csrfToken" value="<?= $this->request->getParam('_csrfToken') ?>"> <tr><th>name</th><td><?=$this->Form->text("Form1.name") ?></td></tr> <tr><th>mail</th><td><?=$this->Form->text("Form1.mail") ?></td></tr> <tr><th>age</th><td><?=$this->Form->text("Form1.age") ?></td></tr> <tr><th></th><td><?=$this->Form->submit("送信") ?></td></tr> <?=$this->Form->end() ?> </table>
Controller
public function index(){ $this->viewBuilder()->autoLayout(false); $this->set('title', 'Hello!'); if($this->request->isPost()) { $this->set('data', $this->request->data['Form1']); } else { $this->set('data', []);
フォームヘルパーはcontrollerで呼び出さずに利用できる
なるほど