レクチャー勉強会の資料

レクチャー勉強会資料 IndexController

初期状態

<?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);        
  }

//********************************************************************************//
}