Description of common problems and solutions in the use of PHP open source development framework ZendFramework_PHP tutorial

WBOY
Release: 2016-07-13 10:28:21
Original
828 people have browsed it

MVC code writing:
Controller code writing:

Copy code The code is as follows:

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(){
//If it is the value from POST, increase it. Otherwise, display the increase page


}
}
?>

Writing content in control:

Copy code The code is as follows:

$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

Introduce the configuration file into the framework

Copy code The code is as follows:

//Configure database parameters and connect to the database
$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);

Single entry mode: localhost/index/add/ access the add method under the index module
function addAction(){} (in IndexController.php)
The default access is the index method under the index module

Create another message.php in the module model

Copy code The code is as follows:

class Message extends Zend_Db_Table
{
protected $_name ="message";
protected $_primary = 'id';
}
?>

Module instantiation:

Copy code The code is as follows:

function indexAction()
{
$message=new message() ;//Instantiate database class

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

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

messages as $message): ?>





****************
Modify and delete data

Copy code The code is as follows:


kk

ll

Add

in index.phtml

Copy code The code is as follows:

Add a new method: edit.phtml

Copy code The code is as follows:

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 '修改数据成功!
返回';
}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 '删除数据成功!返回';
}

异常出现:

复制代码 代码如下:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/788645.htmlTechArticleMVC 代码书写: 控制器代码书写: 复制代码 代码如下: ?php class IndexController extends Zend_Controller_Action { function init() { $this-registry = Zend_Registry...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!