1

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();
}

module.php中:

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

4

1 回答 1

1

您可以在 github 中查看 MVC 存储库:

https://github.com/phalcon/mvc/tree/master/multiple

这个想法是您的应用程序有一个入口点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"
                    )
                );
            }
        );

    }

}

一旦模块被注册,这些函数就会被自动调用(registerModulespublic\index.php. 在这个模块中,registerAutoloadersregisterServices进一步自定义模块,让您更好地控制正在发生的事情。例如,您可能在一个模块中有一个与另一个模块不同的自动加载器,或者让一个模块访问不同的数据库。您可以在特定的Module.php.

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

于 2012-11-05T15:21:33.777 回答