0

我正在尝试对 zend 框架进行小测试,以了解它是如何工作的。我创建了一个模块调查。现在,在这个控制器中,我想包含一个在上面一个目录中找到的文件,当我执行这个时,我得到

( ! ) Warning: require(../database.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0010  367280  {main}( )   ..\index.php:0
2   0.2738  5658600 Zend\Mvc\Application->run( )    ..\index.php:15
3   0.2780  5685104 Zend\EventManager\EventManager->trigger( )  ..\Application.php:297
4   0.2781  5685112 Zend\EventManager\EventManager->triggerListeners( ) ..\EventManager.php:208
5   0.2784  5686672 call_user_func ( )  ..\EventManager.php:464
6   0.2784  5686688 Zend\Mvc\DispatchListener->onDispatch( )    ..\EventManager.php:464
7   0.2785  5686688 Zend\Mvc\Controller\ControllerManager->get( )   ..\DispatchListener.php:90
8   0.2785  5686840 Zend\ServiceManager\AbstractPluginManager->get( )   ..\ControllerManager.php:114
9   0.2786  5686840 Zend\ServiceManager\ServiceManager->get( )  ..\AbstractPluginManager.php:110
10  0.2787  5687256 Zend\ServiceManager\ServiceManager->create( )   ..\ServiceManager.php:437
11  0.2787  5687288 Zend\ServiceManager\AbstractPluginManager->createFromInvokable( )   ..\ServiceManager.php:491
12  0.2788  5687704 Zend\Loader\StandardAutoloader->autoload( ) ..\ServiceManager.php:0
13  0.2788  5687760 Zend\Loader\StandardAutoloader->loadClass( )    ..\StandardAutoloader.php:217
14  0.2850  5694072 include( 'C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php' )  ..\StandardAutoloader.php:306

( ! ) Fatal error: require() [<a href='function.require'>function.require</a>]: Failed opening required '../database.php' (include_path='.;C:\php\pear') in C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0010  367280  {main}( )   ..\index.php:0
2   0.2738  5658600 Zend\Mvc\Application->run( )    ..\index.php:15
3   0.2780  5685104 Zend\EventManager\EventManager->trigger( )  ..\Application.php:297
4   0.2781  5685112 Zend\EventManager\EventManager->triggerListeners( ) ..\EventManager.php:208
5   0.2784  5686672 call_user_func ( )  ..\EventManager.php:464
6   0.2784  5686688 Zend\Mvc\DispatchListener->onDispatch( )    ..\EventManager.php:464
7   0.2785  5686688 Zend\Mvc\Controller\ControllerManager->get( )   ..\DispatchListener.php:90
8   0.2785  5686840 Zend\ServiceManager\AbstractPluginManager->get( )   ..\ControllerManager.php:114
9   0.2786  5686840 Zend\ServiceManager\ServiceManager->get( )  ..\AbstractPluginManager.php:110
10  0.2787  5687256 Zend\ServiceManager\ServiceManager->create( )   ..\ServiceManager.php:437
11  0.2787  5687288 Zend\ServiceManager\AbstractPluginManager->createFromInvokable( )   ..\ServiceManager.php:491
12  0.2788  5687704 Zend\Loader\StandardAutoloader->autoload( ) ..\ServiceManager.php:0
13  0.2788  5687760 Zend\Loader\StandardAutoloader->loadClass( )    ..\StandardAutoloader.php:217
14  0.2850  5694072 include( 'C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php' )  ..\StandardAutoloader.php:306

这是控制器文件

<?php

namespace Survey\Controller;
(require 'test.php');

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Config\Config;

class SurveyController extends AbstractActionController
{
    public function indexAction()
    {
         $plugin = $this->plugin('url');

    }

    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }
}

控制器中的相同代码,当我将 database.php 放在与控制器相同的目录中时,工作正常。为什么它不允许我包含上面的一个目录以及如何解决这个问题?

4

1 回答 1

1

请看一下index.phpZendSkeletonApplication 的。注意更改目录命令,即chdir().

此命令将 PHP 脚本的工作目录设置为应用程序的根目录。所以每当你想在你的脚本中包含一个文件时,你必须相对链接它们。在您的情况下,这将解决问题:

include __DIR__ . '/../database.php';

但是:仅通过文件名我会说您正在做的事情并不是真正想要的。应该不需要为了获取一些数据库参数而进行另一个文件系统交互。这类信息应该放入./config/autoload/database.local.php

您将能够以这种方式使用控制器中的数组语法访问参数:

$config   = $this->getServiceLocator()->get('config');
$dbParams = $config['dbParams'];  // or whatever you name it, default PHP Arrays
于 2013-02-07T07:42:12.410 回答