intro
前回作成した複数ページ対応の追加/更新/削除/閲覧コントローラを使用して実際に画面を作成します。ソースコードはこちらからダウンロードできます。
また、デモサイトより動作を確認できます。
また、デモサイトより動作を確認できます。
1.bootstrap.phpとconfig.iniの作成
以下のように
環境に合わせて
以下のように
環境に合わせて
この記事では以下のSQLを使用しました(MySQL)。
application/bootstrap.php
ファイルを作成します。環境に合わせて
yourBaseUrl
を適切に設定してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php set_include_path('../library' . PATH_SEPARATOR . get_include_path()); define('APP_BASE', '../application'); define('CONFIG_PATH', APP_BASE . '/config.ini'); require_once 'Zend/Controller/Front.php'; require_once 'Zend/Layout.php'; require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db/Table/Abstract.php'; $layout = Zend_Layout::startMvc(); //$layout->getView()->baseUrl = '/yourBaseUrl'; $config = new Zend_Config_Ini(CONFIG_PATH, 'staging'); $params = $config->database->params->toArray(); $params['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER; $dbAdapter = Zend_Db::factory($config->database->adapter, $params); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); Zend_Controller_Front::run(APP_BASE . '/controllers'); |
以下のように
application/config.ini
ファイルを作成します。環境に合わせて
database.params
以下を変更してください。
1 2 3 4 5 6 |
[staging] database.adapter = pdo_mysql database.params.host = localhost database.params.username = db_user database.params.password = db_password database.params.dbname = db_name |
この記事では以下のSQLを使用しました(MySQL)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
CREATE TABLE users ( id integer auto_increment NOT NULL PRIMARY KEY, username varchar(20) NOT NULL, password varchar(20) NOT NULL ); CREATE TABLE profiles ( id integer auto_increment NOT NULL PRIMARY KEY, user_id integer NOT NULL, first_name varchar(100) NOT NULL, last_name varchar(100) NOT NULL, email varchar(200) NOT NULL, url varchar(200) NOT NULL ); |
2.モデルの作成
以下のように
以下のように
次に以下のように
次に以下のように
application/models/Users.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 |
<?php require_once 'Zend/Db/Table/Abstract.php'; require_once 'Zend/Form.php'; class Users extends Zend_Db_Table_Abstract { protected $_name = 'users'; protected $_dependentTables = array('Profiles'); } |
以下のように
application/models/forms/UserForm.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php require_once 'Zend/Form.php'; class UserForm extends Zend_Form { public function __construct() { parent::__construct(); $username = $this->createElement('text', 'username'); $username->setLabel('Account Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,20)); $password = $this->createElement('password', 'password'); $password->setLabel('Password') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,20)); $this->addElements(array( $username, $password )); } } |
次に以下のように
application/models/Profiles.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php require_once 'Zend/Db/Table/Abstract.php'; require_once 'Zend/Form.php'; class Profiles extends Zend_Db_Table_Abstract { protected $_name = 'profiles'; protected $_referenceMap = array( 'Account' => array( 'columns' => 'user_id', 'refTableClass' => 'Users', 'refColumns' => 'id' ) ); } |
次に以下のように
application/models/forms/ProfileForm.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php require_once 'Zend/Form.php'; class ProfileForm extends Zend_Form { public function __construct() { parent::__construct(); $first_name = $this->createElement('text', 'first_name'); $first_name->setLabel('First Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $last_name = $this->createElement('text', 'last_name'); $last_name->setLabel('Last Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $email = $this->createElement('text', 'email'); $email->setLabel('E-Mail') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('emailAddress', false) ->addValidator('stringLength', false, array(1,200)); $url = $this->createElement('text', 'url'); $url->setLabel('URL') ->setRequired(false) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,200)); $this->addElements(array( $first_name, $last_name, $email, $url )); } } |
3.コントローラの作成
以下のように
前回作成した
application/constorllers/UserController.php
ファイルを作成します。前回作成した
My_Controller_MultiPage
クラスを継承しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php require_once 'My/Controller/MultiPage.php'; require_once APP_BASE . '/models/Users.php'; require_once APP_BASE . '/models/Profiles.php'; require_once APP_BASE . '/models/forms/UserForm.php'; require_once APP_BASE . '/models/forms/ProfileForm.php'; class UserController extends My_Controller_MultiPage { protected $_session_name = "User"; protected $_table_form_classes = array( 'Users' => 'UserForm', 'Profiles' => 'ProfileForm' ); public function preDispatch() { $this->view->name = "user"; } public function indexAction() { $this->_forward('list'); } } |
4.ビューの作成
以下のように
以下のように
以下のように
以下のように
application/views/scripts/computer/list.phtml
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<h1><?= $this->translate($this->name . '.title.list') ?></h1> <?php if (!$this->list) : ?> <span><?= $this->translate($this->name . 'label.no_data') ?></span> <?php else : ?> <a href="<?=$this->url(array('action'=>'add'))?>"> <?= $this->translate($this->name . '.label.new') ?></a> <table> <?php $this->header = true; ?> <?php foreach($this->list as $row) : ?> <?php $values = $row->toArray(); ?> <?php if ($this->header) : ?> <tr> <?php foreach($values as $key => $value) : ?> <th><?= $this->translate($key) ?></th> <?php endforeach; ?> <th><?= $this->translate('detail') ?></th> <th><?= $this->translate('edit') ?></th> <th><?= $this->translate('delete') ?></th> <?php $this->header = false; ?> </tr> <?php endif; ?> <tr> <?php foreach($values as $key => $value) : ?> <td><?= $this->escape($value) ?></td> <?php endforeach; ?> <td><a href="<?=$this->url(array('action'=>'detail'))?>?id=<?=$row->id?>"> <?= $this->translate('detail') ?></a></td> <td><a href="<?=$this->url(array('action'=>'update'))?>?id=<?=$row->id?>"> <?= $this->translate('edit') ?></a></td> <td><a href="<?=$this->url(array('action'=>'delete'))?>?id=<?=$row->id?>"> <?= $this->translate('delete') ?></a></td> </tr> <?php endforeach; ?> </table> <span><?= $this->translate($this->name . '.label.max') ?></span> <?php endif; ?> |
以下のように
application/views/scripts/computer/detail.phtml
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<h1><?= $this->translate($this->name . '.title.detail') ?></h1> <?php if ($this->values) : ?> <?php foreach($this->values as $table_name => $table_values) : ?> <h2><?=$this->translate($table_name . '.title.detail')?></h2> <dl> <?php foreach($table_values as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endforeach; ?> <?php endif; ?> <?php if ($this->form) : ?> <?= $this->form ?> <?php endif; ?> |
以下のように
application/views/scripts/computer/form.phtml
ファイルを作成します。
1 2 |
<h1><?= $this->translate($this->name . '.title.' . $this->action) ?></h1> <?= $this->form ?> |
以下のように
application/views/scripts/computer/finish.phtml
ファイルを作成します。
1 2 3 |
<h1><?= $this->translate($this->name . '.title.finish.' . $this->action) ?></h1> <a href="<?=$this->url(array('action'=>'list'))?>"> <?= $this->translate('back') ?></a> |
5.確認
Webサーバにアクセスして複数ページフォーム対応の追加/更新/削除/閲覧機能が実装されていることを確認してみてください。
履歴
日付 | 内容 |
---|---|
2008/4/21 | 公開 |
2008/4/26 | ビューのURLの記述方法を変更しました。 |