Common problems and solutions in the use of PHP open-source development framework ZendFramework. MVC code writing: Controller code writing: Copy the code as follows :? PhpclassIndexControllerextendsZend_Controller_Action {functioninit () {$ this-registryZend_Registry MVC code writing:
Controller code writing:
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 the value is POST, add it. Otherwise, the page is displayed.
}
}
?>
Write content in control:
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 to the framework.
The code is as follows:
// 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.
The code is as follows:
Class Message extends Zend_Db_Table
{
Protected $ _ name = "message ";
Protected $ _ primary = 'id ';
}
?>
Module instantiation:
The code is as follows:
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
}
Messages as $ message):?>
*************
Modify and delete data
The code is as follows:
Kk
Ll
Add index. phtml
The code is as follows:
BaseUrl?> /Index/exit "> Edit
BaseUrl?> /Index/delete "> delete
Add a new method: edit. phtml
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 );
// Update data
$ Message-> update ($ set, $ where );
Unset ($ set );
Echo 'data modified successfully! View-> baseUrl. '/index/"> Return ';
} 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! View-> baseUrl. '/index/"> Return ';
}
Exception:
The code is as follows:
Fatal error: Uncaught exception 'zend _ Controller_Dispatcher_Exception 'with message 'invalid controller specified (index. php)' in
Solution: in index. php
The code is as follows:
$ FrontController = Zend_Controller_Front: getInstance ();
$ FrontController-> setParam ('usedefacontrocontrolleralway', true );
*******
Id/3 is equal to the previous? Id = 3
Http://www.bkjia.com/PHPjc/788645.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/788645.htmlTechArticleMVC code writing: Controller code writing: the code is as follows :? Php class IndexController extends Zend_Controller_Action {function init () {$ this-registry = Zend_Registry...