• 技术文章 >后端开发 >php教程

    phalcon框架多模块程序中 找不到非默认的模块

    2016-06-06 20:29:30原创789
    当我访问http://192.168.0.146/test/显示的是正常页面
    当我访问http://192.168.0.146/test/manage
    显示**Test\Frontend\Controllers\ManageController handler class cannot be loaded**
    我需要执行Test\Manage\Controllers\IndexController 还需要如何处理?

    项目创建过程如下

    phalcon project test modules --enable-webtools
    Phalcon DevTools (2.0.7)
    Success: Controller "index" was successfully created.

    Success: Project "test" was successfully created.

    复制app/frontend-》app/manage
    修改app/manage/controllers下程序的namespace 为Test\Manage\Controllers;
    修改app/manage/Module.php

    registerNamespaces(array(
                'Test\Manage\Controllers' => __DIR__ . '/controllers/',
                'Test\Manage\Models' => __DIR__ . '/models/',
            ));
    
            $loader->register();
        }
    
        /**
         * Registers services related to the module
         *
         * @param DiInterface $di
         */
        public function registerServices(DiInterface $di)
        {
            /**
             * Read configuration
             */
            $config = include APP_PATH . "/apps/manage/config/config.php";
    
            /**
             * Setting up the view component
             */
            $di['view'] = function () {
                $view = new View();
                $view->setViewsDir(__DIR__ . '/views/');
    
                return $view;
            };
    
            /**
             * Database connection is created based in the parameters defined in the configuration file
             */
            $di['db'] = function () use ($config) {
                return new DbAdapter($config->toArray());
            };
        }
    }

    修改test/config/modules.php

    registerModules(array(
        'frontend' => array(
            'className' => 'Test\Frontend\Module',
            'path' => __DIR__ . '/../apps/frontend/Module.php'
        ),
        'manage' => array(
            'className' => 'Test\Manage\Module',
            'path' => __DIR__ . '/../apps/manage/Module.php'
        )
        
        ));

    test/config/services.php未修改

    set('router', function () {
        $router = new Router();
    
        $router->setDefaultModule('frontend');
        $router->setDefaultNamespace('Test\Frontend\Controllers');
    
        return $router;
    });
    
    /**
     * The URL component is used to generate all kinds of URLs in the application
     */
    $di->set('url', function () {
        $url = new UrlResolver();
        $url->setBaseUri('/test/');
    
        return $url;
    });
    
    /**
     * Setting up the view component
     */
    $di->setShared('view', function () use ($config) {
    
        $view = new View();
    
        $view->setViewsDir($config->application->viewsDir);
    
        $view->registerEngines(array(
            '.volt' => function ($view, $di) use ($config) {
    
                $volt = new VoltEngine($view, $di);
    
                $volt->setOptions(array(
                    'compiledPath' => $config->application->cacheDir,
                    'compiledSeparator' => '_'
                ));
    
                return $volt;
            },
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
        ));
    
        return $view;
    });
    
    /**
     * Database connection is created based in the parameters defined in the configuration file
     */
    $di->set('db', function () use ($config) {
        return new DbAdapter($config->database->toArray());
    });
    
    /**
     * If the configuration specify the use of metadata adapter use it or use memory otherwise
     */
    $di->set('modelsMetadata', function () {
        return new MetaDataAdapter();
    });
    
    /**
     * Starts the session the first time some component requests the session service
     */
    $di->setShared('session', function () {
        $session = new SessionAdapter();
        $session->start();
    
        return $session;
    });
    
    /**
    * Set the default namespace for dispatcher
    */
    $di->setShared('dispatcher', function() use ($di) {
        $dispatcher = new Phalcon\Mvc\Dispatcher();
        $dispatcher->setDefaultNamespace('Test\Frontend\Controllers');
        return $dispatcher;
    });

    test/config/routes.php

    get("router");
    
    foreach ($application->getModules() as $key => $module) {
        $namespace = str_replace('Module','Controllers', $module["className"]);
        $router->add('//m.sbmmt.com/m/'.$key.'/:params', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 'index',
            'action' => 'index',
            'params' => 1
        ))->setName($key);
        $router->add('//m.sbmmt.com/m/'.$key.'/:controller/:params', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 1,
            'action' => 'index',
            'params' => 2
        ));
        $router->add('//m.sbmmt.com/m/'.$key.'/:controller/:action/:params', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ));
    }

    public/index.php

    handle()->getContent();
    
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    

    回复内容:

    当我访问http://192.168.0.146/test/显示的是正常页面
    当我访问http://192.168.0.146/test/manage
    显示**Test\Frontend\Controllers\ManageController handler class cannot be loaded**
    我需要执行Test\Manage\Controllers\IndexController 还需要如何处理?

    项目创建过程如下

    phalcon project test modules --enable-webtools
    Phalcon DevTools (2.0.7)
    Success: Controller "index" was successfully created.

    Success: Project "test" was successfully created.

    复制app/frontend-》app/manage
    修改app/manage/controllers下程序的namespace 为Test\Manage\Controllers;
    修改app/manage/Module.php

    registerNamespaces(array(
                'Test\Manage\Controllers' => __DIR__ . '/controllers/',
                'Test\Manage\Models' => __DIR__ . '/models/',
            ));
    
            $loader->register();
        }
    
        /**
         * Registers services related to the module
         *
         * @param DiInterface $di
         */
        public function registerServices(DiInterface $di)
        {
            /**
             * Read configuration
             */
            $config = include APP_PATH . "/apps/manage/config/config.php";
    
            /**
             * Setting up the view component
             */
            $di['view'] = function () {
                $view = new View();
                $view->setViewsDir(__DIR__ . '/views/');
    
                return $view;
            };
    
            /**
             * Database connection is created based in the parameters defined in the configuration file
             */
            $di['db'] = function () use ($config) {
                return new DbAdapter($config->toArray());
            };
        }
    }

    修改test/config/modules.php

    registerModules(array(
        'frontend' => array(
            'className' => 'Test\Frontend\Module',
            'path' => __DIR__ . '/../apps/frontend/Module.php'
        ),
        'manage' => array(
            'className' => 'Test\Manage\Module',
            'path' => __DIR__ . '/../apps/manage/Module.php'
        )
        
        ));

    test/config/services.php未修改

    set('router', function () {
        $router = new Router();
    
        $router->setDefaultModule('frontend');
        $router->setDefaultNamespace('Test\Frontend\Controllers');
    
        return $router;
    });
    
    /**
     * The URL component is used to generate all kinds of URLs in the application
     */
    $di->set('url', function () {
        $url = new UrlResolver();
        $url->setBaseUri('/test/');
    
        return $url;
    });
    
    /**
     * Setting up the view component
     */
    $di->setShared('view', function () use ($config) {
    
        $view = new View();
    
        $view->setViewsDir($config->application->viewsDir);
    
        $view->registerEngines(array(
            '.volt' => function ($view, $di) use ($config) {
    
                $volt = new VoltEngine($view, $di);
    
                $volt->setOptions(array(
                    'compiledPath' => $config->application->cacheDir,
                    'compiledSeparator' => '_'
                ));
    
                return $volt;
            },
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
        ));
    
        return $view;
    });
    
    /**
     * Database connection is created based in the parameters defined in the configuration file
     */
    $di->set('db', function () use ($config) {
        return new DbAdapter($config->database->toArray());
    });
    
    /**
     * If the configuration specify the use of metadata adapter use it or use memory otherwise
     */
    $di->set('modelsMetadata', function () {
        return new MetaDataAdapter();
    });
    
    /**
     * Starts the session the first time some component requests the session service
     */
    $di->setShared('session', function () {
        $session = new SessionAdapter();
        $session->start();
    
        return $session;
    });
    
    /**
    * Set the default namespace for dispatcher
    */
    $di->setShared('dispatcher', function() use ($di) {
        $dispatcher = new Phalcon\Mvc\Dispatcher();
        $dispatcher->setDefaultNamespace('Test\Frontend\Controllers');
        return $dispatcher;
    });

    test/config/routes.php

    get("router");
    
    foreach ($application->getModules() as $key => $module) {
        $namespace = str_replace('Module','Controllers', $module["className"]);
        $router->add('//m.sbmmt.com/m/'.$key.'/:params', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 'index',
            'action' => 'index',
            'params' => 1
        ))->setName($key);
        $router->add('//m.sbmmt.com/m/'.$key.'/:controller/:params', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 1,
            'action' => 'index',
            'params' => 2
        ));
        $router->add('//m.sbmmt.com/m/'.$key.'/:controller/:action/:params', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ));
    }

    public/index.php

    handle()->getContent();
    
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    

    pbulic/index.php
    require DIR . '/../config/routes.php';下面

    加上$di->set('router',$router);

    set('router',$router);
        echo $application->handle()->getContent();
    
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    

    题主给的信息不够,多模块设置里有几个比较重要的东西都不给出来,比如:routes.php 和 各个模块里的 Module.php,这两个都关系到多模块的加载。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:phalcon php
    上一篇:什么情景或者需求下会用的 Laravel 的 Event 和Listener? 下一篇:“抢座位”的逻辑问题:如何获得可帮和可拆的部分
    Web大前端开发直播班

    相关文章推荐

    • 基于在生产环境中使用php性能测试工具xhprof的详解_php实例• php桌面中心(二) 数据库写入_php实例• 请教在linux下mysql不支持视图吗• 密码是什么啊该如何解决• 请问php高手!关于mongodb的应用有关问题

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网