MVC code writing:
Controller code writing:
<? 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 (){
// If the value is POST, add it. Otherwise, the page is displayed.
}
}
?>
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
Introduce the configuration file to the framework.
// Configure database parameters and connect to the database
$ Config = new Zend_Config_Ini ('./application/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: setdefaadapter adapter ($ dbAdapter );
Zend_Registry: set ('dbadapter ', $ dbAdapter );
Single Entry Mode: localhost/index/add/access the add method in the index module
Function addAction () {} (in IndexController. php)
The default access method is the index method under the index module.
Create the message. php In the module model.
<? Php
Class Message extends Zend_Db_Table
{
Protected $ _ name = "message ";
Protected $ _ primary = 'id ';
}
?>
Module instantiation:
Function indexAction ()
{
$ Message = new message (); // instantiate the Database Class
// Obtain the database content
$ This-> view-> messages = $ message-> fetchAll ()-> toArray ();
Echo $ this-> view-> render ('index. phpml'); // display the Template
}
<? Foreach ($ this-> messages as $ message):?>
<Tr>
<Th> <? Php echo $ message ['title'];?> </Th>
<Td> <? Php echo $ message ['content'];?> </Td>
</Tr>
<? Endforeach;?>
*************
Modify and delete data
<? Php if (2 = 2):?>
Kk
<? Php else:?>
Ll
<? Php endif;?>
<A href = "<? Php echo $ this-> baseUrl?> /Index/exit "> edit </a>
<A href = "<? Php echo $ this-> baseUrl?> /Index/delete "> delete </a>
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 'data modified successfully! <A href = "'. $ this-> view-> baseUrl.'/index/"> return </a> ';
} Else {
$ Id = $ this-> _ request-> getParam ('id ');
$ This-> view-> messages = $ message-> fetchAll ('Id = '. $ id)-> toArray ();
Echo $ this-> view-> render ('edit. phpml ');
}
}
Function delAction (){
$ Message = new Message ();
$ Id = (int) $ this-> _ request-> getParam ('id ');
If ($ id> 0 ){
$ Where = 'id = '. $ id;
$ Message-> delete ($ where );
}
Echo 'data deleted successfully! <A href = "'. $ this-> view-> baseUrl.'/index/"> return </a> ';
}
Exception:
Fatal error: Uncaught exception 'zend _ Controller_Dispatcher_Exception 'with message 'invalid controller specified (index. php)' in
Solution: In index. php
$ FrontController = Zend_Controller_Front: getInstance ();
$ FrontController-> setParam ('usedefacontrocontrolleralway', true );
*******
Id/3 is equal to the previous? Id = 3