问题
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