Phalcon多模块项目的默认模块是如何确定的?

发布于 09-02 21:01 字数 2357 浏览 19 评论 0

情景

刚刚使用phalcon-dev-tools生成了一个phalcon的多模块程序,除了默认的frontend模块外,自己添加了other模块

情况描述

  • 正常的通过www.test.com/frontend/test/test和www.test.com/other/test/test访问都没有问题

  • 直接访问www.test.com/test/test走的是frontend的路由(我没有通过$application->setDefaultModule设置默认模块)

测试过程

  • 我考虑可能是因为模块的注册顺序会影响默认情况,但是对调frontend和other模块的注册顺序结果仍然没有改变,依旧是走frontend的路由

  • 当我使用$application->setDefaultModule('other')将other设置为默认模块的时候,仍然不起作用

问题总结

1.希望了解phalcon多模块的默认模块是怎么确定的
2.为什么当我使用setDefaultModule的时候仍然不起作用
3.\Phalcon\Mvc\Application中的setDefaultModule和\Phalcon\Mvc\Router中的setDefaultModule有什么不同

相关代码

测试代码只在默认的多模块应用中添加了other模块相关内容,代码如下

  • other模块的Module.php文件

<?php

namespace Multi\Other;

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


class Module implements ModuleDefinitionInterface
{
    /**
     * Registers an autoloader related to the module
     *
     * @param DiInterface $di
     */
    public function registerAutoloaders(DiInterface $di = null)
    {

        $loader = new Loader();

        $loader->registerNamespaces(array(
            'Multi\Other\Controllers' => __DIR__ . '/controllers/',
            'Multi\Other\Models' => __DIR__ . '/models/',
        ));

        $loader->register();
    }

    /**
     * Registers services related to the module
     *
     * @param DiInterface $di
     */
    public function registerServices(DiInterface $di)
    {

        /**
         * Setting up the view component
         */
        $di['view'] = function () {
            $view = new View();
            $view->setViewsDir(__DIR__ . '/views/');

            return $view;
        };


    }
}
  • 模块注册文件

<?php

/**
 * Register application modules
 */
$application->registerModules(array(
    'other' => [
        'className' => 'Multi\Other\Module',
        'path' => __DIR__ . '/../apps/other/Module.php'
    ],
    'frontend' => array(
        'className' => 'Multi\Frontend\Module',
        'path' => __DIR__ . '/../apps/frontend/Module.php'
    ),
));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韬韬不绝2022-09-09 21:01:12

被自己蠢哭了
答案就在默认的service.php中

/**
 * Registering a router
 */
$di->setShared('router', function () {
    $router = new Router();

    $router->setDefaultModule('other');
    $router->setDefaultNamespace('Multi\Other\Controllers');

    return $router;
});
情愿2022-09-09 21:01:12

楼主,你是在哪里学习phalcon的,感觉网上资料好少。我也想建立多模块,然后二级域名对应到子模块上。这样很是舒服,教教我呀。

虐人心2022-09-09 21:01:12

生成了一个phalcon的多模块程序 的命令是什么样子的?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文