【问题标题】:How to change include directory of Zend Framework如何更改 Zend Framework 的包含目录
【发布时间】:2009-03-30 06:14:30
【问题描述】:

我收到以下错误消息:

Warning: include_once(Zend\Db.php) [function.include-once]: 
failed to open stream: No such file or directory in 
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83    

Warning: include_once() [function.include]: 
Failed opening 'Zend\Db.php' for inclusion (include_path='VPZ/') in 
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83

Warning: require_once(Zend/Exception.php) 
[function.require-once]: failed to open stream: 
No such file or directory in 
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87

Fatal error: require_once() [function.require]: 
Failed opening required 'Zend/Exception.php' (include_path='VPZ/') in 
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87

我想包含 ZendXXX\Db.php

如何改变它

【问题讨论】:

    标签: zend-framework


    【解决方案1】:

    创建一个目录(比如'lib'),并将您的 Zend 目录放入其中。所以你的目录结构是这样的:

    - application
    - lib
      |- Zend
    - wwwroot
      |- index.php
    

    现在您应该将 lib 添加到包含路径中。编辑你的 index.php 文件:

    $includePath = array();
    $includePath[] = '.';
    $includePath[] = './../application';
    $includePath[] = './../lib';
    $includePath[] = get_include_path();
    $includePath = implode(PATH_SEPARATOR,$includePath);
    set_include_path($includePath);
    

    现在您的包含路径中有您的库。你可以像这样包含所有 Zend 组件:

    include 'Zend/Loader.php';
    require_once 'Zend/Db.php';
    

    最好的方法是先包含 Zend_Loader 然后用它来加载类。这样做:

    require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Db');
    

    您还可以注册自动加载类。只需将这一行添加到您的代码之后:

    Zend_Loader::registerAutoLoad('Zend_Loader',true);
    

    现在您不需要包含文件来调用类。只是实例化你的类:

    $session = new Zend_Session_Namespace('user');

    不需要包含“Zend/Session/Namespace.php”。

    【讨论】:

    • 这就是要走的路!如果你使用自动加载,你只需要包含加载器,然后你就可以调用整个框架中的每个类和函数,而不需要包含任何东西。
    【解决方案2】:

    使用set_include_path()See PHP.net documentation

    例子:

     set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Zend');
    

    【讨论】:

      【解决方案3】:

      我通常将框架文件存储在“库”文件夹下:

      • 应用
      • public_html
        • 禅德
        • 常见
        • 等等……

      然后在我的引导文件或前端控制器中,我将该“库”文件夹添加到包含路径:

      set_include_path(get_include_path() . PATH_SEPARATOR . '../library');
      

      另见:

      【讨论】:

      • 这将违反 Zend 类和文件命名约定...is.gd/pBLM
      【解决方案4】:

      其他建议说这样做的原因是因为这是一个糟糕的举动 - 换句话说,你做错了。

      您可以创建一个子目录并将其命名为 Zendxxx,但是您必须将其添加到您的 include_path 中,并在每次添加新命名的版本时更改它。

      我冒昧地猜测一下,并说您没有测试网站的好方法(因此您想将其锁定到 ZF 的特定版本),此外,您没有使用修订版控制,因此如果您在直接在服务器上更改实时运行的代码时发现问题,您希望站点目录中的所有以前版本的代码都能够返回。

      【讨论】:

        【解决方案5】:

        没有库的项目并且包括来自一个位置的库
        项目 C:\xampp\htdocs\my\application
        库 C:\xampp\Zend\库

        修改 index.php

        // Ensure library/ is on include_path
        set_include_path(implode(PATH_SEPARATOR,  
                   array(realpath(APPLICATION_PATH.'/../../../Zend/library'),get_include_path(),)));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          相关资源
          最近更新 更多