How to make imported/aliased names in AppController available to all controllers?

不羁岁月 提交于 2019-12-11 12:37:07

问题


I'm trying to add use Cake\ORM\TableRegistry in AppController to it can be used in all controllers, so there is no need to add it in each one. Is it possible?

i've tried this

<?php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\ORM\TableRegistry; <------ want to add this

class AppController extends Controller
{        
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('ConfigsComp');
    }
}

But i get this error:

Error: Class 'App\Controller\Admin\TableRegistry' not found


回答1:


Is it possible?

No, that's not how importing works, this is a per-file functionality, ie you need to put them in the file where you want to us them.

http://php.net/manual/...importing.php#language.namespaces.importing.scope

I'd suggest to use a proper IDE that auto-adds import statements in case you're having a hard time with adding them manually.



来源:https://stackoverflow.com/questions/32443159/how-to-make-imported-aliased-names-in-appcontroller-available-to-all-controllers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!