public function main()
{
$this->_registerServices();
//Register the installed modules
$this->registerModules(array(
'frontend' => array(
'className' => 'Multiple\Frontend\Module',
'path' => '../apps/frontend/Module.php'
),
'backend' => array(
'className' => 'Multiple\Backend\Module',
'path' => '../apps/backend/Module.php'
)
));
echo $this->handle()->getContent();
}
class Module
{
public function registerAutoloaders()
{
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(array(
'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
'Multiple\Frontend\Models' => '../apps/frontend/models/',
));
$loader->register();
}
/**
* Register the services here to make them general or register in the ModuleDefinition to make them module-specific
*/
public function registerServices($di)
{
//Registering a dispatcher
$di->set('dispatcher', function () {
$dispatcher = new \Phalcon\Mvc\Dispatcher();
//Attach a event listener to the dispatcher
$eventManager = new \Phalcon\Events\Manager();
$eventManager->attach('dispatch', new \Acl('frontend'));
$dispatcher->setEventsManager($eventManager);
$dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
return $dispatcher;
});
//Registering the view component
$di->set('view', function () {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../apps/frontend/views/');
$view->registerEngines(array(".phtml" => 'volt'));
return $view;
});
}
}
我想知道方法registerServices
&&调用模块中的registerAutoloaders
。
答案 0 :(得分:1)
您可以在github中查看MVC repo:
https://github.com/phalcon/mvc/tree/master/multiple
我们的想法是您的应用程序有一个入口点public\index.php
。在该文件中,您有:
public function main()
{
$this->_registerServices();
//Register the installed modules
$this->registerModules(
array(
'frontend' => array(
'className' => 'Multiple\Frontend\Module',
'path' => '../apps/frontend/Module.php'
),
'backend' => array(
'className' => 'Multiple\Backend\Module',
'path' => '../apps/backend/Module.php'
)
)
);
echo $this->handle()->getContent();
}
查看完整档案here。
现在在public\index.php
文件中,您已指示Phalcon您有两个模块,一个是前端,另一个是后端和相应Module.php
文件的位置/类。
在其中一个Module.php
文件(比如前端)中,你会发现:
namespace Multiple\Frontend;
class Module
{
public function registerAutoloaders()
{
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(
array(
'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/',
'Multiple\Frontend\Models' => '../apps/frontend/models/',
)
);
$loader->register();
}
public function registerServices($di)
{
//Registering a dispatcher
$di->set(
'dispatcher',
function ()
{
$dispatcher = new \Phalcon\Mvc\Dispatcher();
//Attach a event listener to the dispatcher
$eventManager = new \Phalcon\Events\Manager();
$eventManager->attach('dispatch', new \Acl('frontend'));
$dispatcher->setEventsManager($eventManager);
$dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
return $dispatcher;
}
);
//Registering the view component
$di->set(
'view',
function ()
{
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../apps/frontend/views/');
return $view;
}
);
$di->set(
'db',
function ()
{
return new \Phalcon\Db\Adapter\Pdo\Mysql(
array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
)
);
}
);
}
}
注册模块后会自动调用这些函数(registerModules
中有public\index.php
。在此模块中,registerAutoloaders
和registerServices
进一步自定义模块更好地控制正在发生的事情。例如,您可能在一个模块中有一个不同的自动加载器,或者有一个模块访问另一个模块而不是另一个模块。您可以设置特定Module.php
中的所有模块。
我知道有关于使用单/多配置制作一套全新的教程的讨论,但它还没有实现。