简体   繁体   English

Zend Framework 2:无法访问命名空间文件中的全局变量

[英]Zend Framework 2: Not being able to access to global vars in namespaced files

I cannot see global vars from an API developed in Zend Framework and I am stuck with that. 我看不到Zend Framework中开发的API的全局变量,我对此感到困惑。

The file system is like this: 文件系统是这样的:

Project
     Site
         prepend.php (no namespace)
         (many folders of the site) ...
         api
             index.php
             module
                usersApi
                    module.php (namespace UsersApi)
                    src
                       usersApi
                           controller
                                AbstractRestfulJsonController.php (n: UsersApi\Controller)
                                usersController.php (n: UsersApi\Controller)
         core
             config
                config.php
                config.xml

The Apache is configured to execute a prepend file which aim is to load the config.php file, which loads the config.xml so it means that before every request is executed, it always loads all the configuration and then retrieves from core_framework_config::getConfig()->{{key}}. Apache被配置为执行一个前置文件,该文件的目标是加载config.php文件,该文件加载config.xml,因此这意味着在执行每个请求之前,它始终加载所有配置,然后从core_framework_config :: getConfig中检索。 ()-> {{key}}。

The problem is that I am able to get the config everywhere but in the web services. 问题是我可以在Web服务之外的任何地方获得配置。 In fact the index.php inside api folder can access the core_framework_config and all their vars but when I enter in a namespaced file (eg userController) I get an error executing it both inside and outside the class, but always in namespaced files. 实际上,api文件夹中的index.php可以访问core_framework_config及其所有变量,但是当我输入一个命名空间文件(例如userController)时,在类的内部和外部执行该错误,但总是在命名空间文件中执行。 The error is that the UsersApi\\Controller\\core_framework_config method does not exist. 错误是UsersApi \\ Controller \\ core_framework_config方法不存在。 Can anyone help me? 谁能帮我?

UsersController.php: UsersController.php:

namespace UsersApi\Controller;

use UsersApi\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

class UsersController extends AbstractRestfulJsonController
{
    public function get($id) {   // Action used for GET requests with resource Id
        // I MUST RELOAD THE CONFIG FILE BECAUSE I CANNOT ACCESS TO CONFIGURED FILE

        $items_per_page = core_framework_config::getConfig()->items_per_page; // this fails with the error mentioned before
    }
}

我通过在开始处添加斜杠来修复它:\\ core_framework_config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM