加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

phalconphp中的多模块MVC结构

发布时间:2020-12-13 17:47:03 所属栏目:PHP教程 来源:网络整理
导读:嗨,我正在尝试为前端和后端实现多模块MVC,就像 phalconphp documentations中那样.但我无法使其工作.大约一个小时但我真的无法理解问题出在哪里. 任何人都可以指导我如何为前端和后端的多模块mvc制作骨架. 我应该把Moudle.php放在前端和后端 还有什么我应该放
嗨,我正在尝试为前端和后端实现多模块MVC,就像 phalconphp documentations中那样.但我无法使其工作.大约一个小时但我真的无法理解问题出在哪里.

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

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

解决方法

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 PhalconMvcApplication($di);

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

echo $application->handle()->getContent();

在我的Module.php中:

<?php

namespace AppsWeb;

use PhalconLoader;
use PhalconMvcDispatcher;
use PhalconMvcView;
use PhalconMvcModuleDefinitionInterface;

class Module implements ModuleDefinitionInterface
{

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

        $loader = new Loader();

        $loader->registerNamespaces(
            array(
                'AppsWebControllers' => '../apps/web/controllers/',)
        );

        $loader->register();
    }

    /**
     * Register specific services for the module
     * @param PhalconDIFactoryDefault $di
     */
    public function registerServices($di)
    {
        //Registering a dispatcher
        $di->set(
            'dispatcher',function() use ($di) {
                $eventsManager = $di->getShared('eventsManager');
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace('AppsWebControllers');
                $eventsManager->attach(
                    'dispatch:beforeException',function($event,$dispatcher,$exception) use ($di) {
                        /* @var $dispatcher PhalconMvcDispatcher */
                        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','action' => 'uncaughtException',)
                                );
                                return false;
                        }
                    }
                );
                $dispatcher->setEventsManager($eventsManager);
                return $dispatcher;
            }
        );
    }

}

希望这有帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读