我在multimodule中加载module.php时遇到问题。
这是Cores\Application
<?php
namespace Cores;
use \Phalcon\Di\FactoryDefault,
\Phalcon\Loader,
//\Phalcon\Registry,
\Phalcon\Mvc\Router,
\Phalcon\Mvc\Application as PhalconApplication,
\Phalcon\Mvc\Events\Manager as EventsManager;
class Application extends PhalconApplication
{
protected $_config;
public function __construct()
{
$di = new FactoryDefault();
$this->_config = Config::factory();
parent::__construct($di);
}
private function __initModule()
{
$modulesDir = $this->_config->Config->application->modulesDir;
$dir = [];
/**
* Faster way to load directory to find Modules
*/
$objects = new \IteratorIterator(new \RecursiveDirectoryIterator($modulesDir));
foreach($objects as $key => $ojb){
if ($ojb->getFilename() != '.' AND $ojb->getFilename() != '..') {
$dir[] = [
$ojb->getFilename() => [
'className' => 'Modules' . DS . $ojb->getFilename() . DS . 'Module',
'path' => $ojb->getPath() . '/' . $ojb->getFilename() . '/Module.php'
]
];
}
}
parent::registerModules($dir);
}
private function __initRoute()
{
$di = $this->getDI();
$loader = new loader();
$loader
->registerDirs([BASEPATH . 'App/Libraries/'])
->register();
$di->set('router', function () {
$router = new Router();
$router->setDefaultModule('Administrator');
$router->add('/:controller/:action', [
'module' => 'Modules\Administrator',
'controller' => 1,
'action' => 2,
])->setName('Administrator');
return $router;
});
}
public function run()
{
$this->__initModule();
$this->__initRoute();
//echo '<pre>'.var_export($this->getModules(), true).'</pre>';
try {
echo $this->handle()->getContent();
} catch (\Exception $err) {
echo '<pre>'.var_export($err, true).'</pre>';
}
}
}
这是Modules\Administrator
module.php
<?php
namespace Modules\Administrator
use \Phalcon\Loader,
\Phalcon\Mvc\View,
\Phalcon\DiInterface,
\Phalcon\Mvc\Dispatcher,
\Phalcon\Mvc\ModuleDefinitionInterface;
class Module implements ModuleDefinitionInterface
{
/**
* Register a specific autoloader for the module
*/
public registerAutoloaders(DiInterface $di = null)
{
$loader = new Loader();
$loader->registerNamespaces(
[
'Modules\\Administrator\\Controllers' => APP_PATH . '/Modules/Administrator/Controllers',
'Moduels\\Administrator\\Models' => APP_PATH . '/Modules/Administrator/Models'
]
);
$loader->register();
}
/**
* Register specific services for the module
*/
public registerServices(DiInterface $di = null)
{
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespaces('Modules\\Administrator\\Controllers');
return $dispatcher;
});
$di->set('view', function () {
$view = new View();
$view->setViewsDir(APP_PATH . 'Administrator/Views/');
return $view;
});
}
}
我认为我在他们的网站上写了“喜欢”phalcon多模块但是没有加载module.php,为什么?
错误说Module \'Administrator\' isn\'t registered in the application container
请向我解释一下!
答案 0 :(得分:1)
1。 你不需要使用\ RecursiveDirectoryIterator。
$objects = new \IteratorIterator(new \RecursiveDirectoryIterator($modulesDir));
您只需要来自第一级的文件夹
$objects = new \IteratorIterator($modulesDir);
2。 斜杠/反斜杠的错误和主要错误是数组的结构。检查文章https://docs.phalconphp.com/en/3.0.1/reference/applications.html#multi-module
中的结构$dir[] = [
$ojb->getFilename() => [
'className' => 'Modules' . DS . $ojb->getFilename() . DS . 'Module',
'path' => $ojb->getPath() . '/' . $ojb->getFilename() . '/Module.php'
]
];
必须
$dir[$ojb->getFilename()] = [
'className' => 'Modules\\' . $ojb->getFilename() . '\\Module',
'path' => $ojb->getPath() . DS . $ojb->getFilename() . DS . 'Module.php'
];
3。 在路径错误的模块名称
$router->add('/:controller/:action', [
'module' => 'Modules\Administrator',
'controller' => 1,
'action' => 2,
])->setName('Administrator');
必须
$router->add('/:controller/:action', [
'module' => 'Administrator',
'controller' => 1,
'action' => 2,
])->setName('Administrator');