在这之前,先进行各个软件的安装. 这就不说了,主要还是httd.conf 挂载php模块 [php根目录下install.txt中]
# For PHP 4 do something like this:
LoadModule php4_module "c:/php/php4apache2.dll"
# Don't forget to copy the php4apache2.dll file from the sapi directory!
AddType application/x-httpd-php .php
# For PHP 5 do something like this:
LoadModule php5_module "c:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"
一、设置和常见问题
1、Apache2.2的设置:
1)在Apache2.2的安装目录下找到XXX/Apache2.2/conf/httpd.conf文件,对其进行修改编辑:
找到
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
将AllowOverride None改为AllowOverride All
如果不进行该项设置的话,点击public的时候,不是打开网站,而是进入了public目录。
2)找到#LoadModule rewrite_module modules/mod_rewrite.so
将其改为LoadModule rewrite_module modules/mod_rewrite.so,即去掉注释符号#
如果此处没改的话,到时候会出现如下错误提示:
Internal Server Error
修改之后,重启Apache,才能生效。
2、PHP对MySQL数据库的PDO扩展设置
(转)zend framework里面连接数据库是通过PDO(问:何谓PDO?答:PHP Data Object。该扩展提供PHP内置类 PDO来对数据库进行访问,不同数据库使用相同的方法名,解决数据库连接不统一的问题)来做的。如果你的PHP版本没有PDO或者没有打开,需要做相应的更新了修改。
1)在php安装目录里,找到libmysql.dll,将其复制到C:\Windows\System32下。否则,无论接下来如何修改,都会出现错误:
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'The mysql driver is not currently installed'
(参考自一个国外论坛,现在国内的论坛都是到处抄袭,而且解决问题很片面)
2)然后在php.ini里找到
;extension=php_pdo.dll
;extension=php_pdo_mysql.dll
;extension=php_mysql.dll
将其前面的“;”去掉(根据需要,可以对相应的extension进行展开),如下:
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_mysql.dll
否则会分别出现如下错误:
(php_pdo_mysql.dll)
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'The mysql driver is not currently installed'
(php_pdo.dll、php_mysql.dll)
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'The PDO extension is required for this adapter but the extension is not loaded'
3)找到
extension_dir =
改为
extension_dir = "C:\php5\ext"
否则会出现如下错误:
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'The PDO extension is required for this adapter but the extension is not loaded'
4)其他问题
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)'
解决办法:$frontController->setParam('useDefaultControllerAlways', true);
Notice: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in
解决办法:
将://require_once ‘Zend/Loader.php’;
//Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件
换成:
require_once ‘Zend/Loader/Autoloader.php’;
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
二、具体配置过程
网上有很多教程,这里我说一下我自己的一个配置过程,仅供参考。
具体可以参考(Zend+Framework+入门指南)
由于Apache安装后,其默认的web根目录为XXX/Apache2.2/htdocs,如果觉得不方便可以在httpd.conf中进行修改:
找到
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "D:/Program Files/Windows7/Apache2.2/htdocs">
将Directory的路径设置为你想要的web的根目录路径,重启Apache就行了。
这里我就不进行修改了。在htdocs中新建一个文件夹home
在home下新建 application、library、public、configs四个文件夹。
然后在application文件夹中新建controllers、models、views三个文件夹,这就是MVC模式的对应的三个文件夹了。
由于使用Smarty来代替ZendFrameWork本身的views,因此,根据需要,在views文件夹下建立configs、templates、templates_c三个文件夹,在templates_c下建立cache文件夹。
然后将ZendFrameWork和Smarty的库文件都放在library文件夹中。
先简单预览下目录结构:
|--home
|--application
|--controllers
|--IndexController.php
|--models
|--views
|--configs
|--templates
|--index.tpl(Smarty模板文件)
|--templates_c
|--cache
|--public
|--index.php(站点入口)
|--.htaccess(配置文件)
|--library
|--Zend
|--Smarty
|--SmartyInterface.php(为使用Smarty定义的接口)
|--configs
|--conf.ini(数据库配置文件)
首先是,配置文件编辑.htaccess,内容为:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
php_flag magic_quotes_gpc off
php_flag register_globals off
关于windows下创建空文件名的方法:
在“开始菜单——附件——记事本”,打开记事本后,“文件——保存”,文件类型选择所有文件,文件名输入.htaccess,这样就行了。
入口文件index.php编辑,内容如下:
- <?php
- //define语句放在其他语句之前,否则会出错
- //PATH_SEPARATOR:路径分隔符,include多个路径使用,
- //在win下,当你要include多个路径的话,你要用”;”隔开,
- //但在linux下就使用”:”隔开的
- define('P_S',PATH_SEPARATOR);
- //设置smarty模板路径时使用
- define('APPLICATION_PATH', '../application');
- //设置错误提示、默认时区
- error_reporting(E_ALL & (~E_NOTICE) | E_STRICT );
- ini_set('display_errors',1);
- date_default_timezone_set('Asia/Shanghai');
- //设置include路径
- set_include_path('.'.P_S.'../library'.P_S.'../application/models/'.P_S.get_include_path());
- //设置自动载入类文件
- /*require_once 'Zend/Loader.php';
- Zend_Loader::registerAutoload();*/
- require_once 'Zend/Loader/Autoloader.php';
- Zend_Loader_Autoloader::getInstance();
- //数据库配置
- $config = new Zend_Config_Ini('../configs/conf.ini', 'db');
- $dbAdapter = Zend_Db::factory($config->db->adapter,$config->db->params->toArray());//创建数据库适配器
- Zend_Db_Table::setDefaultAdapter($dbAdapter);//设置表的默认适配器,除非你特别指定,否则所有的zend_db_table类实例都会使用 默认adapter
- Zend_Registry::set('dbAdapter', $dbAdapter);
- //设置Controller,前端控制器
- $frontController = Zend_Controller_Front::getInstance();
- $frontController->setControllerDirectory('../application/controllers');
- $frontController->throwExceptions(true);
- //设置Smarty模板
- //方法一,用来测试Smarty是否可用
- /*require_once('Smarty/Smarty.class.php');
- $frontController->setParam('useDefaultControllerAlways', true);
- //关闭ZendFrameWork本身的视图view
- $frontController->setParam('noViewRenderer', true);
- $smarty = new Smarty;
- $smarty->template_dir = '../application/views/templates/';
- $smarty->compile_dir = '../application/views/templates_c/';
- $smarty->config_dir = '../application/views/configs/';
- $smarty->cache_dir = '../application/views/templates_c/cache/';
- $smarty->assign('name','Ned');
- $smarty->display('index.tpl');*/
- //方法二:
- require_once 'SmartyInterface.php';
- //关闭ZendFrameWork本身的视图view
- $frontController->setParam('noViewRenderer', true);
- $frontController->setParam('useDefaultControllerAlways', true);
- $views = new Smarty_Zend_View(APPLICATION_PATH.'/views/templates',
- array('template_dir' => APPLICATION_PATH.'/views/templates',
- 'compile_dir' => APPLICATION_PATH. '/views/templates_c',
- 'cache_dir' => APPLICATION_PATH. '/views/templates_c/cache',
- 'debugging' => false,
- 'caching' => false,
- 'cache_lifetime' => 0,
- 'left_delimiter' => '<#',
- 'right_delimiter' => '#>',));
- Zend_Registry::set('views', $views);
- $frontController->dispatch();//运行
- ?>
Smarty接口定义:(在ZendFrameWork的库文件Zend/View/Interface.php中给了接口定义的模型,可以在此之上进行修改)
- <?php
- require_once('Smarty/Smarty.class.php');
- require_once('Zend/View/Interface.php');
- class Smarty_Zend_View implements Zend_View_Interface
- {
- protected $_smarty;
- /**
- *构造函数
- */
- public function __construct($tmplPath = null, $extraParams = array())
- {
- $this->_smarty = new Smarty;
- if (null !== $tmplPath)
- {
- $this->setScriptPath($tmplPath);
- }
- foreach ($extraParams as $key => $value)
- {
- $this->_smarty->$key = $value;
- }
- }
- /**
- * Return the template engine object, if any
- *
- * If using a third-party template engine, such as Smarty, patTemplate,
- * phplib, etc, return the template engine object. Useful for calling
- * methods on these objects, such as for setting filters, modifiers, etc.
- *
- * @return mixed
- */
- public function getEngine()
- {
- return $this->_smarty;
- }
- /**
- * Set the path to find the view script used by render()
- *
- * @param string|array The directory (-ies) to set as the path. Note that
- * the concrete view implentation may not necessarily support multiple
- * directories.
- * @return void
- */
- public function setScriptPath($path)
- {
- //if (is_readable($path))
- {
- $this->_smarty->template_dir = $path;
- return;
- }
- throw new Exception('Invalid path provided');
- }
- /**
- * Retrieve all view script paths
- *
- * @return array
- */
- public function getScriptPaths()
- {
- return array($this->_smarty->template_dir);
- }
- /**
- * Set a base path to all view resources
- *
- * @param string $path
- * @param string $classPrefix
- * @return void
- */
- public function setBasePath($path, $prefix = 'Zend_View')
- {
- return $this->setScriptPath($path);
- }
- /**
- * Add an additional path to view resources
- *
- * @param string $path
- * @param string $classPrefix
- * @return void
- */
- public function addBasePath($path, $prefix = 'Zend_View')
- {
- return $this->setScriptPath($path);
- }
- /**
- * Assign a variable to the view
- *
- * @param string $key The variable name.
- * @param mixed $val The variable value.
- * @return void
- */
- public function __set($key, $val)
- {
- $this->_smarty->assign($key, $val);
- }
- /**
- * Allows testing with empty() and isset() to work
- *
- * @param string $key
- * @return boolean
- */
- public function __isset($key)
- {
- return (null !== $this->_smarty->get_template_vars($key));
- }
- /**
- * Allows unset() on object properties to work
- *
- * @param string $key
- * @return void
- */
- public function __unset($key)
- {
- $this->_smarty->clear_assign($key);
- }
- /**
- * Assign variables to the view script via differing strategies.
- *
- * Suggested implementation is to allow setting a specific key to the
- * specified value, OR passing an array of key => value pairs to set en
- * masse.
- *
- * @see __set()
- * @param string|array $spec The assignment strategy to use (key or array of key
- * => value pairs)
- * @param mixed $value (Optional) If assigning a named variable, use this
- * as the value.
- * @return void
- */
- public function assign($spec, $value = null)
- {
- if (is_array($spec))
- {
- $this->_smarty->assign($spec);
- return;
- }
- $this->_smarty->assign($spec, $value);
- }
- /**
- * Clear all assigned variables
- *
- * Clears all variables assigned to Zend_View either via {@link assign()} or
- * property overloading ({@link __get()}/{@link __set()}).
- *
- * @return void
- */
- public function clearVars()
- {
- $this->_smarty->clear_all_assign();
- }
- /**
- * Processes a view script and returns the output.
- *
- * @param string $name The script name to process.
- * @return string The script output.
- */
- public function render($name)
- {
- return $this->_smarty->fetch($name);
- }
- /**
- * Retrieve an assigned variable
- *
- * @param string $key The variable name.
- * @return mixed The variable value.
- */
- public function __get($key)
- {
- return $this->_smarty->get_template_vars($key);
- }
- /**
- * 用于兼容 smarty 的 display 方法
- */
- public function display($resource_name, $cache_id = null, $compile_id = null)
- {
- return $this->_smarty->display($resource_name, $cache_id, $compile_id);
- }
- /**
- * 用于兼容 smarty 的 fetch 方法
- */
- public function fetch($resource_name, $cache_id = null, $compile_id = null)
- {
- return $this->_smarty->fetch($resource_name, $cache_id, $compile_id);
- }
- }
数据库配置文件conf.ini
- [db]
- db.adapter = "Pdo_Mysql"
- db.params.charset = "utf8"
- db.params.host = "localhost"
- db.params.username = "root"
- db.params.password = "chen" ;密码
- db.params.dbname = "chenpeijie" ;数据库名
在此之前,应该先创建好相应的数据库。此处数据库库名为chenpeijie,用户名是root,密码是你安装MySQL时设置的密码。
关于数据库的操作,可参考
models/Albums.php
- <?php
- class Albums extends Zend_Db_Table
- {
- protected $_name = 'albums';
- }
- ?>
controllers/IndexController.php
- <?php
- require_once 'Albums.php';
- class IndexController extends Zend_Controller_Action
- {
- var $views; /*模板对象*/
- function init()
- {
- $this->views = Zend_Registry::get('views');
- }
- public function indexAction ()
- {
- //定义模版显示的变量
- $this->views->assign('page_title','My Albums');
- $albums = new Albums();
- $s = $albums->fetchAll();
- $this->views->assign('s',$s);
- $this->views->display('index.tpl');
- }
- }
- ?>
templates/views/index.tpl
- <style>
- body,html {
- margin: 0 5px;
- font-family: Verdana,sans-serif;
- }
- h1 {
- font-size:1.4em;
- color: #008000;
- }
- a {
- color: #008000;
- }
- /* Table */
- th {
- text-align: left;
- }
- td, th {
- padding-right: 5px;
- }
- /* style form */
- form dt {
- 100px;
- display: block;
- }
- </style>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <h1>
- <#$page_title#>
- </h1>
- <p><a href="http://localhost/home/public/add">Add new album</a></p>
- <table>
- <tr>
- <th>Title</th>
- <th>Artist</th>
- <th> </th>
- </tr>
- {foreach $s as $value}
- <tr>
- <td><#$value.title#></td>
- <td><#$value.artist#></td>
- <td>
- <a href="http://localhost/home/public/index/edit">Edit</a>
- <a href="http://localhost/home/public/index/delete">Delete</a>
- </td>
- </tr>
- {/foreach}
- </table>
- </body>
- </html>