北大青鸟教育 主页 > 北大青鸟热门技术 > python技术 > 正文

zendframework之环境搭建出

2012-07-20 14:05 点击咨询

 

今天我们来讲解一下zendframework的环境搭建
index.php
  1. <?php

  2.  

  3. // Define path to application directory

  4. defined('APPLICATION_PATH')

  5.     || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

  6.  

  7. // Define application environment

  8. defined('APPLICATION_ENV')

  9.     || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

  10.  

  11. // Ensure library/ is on include_path

  12. set_include_path(implode(PATH_SEPARATOR, array(

  13.     realpath(APPLICATION_PATH . '/../library'),

  14.     get_include_path(),

  15. )));

  16.  

  17. /** Zend_Application */

  18. require_once 'Zend/Application.php';

  19.  

  20.  

  21.  

  22. // Create application, bootstrap, and run

  23. $application = new Zend_Application(

  24.     APPLICATION_ENV,

  25.     APPLICATION_PATH . '/configs/application.ini'

  26. );

  27.  

  28. $application->bootstrap()

  29.             ->run();
复制代码



Bootstrap.php
  1. <?php

  2. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

  3. {

  4.                 protected function _initAutoload()

  5.         {

  6.                 $autoLoader = Zend_Loader_Autoloader::getInstance();

  7.                 //$autoLoader->registerNamespace('Controller_');

  8.                 $autoLoader->registerNamespace('Sun_');

  9.                 $resourceLoader = new Zend_Loader_Autoloader_Resource(

  10.                         array('basePath' => APPLICATION_PATH ,

  11.                             'namespace' => '' ,

  12.                             'resourceTypes' => array('form' => array('path' => 'forms/' , 'namespace' => 'Form_') ,

  13.                                 'model' => array('path' => 'models/' , 'namespace' => 'Model_'))));

  14.                 // Return it so that it can be stored by the bootstrap

  15.                 return $autoLoader;

  16.  

  17.         }

  18.  

  19. }

  20.  

  21.  
复制代码



application.ini
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"


resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.params.displayExceptions = 1
resources.frontController.params.prefixDefaultModule = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
;resources.frontController.plugins.acl = "Sun_Controller_Plugin_Acl"
;resources.modules[] = ""

resources.db.adapter = "pdo_mysql"
resources.db.params.host = "127.0.0.1"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.dbname = "zf"
resources.db.isDefaultTableAdapter = true
resources.db.params.driver_options.1002 = "SET NAMES utf8"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1



IndexController.php
  1. <?php

  2.  

  3. class IndexController extends Zend_Controller_Action

  4. {

  5.  

  6.         /**

  7. * 控制器初始化

  8. *

  9. */

  10.         public function init()

  11.         {

  12.                 //$this->_helper->layout->setLayout('admin');

  13.                 $this->_helper->layout->disableLayout();

  14.                 //$this->_helper->viewRenderer->setNoRender();

  15.         }

  16.  

  17.  

  18.         public function indexAction()

  19.         {

  20.  

  21.                 $modelUser = new Model_User();

  22.                 $result = $modelUser->getUserById(1);

  23.                 print_r($result);

  24. exit;

  25. }

  26.  

  27.  

  28. }

  29.  

  30.  
复制代码



Model的User.php
  1. <?php

  2.  

  3. class Model_User extends Zend_Db_Table_Abstract

  4. {

  5.         protected $_name = 'user';

  6.         protected $_primary = 'uid';

  7.  

  8.  

  9.         /**

  10.          * 获取用户信息

  11.          *

  12.          */

  13.         public function getUserById($id)

  14.         {

  15.                 $select = $this->select();

  16.  

  17.                 $select->from('user');

  18.                 $select->where('uid=?', $id);

  19.  

  20.                 $sql = $select->__toString();

  21.                 return $this->_db->fetchAll( $sql );

  22.         }

  23. }
复制代码



地址栏:
http://localhost/zf/public/

Array(    [0] => Array        (            [uid] => 1            [username] => admin            [password] => 111111            [role_id] => 3        ))

 

 

 
试听课
徐州市中博教育培训中心    版权所有    苏ICP备13053530号
地址:徐州市解放南路181号北大青鸟徐州中博(六中对面)        矿大校区地址:徐州市解放南路中国矿业大学(文昌校区西校区)
电话:0516-85628888        邮编:221000

苏公网安备 32030302000208号