今天我们来讲解一下zendframework的环境搭建
index.php
-
<?php
-
-
// Define path to application directory
-
defined('APPLICATION_PATH')
-
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
-
-
// Define application environment
-
defined('APPLICATION_ENV')
-
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
-
-
// Ensure library/ is on include_path
-
set_include_path(implode(PATH_SEPARATOR, array(
-
realpath(APPLICATION_PATH . '/../library'),
-
get_include_path(),
-
)));
-
-
/** Zend_Application */
-
require_once 'Zend/Application.php';
-
-
-
-
// Create application, bootstrap, and run
-
$application = new Zend_Application(
-
APPLICATION_ENV,
-
APPLICATION_PATH . '/configs/application.ini'
-
);
-
-
$application->bootstrap()
-
->run();
复制代码
Bootstrap.php
-
<?php
-
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
-
{
-
protected function _initAutoload()
-
{
-
$autoLoader = Zend_Loader_Autoloader::getInstance();
-
//$autoLoader->registerNamespace('Controller_');
-
$autoLoader->registerNamespace('Sun_');
-
$resourceLoader = new Zend_Loader_Autoloader_Resource(
-
array('basePath' => APPLICATION_PATH ,
-
'namespace' => '' ,
-
'resourceTypes' => array('form' => array('path' => 'forms/' , 'namespace' => 'Form_') ,
-
'model' => array('path' => 'models/' , 'namespace' => 'Model_'))));
-
// Return it so that it can be stored by the bootstrap
-
return $autoLoader;
-
-
}
-
-
}
-
-
复制代码
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
-
<?php
-
-
class IndexController extends Zend_Controller_Action
-
{
-
-
/**
-
* 控制器初始化
-
*
-
*/
-
public function init()
-
{
-
//$this->_helper->layout->setLayout('admin');
-
$this->_helper->layout->disableLayout();
-
//$this->_helper->viewRenderer->setNoRender();
-
}
-
-
-
public function indexAction()
-
{
-
-
$modelUser = new Model_User();
-
$result = $modelUser->getUserById(1);
-
print_r($result);
-
exit;
-
}
-
-
-
}
-
-
复制代码
Model的User.php
-
<?php
-
-
class Model_User extends Zend_Db_Table_Abstract
-
{
-
protected $_name = 'user';
-
protected $_primary = 'uid';
-
-
-
/**
-
* 获取用户信息
-
*
-
*/
-
public function getUserById($id)
-
{
-
$select = $this->select();
-
-
$select->from('user');
-
$select->where('uid=?', $id);
-
-
$sql = $select->__toString();
-
return $this->_db->fetchAll( $sql );
-
}
-
}
复制代码
地址栏:
http://localhost/zf/public/
Array( [0] => Array ( [uid] => 1 [username] => admin [password] => 111111 [role_id] => 3 ))
|
|