zend framework 实例 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
index.php 页面
- <?php
- error_reporting(E_ALL|E_STRICT);
- date_default_timezone_set('Asia/Shanghai');
- set_include_path('.' .PATH_SEPARATOR.'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR. get_include_path());
- require_once"Zend/Loader/Autoloader.php";
- Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
- $registry = Zend_Registry::getInstance();
- $view = new Zend_View();
- $view->setScriptPath('./application/views/scripts/');
- $registry['view'] = $view;
-
- $config=newZend_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 NAMESutf8');
- Zend_Db_Table::setDefaultAdapter($dbAdapter);
- Zend_Registry::set('dbAdapter',$dbAdapter);
-
-
- $frontController=Zend_Controller_Front::getInstance();
- $frontController->setBaseUrl('/zendframework')
- ->setParam('noViewRenderer',true)
- ->setControllerDirectory('./application/controllers')
- ->throwExceptions(true)
- ->dispatch();
- ?>
IndexController.php页面
- <?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()
- {
-
- $content = new Content();
- $db = $content->getAdapter();
-
- $where = $db->quoteInto('1 = ?', '1');
-
- $order = 'id desc';
-
- $info =$content->fetchAll($where,$order)->toArray();
- $this->view->news = $info;
- echo$this->view->render('index.phtml');
- }
-
- functionaddAction()
- {
-
- if(strtolower($_SERVER['REQUEST_METHOD']) =='post'){
-
- $title =$this->_request->getPost('title');
- $content =$this->_request->getPost('content');
- $addtime = time();
- $news = new Content();
-
- $data = array(
- 'title'=>$title,
- 'content' =>$content,
- 'addtime'=>$addtime
- );
-
- if($news->insert($data)){
- echo'添加数据成功!';
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- unset($data);
- }else{
- echo'添加数据失败!';
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- }
- }
- }
-
- functioneditAction()
- {
-
- $content = new Content();
- $db =$content->getAdapter();
-
- if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
-
- $id =$this->_request->getPost('id');
- $title =$this->_request->getPost('title');
- $content =$this->_request->getPost('content');
-
- $data = array(
- 'title'=>$title,
- 'content'=>$content,
- );
-
- $where = $db->quoteInto('id =?',$id);
-
- $content->update($data,$where);
-
- echo "更新数据成功!";
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- unset($data);
- }else{
- $id =$this->_request->getParam('id');
- $this->view->content=$content->fetchAll('id='.$id)->toArray();
- echo$this->view->render('edit.phtml');
- }
- }
-
- functiondeleteAction()
- {
-
- $content = new Content();
-
-
- $db =$content->getAdapter();
-
- $id =$this->_request->getParam('id');
-
- $where = $db->quoteInto('id =?',$id);
-
- $content->delete($where);
- echo "删除成功!";
- echo'<ahref="'.$this->view->baseUrl.'/index/index/">返回首页</a>';
- }
-
- functionarticleAction()
- {
-
- $content = new Content();
-
- $id =$this->_request->getParam('id');
-
- $info =$content->find($id)->toArray();
-
- $this->view->info =$info;
- echo$this->view->render('article.phtml');
- }
- }
model content.php
- <?php
- class Content extends Zend_Db_Table
- {
-
- protected $_name = "content";
- protected $_primary = 'id';
- }
- ?>
|
|
|
|