开发环境是win7x64/WAMP server2.5/Phalcon-1.3.4
我现在用phalcon框架做一个小站点,内部有多个功能,但是在使用multi modules时不能识别正确的module
<?php
error_reporting(E_ALL);
class Application extends \Phalcon\Mvc\Application {
/**
* Register the services here to make them general or register in the ModuleDefinition to make them module-specific
*/
protected function _registerServices() {
$di = new \Phalcon\DI\FactoryDefault();
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
__DIR__ . '/../apps/library/',
)
)->register();
//Registering a router
$di->set('router', function () {
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule("Entrance");
$router->add('/:controller/:action', array(
'module' => 'entrance',
'controller' => 1,
'action' => 2,
));
$router->add("/functions1", array(
'module' => 'function1',
'controller' => 'index',
'action' => 'index',
));
return $router;
});
$di->set('url', function () {
$url = new \Phalcon\Mvc\Url();
$url->setBaseUri('/myproject');
return $url;
});
$this->setDI($di);
}
public function main() {
$this->_registerServices();
//Register the installed modules
$this->registerModules(array(
'Entrance' => array(
'className' => 'Multiple\Entrance\Module',
'path' => '../apps/entrance/Module.php',
),
'Impellerhelper' => array(
'className' => 'Multiple\Function1\Module',
'path' => '../apps/Function1/Module.php',
),
));
echo $this->handle()->getContent();
}
}
$application = new Application();
$application->main();
访问
http://localhost/myproject/index/index
没有问题
但是访问
http://localhost/myproject/function1/index/index
时却出现了问题,下面是xdebug的提示信息
Phalcon\Mvc\Dispatcher\Exception: Multiple\Entrance\Controllers\Function1Controller handler class cannot be loaded in D:\Program Files\wamp\www\myproject\public\index.php on line 79
应该说提示信息还是挺明确的,就是没能载入一个类,或者说没能找到那个类,但是我清查了代码,确实都写对了。
而且如果采用
$router->setDefaults(array(
"namespace" => "Multiple\Entrance",
"module" => "entrance',
"controller" => "index",
"action" => "index"
));
则连默认页面都会出现找不到类的错误
我还到stackoverflow上面去查了,有人有类似问题,但是回答都是修改router或者修改baseuri之类,我都已经尝试过了,还是不行
参考这个项目的多模块:https://github.com/KevinJay/PhalconCMS
因为router指定的module是function1
$router->add("/functions1", array( 'module' => 'function1', 'controller' => 'index', 'action' => 'index', ));
但是注册的module是Entrance和Impellerhelper
//Register the installed modules $this->registerModules(array( 'Entrance' => array( 'className' => 'Multiple\Entrance\Module', 'path' => '../apps/entrance/Module.php', ), 'Impellerhelper' => array( 'className' => 'Multiple\Function1\Module', 'path' => '../apps/Function1/Module.php', ), ));
因此,找不到function1。
建议参考 https://github.com/phalcon/mvc 修改
每个module.php里,记得在registerServices这个方法中设置默认的namespace
public function registerServices(DiInterface $di)
{
$dispatcher = $di['dispatcher'];
$dispatcher->setDefaultNamespace(__NAMESPACE__.'\Controllers');
// $view = $di['view'];
// $view->setViewsDir(__DIR__.'/views/');
}
估计比较马虎:
1,检查Moudle的注册块名字和文件夹对不对应,
2,大小写的问题
3,
php
$router->add("/functions1", array( 'module' => 'function1', 'controller' => 'index', 'action' => 'index', ));
所以,对应的链接应该是http://localhost/myproject/functions1/index/index
而不是:
http://localhost/myproject/function1/index/index