简体   繁体   中英

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.

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}}.

The problem is that I am able to get the config everywhere but in the web services. 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. The error is that the UsersApi\\Controller\\core_framework_config method does not exist. Can anyone help me?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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