簡體   English   中英

Zend Framework 2:無法訪問命名空間文件中的全局變量

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

我看不到Zend Framework中開發的API的全局變量,我對此感到困惑。

文件系統是這樣的:

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

Apache被配置為執行一個前置文件,該文件的目標是加載config.php文件,該文件加載config.xml,因此這意味着在執行每個請求之前,它始終加載所有配置,然后從core_framework_config :: getConfig中檢索。 ()-> {{key}}。

問題是我可以在Web服務之外的任何地方獲得配置。 實際上,api文件夾中的index.php可以訪問core_framework_config及其所有變量,但是當我輸入一個命名空間文件(例如userController)時,在類的內部和外部執行該錯誤,但總是在命名空間文件中執行。 錯誤是UsersApi \\ Controller \\ core_framework_config方法不存在。 誰能幫我?

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