phalconphp中的多模块MVC结构

Par*_*ars 4 php phalcon

嗨,我正在尝试为前端和后端实现多模块MVC,就像phalconphp文档中的内容一样.但我不能让它发挥作用.大约一个小时但我真的无法理解问题出在哪里.

任何人都可以指导我如何为前端和后端的多模块mvc制作骨架.

我应该把Moudle.php放在前端和后端
我应该把它放在public/index.php中的bootstrap文件中
以及我需要的任何额外文件或信息.

Rog*_*mas 7

GitHub上phalcon/mvc存储​​库中的代码将有所帮助.你可以在这里找到它:https: //github.com/phalcon/mvc/tree/master/multiple

更具体地说,您将对以下内容感兴趣:

https://github.com/phalcon/mvc/blob/master/multiple/public/index.php https://github.com/phalcon/mvc/blob/master/multiple/apps/backend/Module.php

我倾向于在index.php中使用它:

$application = new \Phalcon\Mvc\Application($di);

// Register the installed modules
$application->registerModules(
    array(
        'web' => array(
            'className' => 'Apps\Web\Module',
            'path'      => '../apps/web/Module.php',
        )
    )
);

echo $application->handle()->getContent();
Run Code Online (Sandbox Code Playgroud)

在我的Module.php中:

<?php

namespace Apps\Web;

use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use Phalcon\Mvc\ModuleDefinitionInterface;

class Module implements ModuleDefinitionInterface
{

    /**
     * Register a specific autoloader for the module
     */
    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(
            array(
                'Apps\Web\Controllers' => '../apps/web/controllers/',
            )
        );

        $loader->register();
    }

    /**
     * Register specific services for the module
     * @param \Phalcon\DI\FactoryDefault $di
     */
    public function registerServices($di)
    {
        //Registering a dispatcher
        $di->set(
            'dispatcher',
            function() use ($di) {
                $eventsManager = $di->getShared('eventsManager');
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace('Apps\Web\Controllers');
                $eventsManager->attach(
                    'dispatch:beforeException',
                    function($event, $dispatcher, $exception) use ($di) {
                        /* @var $dispatcher \Phalcon\Mvc\Dispatcher */
                        switch ($exception->getCode()) {
                            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                                $di->set('lastException', $exception);
                                $dispatcher->forward(
                                    array(
                                        'module' => 'web',
                                        'controller' => 'error',
                                        'action' => 'notFound',
                                    )
                                );
                                return false;
                            default:
                                $di->set('lastException', $exception);
                                $dispatcher->forward(
                                    array(
                                        'module' => 'web',
                                        'controller' => 'error',
                                        'action' => 'uncaughtException',
                                    )
                                );
                                return false;
                        }
                    }
                );
                $dispatcher->setEventsManager($eventsManager);
                return $dispatcher;
            }
        );
    }

}
Run Code Online (Sandbox Code Playgroud)

希望这有帮助!