Phalcon 多模块中未加载模块

Module not loading in Phalcon multi module

我在多模块中加载 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;
        });
    }
}

我想我在他们的网站上写了 "like" phalcon 多模块但是 module.php 没有加载,为什么?

错误说 Module \'Administrator\' isn\'t registered in the application container

请给我解释一下!

1。 你不需要使用 \RecursiveDirectoryIterator.

$objects = new \IteratorIterator(new \RecursiveDirectoryIterator($modulesDir));

您只需要第一层的文件夹

$objects = new \IteratorIterator($modulesDir);

2。 slash/backslash 的错误,主要错误是数组的结构。检查文章 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');