setucocms (1.6.1) | 2013-03-24 21:20 |
<?php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body } }
<?php /** * インデックスコントローラー * * @author charles */ class IndexController extends Zend_Controller_Action { /** * フォームページを表示します。 * * @return void */ public function indexAction() { // フォームオブジェクト作成 $form = $this->_createForm(); $this->view->form = $form; //フォームで遷移するURLを設定する フォームのsubmitボタンからcreateActionに遷移する $form->setAction($this->_helper->url('create')); } /** * フォームの雛形を作成します。 * * @return Zend_Form */ private function _createForm() { $form = new Zend_Form(); //フォームのmethodをpostに設定する $form->setMethod('post'); //フォームでinputタグなどの入力するものを設定する (hiddenとかも) //エラーチェック及び、trim関数などでデータ整形をする $form->addElement('text', 'title', array( 'label' => 'タイトル', 'required' => true, 'filters' => array('StringTrim'), )); $form->addElement('textarea', 'content', array('label' => '内容')); $form->addElement('hidden', 'id'); $form->addElement('submit', 'submit'); // hiddenとボタン系のデコレータは必要最低限にする $form->setElementDecorators(array('ViewHelper'), array('id', 'submit')); return $form; } }
<?php //****************************追記*******************// require_once('../application/models/MemoService.php'); //***************************************************// class IndexController extends Zend_Controller_Action { //*****************************************************************************// /** 処理を委譲するサービスクラス * * @var MemoService */ private $_memoService; public function init() { //サービスクラスのインスタンスを生成して、クラスメンバー変数として保持する $this->_memoService = new MemoService(); } //***********************************************************************************// public function indexAction() { //フォームオブジェクト作成 $form = $this->_createForm(); //ビュークラスにフォームクラスを渡す $this->view->form = $form; //フォームで遷移するURLを設定する フォームのsubmitボタンからcreateActionに遷移する $form->setAction($this->_helper->url('create')); } /** * フォームのクラスを作成します * * */ private function _createForm() { $form = new Zend_Form(); //フォームのmethodをpostに設定する $form->setMethod('post'); //フォームでinputタグなどの入力するものを設定する $form->addElement('text', 'title', array( 'label' => 'タイトル', 'required' => true, 'filters' => array('StringTrim') )); $form->addElement('textarea', 'content', array('label' => '内容')); $form->addElement('hidden', 'id'); $form->addElement('submit', 'submit'); $form->setElementDecorators(array('ViewHelper'), array('id', 'submit')); return $form; } //**********************************************************************************// /** * メモを新規作成します。 * * @return void */ public function createAction() { // フォームオブジェクト作成 $form = $this->_createForm(); //レンダリングさせないようにする $this->_helper->viewRenderer->setNoRender(); // パラメータチェック if (!$form->isValid($_POST)) { $this->_setParam('form', $form); return $this->_forward('index'); } // 作成処理 $id = $this->_memoService->createMemo($form->getValues()); // 作成したメモの表示 $this->_redirect('/index/index/id/' . $id); } //********************************************************************************// }
[PageInfo]
LastUpdate: 2010-07-31 10:17:17, ModifiedBy: mars-3
[License]
Creative Commons 2.1 Attribution-ShareAlike
[Permissions]
view:all, edit:members, delete/config:members