Php 在多模块配置中,如何调用模块方法?

Php 在多模块配置中,如何调用模块方法?,php,phalcon,Php,Phalcon,在public/index.php中: public function main() { $this->_registerServices(); //Register the installed modules $this->registerModules(array( 'frontend' => array( 'className' => 'Multiple\Frontend\Module',

在public/index.php中:

public function main()
{

    $this->_registerServices();

    //Register the installed modules
    $this->registerModules(array(
        'frontend' => array(
            'className' => 'Multiple\Frontend\Module',
            'path' => '../apps/frontend/Module.php'
        ),
        'backend' => array(
            'className' => 'Multiple\Backend\Module',
            'path' => '../apps/backend/Module.php'
        )
    ));

    echo $this->handle()->getContent();
}
class Module
{
    public function registerAutoloaders()
    {

        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces(array(
            'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
            'Multiple\Frontend\Models' => '../apps/frontend/models/',
        ));

        $loader->register();
    }

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function registerServices($di)
    {

        //Registering a dispatcher
        $di->set('dispatcher', function () {
            $dispatcher = new \Phalcon\Mvc\Dispatcher();

            //Attach a event listener to the dispatcher
            $eventManager = new \Phalcon\Events\Manager();
            $eventManager->attach('dispatch', new \Acl('frontend'));

            $dispatcher->setEventsManager($eventManager);
            $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
            return $dispatcher;
        });


        //Registering the view component
        $di->set('view', function () {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir('../apps/frontend/views/');
            $view->registerEngines(array(".phtml" => 'volt'));
            return $view;
        });
    }
}

module.php中

public function main()
{

    $this->_registerServices();

    //Register the installed modules
    $this->registerModules(array(
        'frontend' => array(
            'className' => 'Multiple\Frontend\Module',
            'path' => '../apps/frontend/Module.php'
        ),
        'backend' => array(
            'className' => 'Multiple\Backend\Module',
            'path' => '../apps/backend/Module.php'
        )
    ));

    echo $this->handle()->getContent();
}
class Module
{
    public function registerAutoloaders()
    {

        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces(array(
            'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
            'Multiple\Frontend\Models' => '../apps/frontend/models/',
        ));

        $loader->register();
    }

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function registerServices($di)
    {

        //Registering a dispatcher
        $di->set('dispatcher', function () {
            $dispatcher = new \Phalcon\Mvc\Dispatcher();

            //Attach a event listener to the dispatcher
            $eventManager = new \Phalcon\Events\Manager();
            $eventManager->attach('dispatch', new \Acl('frontend'));

            $dispatcher->setEventsManager($eventManager);
            $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
            return $dispatcher;
        });


        //Registering the view component
        $di->set('view', function () {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir('../apps/frontend/views/');
            $view->registerEngines(array(".phtml" => 'volt'));
            return $view;
        });
    }
}

我想知道如何调用模块中的方法
registerServices
&&registerAutoloaders。

您可以在github中检查MVC回购:

其思想是应用程序有一个入口点
public\index.php
。在该文件中,您有:

public function main()
{
    $this->_registerServices();

    //Register the installed modules
    $this->registerModules(
        array(
            'frontend' => array(
                'className' => 'Multiple\Frontend\Module',
                'path' => '../apps/frontend/Module.php'
            ),
            'backend' => array(
                'className' => 'Multiple\Backend\Module',
                'path' => '../apps/backend/Module.php'
            )
        )
    );

    echo $this->handle()->getContent();
}
查看完整文件

现在在
public\index.php
文件中,您已经指示Phalcon有两个模块,一个是前端,一个是后端,以及相应
Module.php
文件的位置/类

在其中一个
Module.php
文件(比如前端)中,您会发现:

namespace Multiple\Frontend;

class Module
{

    public function registerAutoloaders()
    {

        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces(
            array(
                'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
                'Multiple\Frontend\Models'      => '../apps/frontend/models/',
            )
        );

        $loader->register();
    }

    public function registerServices($di)
    {

        //Registering a dispatcher
        $di->set(
            'dispatcher', 
            function () 
            {
                $dispatcher = new \Phalcon\Mvc\Dispatcher();

                //Attach a event listener to the dispatcher
                $eventManager = new \Phalcon\Events\Manager();
                $eventManager->attach('dispatch', new \Acl('frontend'));

                $dispatcher->setEventsManager($eventManager);
                $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
                return $dispatcher;
            }
        );

        //Registering the view component
        $di->set(
            'view', 
            function () 
            {
                $view = new \Phalcon\Mvc\View();
                $view->setViewsDir('../apps/frontend/views/');
                return $view;
            }
        );

        $di->set(
            'db', 
            function () 
            {
                return new \Phalcon\Db\Adapter\Pdo\Mysql(
                    array(
                        "host"     => "localhost",
                        "username" => "root",
                        "password" => "secret",
                        "dbname"   => "invo"
                    )
                );
            }
        );

    }

}
这些函数在模块注册后自动调用(使用
public\index.php
中的
registerModules
。在此模块中,
registerAutoloaders
registerServices
进一步自定义模块,让您更好地控制正在进行的操作。例如,您可能在一个模块中有不同的自动加载器,或者一个模块访问dif不同的数据库。您可以在特定的
Module.php
中设置所有这些数据库


我知道有人在谈论如何使用单/多配置制作一套全新的教程,但它尚未实现。

您可以在github中查看MVC回购:

您的应用程序有一个入口点
public\index.php

public function main()
{
    $this->_registerServices();

    //Register the installed modules
    $this->registerModules(
        array(
            'frontend' => array(
                'className' => 'Multiple\Frontend\Module',
                'path' => '../apps/frontend/Module.php'
            ),
            'backend' => array(
                'className' => 'Multiple\Backend\Module',
                'path' => '../apps/backend/Module.php'
            )
        )
    );

    echo $this->handle()->getContent();
}
查看完整文件

现在在
public\index.php
文件中,您已经指示Phalcon有两个模块,一个是前端,一个是后端,以及相应
Module.php
文件的位置/类

在其中一个
Module.php
文件(比如前端)中,您会发现:

namespace Multiple\Frontend;

class Module
{

    public function registerAutoloaders()
    {

        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces(
            array(
                'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
                'Multiple\Frontend\Models'      => '../apps/frontend/models/',
            )
        );

        $loader->register();
    }

    public function registerServices($di)
    {

        //Registering a dispatcher
        $di->set(
            'dispatcher', 
            function () 
            {
                $dispatcher = new \Phalcon\Mvc\Dispatcher();

                //Attach a event listener to the dispatcher
                $eventManager = new \Phalcon\Events\Manager();
                $eventManager->attach('dispatch', new \Acl('frontend'));

                $dispatcher->setEventsManager($eventManager);
                $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
                return $dispatcher;
            }
        );

        //Registering the view component
        $di->set(
            'view', 
            function () 
            {
                $view = new \Phalcon\Mvc\View();
                $view->setViewsDir('../apps/frontend/views/');
                return $view;
            }
        );

        $di->set(
            'db', 
            function () 
            {
                return new \Phalcon\Db\Adapter\Pdo\Mysql(
                    array(
                        "host"     => "localhost",
                        "username" => "root",
                        "password" => "secret",
                        "dbname"   => "invo"
                    )
                );
            }
        );

    }

}
这些函数在模块注册后自动调用(使用
public\index.php
中的
registerModules
。在此模块中,
registerAutoloaders
registerServices
进一步自定义模块,让您更好地控制正在进行的操作。例如,您可能在一个模块中有不同的自动加载器,或者一个模块访问dif不同的数据库。您可以在特定的
Module.php
中设置所有这些数据库


我知道有人在谈论用单个/多个配置制作一套全新的教程,但它还没有实现。

我的意思是,在public/index.php中,使用registerModules注册模块,模块的方法registerAutoloaders和registerServices会自动调用?谁调用这两种方法?在框架c语言定义中ition?Phalcon自动调用它。如果您在
Module.php
文件中有
registerAutoloaders
registerServices
,只要模块在
public\index.php
中注册,它们就会被调用。哦,我理解,但是如果这些方法(自动工作方法)不是在文档中写,我只是尝试一下,另外,控制器的初始化()方法是否自动调用?@netstu我将更正文档中提到的内容。谢谢!我的意思是,在public/index.php中,使用registerModules注册模块,模块的方法registerAutoloaders和registerServices会自动调用?谁调用这两个方法?在框架c语言定义中?Phalcon会自动调用它。如果您在
Module.php
文件中有
registerAutoloaders
registerServices
,只要模块在
public\index.php
中注册,它们就会被调用,哦,我明白了,但是如果这些方法(自动工作方法)没有写入文档,我只会尝试另外,控制器的initialize()方法是否自动调用?@netstu我将更正文档中提到的内容。谢谢!