PHPオープンソース開発フレームZendFraamewark使用中によくある問題説明と解決案

4592 ワード

MVCコード記入:コントローラコード記入:

<?php
class IndexController extends Zend_Controller_Action
{
function init()
{
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl();

}
function indexAction()
{
$this->view->word=" I love spurs";

echo $this->view->render("index.html");

}
function addAction(){
// POST . .


}
}
?>

コントロールに内容を書きます。

$this->view->word="ggg";
$this->view->render("index.html");
---->index.html echo $this->word;

application->config.ini
[general]
db.adapter=PDO_MYSQL
db.config.host=localhost
db.config.username=root
db.config.password=
db.config.dbname=think_zw

設定ファイルはフレームワークに導入されます。

// ,
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
単一エントリモード:localhost/index/add/アクセスindexモジュール下のaddメソッドfunction addAction(){}(IndexController.php)デフォルトでindexモジュールにアクセスするindex方法
モジュールmodelの中のmessage.phpをもう一つ作ります。

<?php
class Message extends Zend_Db_Table
{
protected $_name ="message";
protected $_primary = 'id';
}
?>
モジュールの実装:

function indexAction()
{
$message=new message();//

//
$this->view->messages=$message->fetchAll()->toArray();

echo $this->view->render('index.phtml');//
}

<?foreach($this->messages as $message): ?>
<tr>
<th><?php echo $message['title']; ?></th>
<td><?php echo $message['content']; ?></td>
</tr>
<?endforeach; ?>

******を変更し、データを削除します。

<?php if(2==2):?>
kk
<?php else:?>
ll
<?php endif;?>
index.phtmlに加えて

<a href="<?php echo $this->baseUrl?>/index/exit"> </a>
<a href="<?php echo $this->baseUrl?>/index/delete"> </a>
新しい方法を追加します。edit.phtml

function editAction(){

$message = new Message();
$db = $message->getAdapter();

if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
$id = $this->_request->getPost('id');
$cid = $this->_request->getPost('cid');
$title = $this->_request->getPost('title');

$set = array(
'cid'=>$cid,
'title'=>$title
);
$where = $db->quoteInto('id = ?',$id);
//
$message->update($set,$where);
unset($set);
echo ' !<a href="'.$this->view->baseUrl.'/index/index/"> </a>';
}else{
$id = $this->_request->getParam('id');
$this->view->messages = $message->fetchAll('id='.$id)->toArray();
echo $this->view->render('edit.phtml');
}
}


function delAction(){
$message = new Message();
$id = (int)$this->_request->getParam('id');

if($id > 0){
$where = 'id = ' . $id;
$message->delete($where);
}
echo ' !<a href="'.$this->view->baseUrl.'/index/index/"> </a>';
}

異常発生:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index.php)' in
解決方法:index.phpにおける

$frontController =Zend_Controller_Front::getInstance();
$frontController->setParam('useDefaultControllerAlways', true);
*********id/3は以前のものに等しいですか?id=3