我正在尝试对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放在与控制器相同的目录中时,工作正常。 为什么它不允许我包含一个上面的目录以及如何解决这个问题?
答案 0 :(得分:1)
请查看ZendSkeletonApplication的index.php
。注意更改目录命令chdir()
。
此命令将PHP-Scripts的工作目录设置为应用程序的ROOT。因此,只要您想在脚本中包含文件,就必须相对地链接它们。在你的情况下,这将解决问题:
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