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

查看:152
本文介绍了phalcon-route - Phalcon多模块项目的默认模块是如何确定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

情景

刚刚使用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'
    ),
));

解决方案

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

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

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

    return $router;
});

这篇关于phalcon-route - Phalcon多模块项目的默认模块是如何确定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆