Ok, to create the multimodule application with dividing the frontend and the backend we need to do the next:
Folder's structure:
app
frontend
Module.php
backend
Module.php
models
public
...trash...
In every Module.php we need to put such strings:
class Module implements \Phalcon\Mvc\ModuleDefinitionInterface
{
public function registerAutoloaders()
{
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(
array(
'Backend\Controllers' => '../app/backend/controllers/',
// For frontend's module.php: 'Frontend\Controllers' => '../app/backend/controllers/',
)
);
$loader->register();
}
public function registerServices($di)
{
$di->set('dispatcher', function() use ($di){
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setDefaultNamespace("Backend\Controllers\\");
// For frontend's module.php: $dispatcher->setDefaultNamespace("Frontend\Controllers\\");
return $dispatcher;
});
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/backend/views/');
// For frontend's module.php: $view->setViewsDir('../app/frontend/views/');
return $view;
});
// This function will 'divide' parts of the application with the correct url:
$di->set('url', function() use ($di) {
$url = new \Phalcon\Mvc\Url();
$url->setBaseUri("/adminko/");
// For frontend module.php: $url->setBaseUri("/");
return $url;
});
and dont forget to instance $router:
$router->add('/adminko', array(
'module' => "backend",
'action' => "index",
'params' => "index"
));
$router->add('/adminko/:controller', array(
'module' => "backend",
'controller' => 1,
'action' => "index"
));
$router->add('/admin/:controller/:action/:params', array(
'module' => "backend",
'controller' => 1,
'action' => 2,
'params' => 3
));
And now the link "https://site.com/adminko" will forward us to backend, where all links on the page will be prefixed with "adminko".
The link "https://site.com" will forward us to frontend, where all links on the page will be not prefixed.
It is very good behavior. But, unfortunately, in this case \Phalcon\Tag::stylesheetLink() and \Phalcon\Tag::javascriptInclude() will not work correctly.
But I have an another problem:
How to redirect the user to the frontend from the backend when the user pressed the link in the frontend with the backend's prefix?
And how to not redirect the user to the frontend from the backend when the user pressed the same link in the backend?
It's all about authorization. I want to authorize my users in the frontend and in the backend with single AuthController that situated in the backend. Now if authorization was success the user forwards to the backend's index page:
...
if ($this->security->checkHash($password, $user->password))
{
$this->session->set('auth', 1);
return $this->response->redirect('index/index');
// return $this->dispatcher->forward(array('controller'=>'index')); // the same behavior as redirect
}
Help me, please :)