Home >Backend Development >PHP Tutorial >Common problems related to the use of ZendFramework_PHP tutorial
MVC code writing:
Controller code writing:
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 add page
}
}
? >
Write content in the control: $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
The configuration file is introduced into the framework
//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
class Message extends Zend_Db_Table
{
protected $_name ="message";
protected $_primary = ' id';
}
?>
Module instantiation:
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
kk
ll
Add a new method: 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 );
//Update data
$message->update($set,$where);
unset($set);
echo 'Modify data successfully! return';
}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 'Delete Data success! Return';
}
Exception occurred:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index.php)' in
Solution: Add
$frontController->setParam('useDefaultControllerAlways', true);
after
*******
id/3 is equal to the previous ?id=3