PHP開源開發架構ZendFramework使用中常見問題說明及解決方案_PHP教程

來源:互聯網
上載者:User
MVC 代碼書寫:
控制器代碼書寫:

複製代碼 代碼如下:
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

設定檔引入到framework裡面去

複製代碼 代碼如下:
//設定資料庫參數,並串連資料庫
$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

複製代碼 代碼如下:
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');//顯示模版
}

messages as $message): ?>





*************
修改和刪除資料

複製代碼 代碼如下:

kk

ll

index.phtml裡面加上

複製代碼 代碼如下:
baseUrl?>/index/exit">編輯
baseUrl?>/index/delete">刪除

添加一個新的方法: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 '修改資料成功!view->baseUrl.'/index/index/">返回';
}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 '刪除資料成功!view->baseUrl.'/index/index/">返回';
}

異常出現:

複製代碼 代碼如下:
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

http://www.bkjia.com/PHPjc/788645.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/788645.htmlTechArticleMVC 代碼書寫: 控制器代碼書寫: 複製代碼 代碼如下: ?php class IndexController extends Zend_Controller_Action { function init() { $this-registry = Zend_Registry...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.