zoukankan      html  css  js  c++  java
  • windows 下 ZendFrameWork 平台的搭建

    在这之前,先进行各个软件的安装. 这就不说了,主要还是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编辑,内容如下:

    1. <?php  
    2. //define语句放在其他语句之前,否则会出错  
    3. //PATH_SEPARATOR:路径分隔符,include多个路径使用,  
    4. //在win下,当你要include多个路径的话,你要用”;”隔开,  
    5. //但在linux下就使用”:”隔开的  
    6.     define('P_S',PATH_SEPARATOR);  
    7.     //设置smarty模板路径时使用  
    8.     define('APPLICATION_PATH''../application');  
    9.       
    10. //设置错误提示、默认时区  
    11.     error_reporting(E_ALL & (~E_NOTICE) | E_STRICT );  
    12.     ini_set('display_errors',1);  
    13.     date_default_timezone_set('Asia/Shanghai');  
    14.       
    15.       
    16. //设置include路径  
    17.     set_include_path('.'.P_S.'../library'.P_S.'../application/models/'.P_S.get_include_path());  
    18. //设置自动载入类文件  
    19.     /*require_once 'Zend/Loader.php'; 
    20.     Zend_Loader::registerAutoload();*/  
    21.     require_once 'Zend/Loader/Autoloader.php';  
    22.     Zend_Loader_Autoloader::getInstance();  
    23.   
    24. //数据库配置  
    25.     $config = new Zend_Config_Ini('../configs/conf.ini''db');  
    26.     $dbAdapter = Zend_Db::factory($config->db->adapter,$config->db->params->toArray());//创建数据库适配器  
    27.     Zend_Db_Table::setDefaultAdapter($dbAdapter);//设置表的默认适配器,除非你特别指定,否则所有的zend_db_table类实例都会使用 默认adapter  
    28.     Zend_Registry::set('dbAdapter'$dbAdapter);  
    29.   
    30.   
    31.   
    32. //设置Controller,前端控制器  
    33.     $frontController = Zend_Controller_Front::getInstance();  
    34.     $frontController->setControllerDirectory('../application/controllers');  
    35.     $frontController->throwExceptions(true);  
    36.   
    37.       
    38. //设置Smarty模板  
    39. //方法一,用来测试Smarty是否可用  
    40.     /*require_once('Smarty/Smarty.class.php'); 
    41.     $frontController->setParam('useDefaultControllerAlways', true); 
    42.     //关闭ZendFrameWork本身的视图view 
    43.     $frontController->setParam('noViewRenderer', true); 
    44.  
    45.     $smarty = new Smarty; 
    46.     $smarty->template_dir = '../application/views/templates/'; 
    47.     $smarty->compile_dir = '../application/views/templates_c/'; 
    48.     $smarty->config_dir = '../application/views/configs/'; 
    49.     $smarty->cache_dir = '../application/views/templates_c/cache/'; 
    50.     $smarty->assign('name','Ned'); 
    51.     $smarty->display('index.tpl');*/  
    52. //方法二:  
    53.     require_once 'SmartyInterface.php';  
    54.     //关闭ZendFrameWork本身的视图view  
    55.     $frontController->setParam('noViewRenderer', true);  
    56.     $frontController->setParam('useDefaultControllerAlways', true);  
    57.         $views = new Smarty_Zend_View(APPLICATION_PATH.'/views/templates',   
    58.                  array('template_dir' => APPLICATION_PATH.'/views/templates',  
    59.                        'compile_dir' => APPLICATION_PATH. '/views/templates_c',  
    60.                        'cache_dir' => APPLICATION_PATH. '/views/templates_c/cache',  
    61.                        'debugging' => false,  
    62.                        'caching' => false,  
    63.                        'cache_lifetime' => 0,  
    64.                        'left_delimiter' => '<#',                     
    65.                        'right_delimiter' => '#>',));   
    66.         Zend_Registry::set('views'$views);  
    67.         $frontController->dispatch();//运行  
    68. ?>  

     

    Smarty接口定义:(在ZendFrameWork的库文件Zend/View/Interface.php中给了接口定义的模型,可以在此之上进行修改)

    1. <?php  
    2. require_once('Smarty/Smarty.class.php');  
    3. require_once('Zend/View/Interface.php');  
    4.   
    5.   
    6. class Smarty_Zend_View implements Zend_View_Interface  
    7. {  
    8.     protected $_smarty;  
    9.       
    10.     /** 
    11.     *构造函数 
    12.     */  
    13.     public function __construct($tmplPath = null, $extraParams = array())  
    14.     {  
    15.         $this->_smarty = new Smarty;  
    16.   
    17.         if (null !== $tmplPath)   
    18.         {  
    19.             $this->setScriptPath($tmplPath);  
    20.         }  
    21.   
    22.         foreach ($extraParams as $key => $value)   
    23.         {  
    24.             $this->_smarty->$key = $value;  
    25.         }  
    26.     }  
    27.       
    28.     /** 
    29.      * Return the template engine object, if any 
    30.      * 
    31.      * If using a third-party template engine, such as Smarty, patTemplate, 
    32.      * phplib, etc, return the template engine object. Useful for calling 
    33.      * methods on these objects, such as for setting filters, modifiers, etc. 
    34.      * 
    35.      * @return mixed 
    36.      */  
    37.     public function getEngine()  
    38.     {  
    39.         return $this->_smarty;  
    40.     }  
    41.     /** 
    42.      * Set the path to find the view script used by render() 
    43.      * 
    44.      * @param string|array The directory (-ies) to set as the path. Note that 
    45.      * the concrete view implentation may not necessarily support multiple 
    46.      * directories. 
    47.      * @return void 
    48.      */  
    49.     public function setScriptPath($path)  
    50.     {  
    51.         //if (is_readable($path))   
    52.         {  
    53.             $this->_smarty->template_dir = $path;  
    54.             return;  
    55.         }  
    56.   
    57.         throw new Exception('Invalid path provided');  
    58.     }  
    59.   
    60.     /** 
    61.      * Retrieve all view script paths 
    62.      * 
    63.      * @return array 
    64.      */  
    65.     public function getScriptPaths()  
    66.     {  
    67.         return array($this->_smarty->template_dir);  
    68.     }  
    69.   
    70.     /** 
    71.      * Set a base path to all view resources 
    72.      * 
    73.      * @param  string $path 
    74.      * @param  string $classPrefix 
    75.      * @return void 
    76.      */  
    77.     public function setBasePath($path$prefix = 'Zend_View')  
    78.     {  
    79.         return $this->setScriptPath($path);  
    80.     }  
    81.   
    82.     /** 
    83.      * Add an additional path to view resources 
    84.      * 
    85.      * @param  string $path 
    86.      * @param  string $classPrefix 
    87.      * @return void 
    88.      */  
    89.     public function addBasePath($path$prefix = 'Zend_View')  
    90.     {  
    91.         return $this->setScriptPath($path);  
    92.     }  
    93.   
    94.     /** 
    95.      * Assign a variable to the view 
    96.      * 
    97.      * @param string $key The variable name. 
    98.      * @param mixed $val The variable value. 
    99.      * @return void 
    100.      */  
    101.     public function __set($key$val)  
    102.     {  
    103.         $this->_smarty->assign($key$val);  
    104.     }  
    105.   
    106.     /** 
    107.      * Allows testing with empty() and isset() to work 
    108.      * 
    109.      * @param string $key 
    110.      * @return boolean 
    111.      */  
    112.     public function __isset($key)  
    113.     {  
    114.         return (null !== $this->_smarty->get_template_vars($key));  
    115.     }  
    116.   
    117.     /** 
    118.      * Allows unset() on object properties to work 
    119.      * 
    120.      * @param string $key 
    121.      * @return void 
    122.      */  
    123.     public function __unset($key)  
    124.     {  
    125.         $this->_smarty->clear_assign($key);  
    126.     }  
    127.   
    128.     /** 
    129.      * Assign variables to the view script via differing strategies. 
    130.      * 
    131.      * Suggested implementation is to allow setting a specific key to the 
    132.      * specified value, OR passing an array of key => value pairs to set en 
    133.      * masse. 
    134.      * 
    135.      * @see __set() 
    136.      * @param string|array $spec The assignment strategy to use (key or array of key 
    137.      * => value pairs) 
    138.      * @param mixed $value (Optional) If assigning a named variable, use this 
    139.      * as the value. 
    140.      * @return void 
    141.      */  
    142.     public function assign($spec$value = null)  
    143.     {  
    144.         if (is_array($spec))   
    145.         {  
    146.             $this->_smarty->assign($spec);  
    147.             return;  
    148.         }  
    149.   
    150.         $this->_smarty->assign($spec$value);  
    151.     }  
    152.   
    153.     /** 
    154.      * Clear all assigned variables 
    155.      * 
    156.      * Clears all variables assigned to Zend_View either via {@link assign()} or 
    157.      * property overloading ({@link __get()}/{@link __set()}). 
    158.      * 
    159.      * @return void 
    160.      */  
    161.     public function clearVars()  
    162.     {  
    163.         $this->_smarty->clear_all_assign();  
    164.     }  
    165.   
    166.     /** 
    167.      * Processes a view script and returns the output. 
    168.      * 
    169.      * @param string $name The script name to process. 
    170.      * @return string The script output. 
    171.      */  
    172.     public function render($name)  
    173.     {  
    174.         return $this->_smarty->fetch($name);  
    175.     }  
    176.       
    177. /** 
    178. * Retrieve an assigned variable 
    179. * 
    180. * @param string $key The variable name. 
    181. * @return mixed The variable value. 
    182. */  
    183.     public function __get($key)  
    184.     {  
    185.         return $this->_smarty->get_template_vars($key);  
    186.     }  
    187. /** 
    188. * 用于兼容 smarty 的 display 方法 
    189. */  
    190.     public function display($resource_name$cache_id = null, $compile_id = null)   
    191.     {  
    192.         return $this->_smarty->display($resource_name$cache_id$compile_id);  
    193.     }  
    194.   
    195. /** 
    196. * 用于兼容 smarty 的 fetch 方法 
    197. */  
    198.     public function fetch($resource_name$cache_id = null, $compile_id = null)   
    199.     {  
    200.         return $this->_smarty->fetch($resource_name$cache_id$compile_id);  
    201.     }  
    202. }  


    数据库配置文件conf.ini

    1. [db]  
    2. db.adapter = "Pdo_Mysql"  
    3. db.params.charset = "utf8"  
    4. db.params.host = "localhost"  
    5. db.params.username = "root"  
    6. db.params.password = "chen"         ;密码  
    7. db.params.dbname = "chenpeijie"     ;数据库名  

     在此之前,应该先创建好相应的数据库。此处数据库库名为chenpeijie,用户名是root,密码是你安装MySQL时设置的密码。
    关于数据库的操作,可参考

    MySQL5.1中文参考手册

     

    models/Albums.php

    1. <?php  
    2. class Albums extends Zend_Db_Table  
    3. {  
    4.     protected $_name = 'albums';  
    5. }  
    6. ?>  

     

    controllers/IndexController.php

    1. <?php  
    2.   
    3. require_once 'Albums.php';  
    4.   
    5. class IndexController extends Zend_Controller_Action  
    6. {  
    7.      var $views;   /*模板对象*/  
    8.      function init()   
    9.      {  
    10.     $this->views = Zend_Registry::get('views');     
    11.      }   
    12.        
    13.     public function indexAction ()  
    14.     {  
    15.         //定义模版显示的变量   
    16.         $this->views->assign('page_title','My Albums');  
    17.         $albums = new Albums();  
    18.         $s = $albums->fetchAll();  
    19.         $this->views->assign('s',$s);    
    20.         $this->views->display('index.tpl');    
    21.     }  
    22. }  
    23. ?>  


    templates/views/index.tpl

      1. <style>  
      2. body,html {   
      3.         margin: 0 5px;   
      4.         font-family: Verdana,sans-serif;   
      5. }   
      6.   
      7. h1 {   
      8. font-size:1.4em;   
      9. color: #008000;   
      10. }  
      11. a {   
      12. color: #008000;   
      13. }   
      14. /* Table */   
      15. th {   
      16. text-align: left;   
      17. }   
      18. td, th {   
      19. padding-right: 5px;   
      20. }   
      21.   
      22. /* style form */   
      23. form dt {   
      24.  100px;   
      25. display: block;   
      26. }  
      27. </style>  
      28.   
      29. <html>  
      30. <head>  
      31. <title></title>  
      32. </head>  
      33. <body>  
      34. <h1>  
      35. <#$page_title#>  
      36. </h1>  
      37. <p><a href="http://localhost/home/public/add">Add new album</a></p>   
      38. <table>   
      39. <tr>   
      40.       <th>Title</th>   
      41.       <th>Artist</th>   
      42.       <th> </th>   
      43. </tr>   
      44. {foreach $s as $value}  
      45. <tr>   
      46.       <td><#$value.title#></td>   
      47.       <td><#$value.artist#></td>   
      48.       <td>   
      49.            <a href="http://localhost/home/public/index/edit">Edit</a>   
      50.            <a href="http://localhost/home/public/index/delete">Delete</a>   
      51.      </td>   
      52. </tr>   
      53. {/foreach}  
      54. </table>   
      55.   
      56. </body>  
      57. </html>  
  • 相关阅读:
    do...while(0)的妙用
    用位运算实现求绝对值-有效避开ifelse判断
    经典排序算法的C++ template封装
    DOM学习总结(二) 熊削铁如泥
    标签设计Loop标签
    asp:树型select菜单
    自家用的DataReapter分页代码
    正则表达式(一)
    C#中利用正则表达式实现字符串搜索
    解读C#中的正则表达式
  • 原文地址:https://www.cnblogs.com/dafa/p/2875580.html
Copyright © 2011-2022 走看看