Answer the question
In order to leave comments, you need to log in
How to solve the problem Phalcon Router Service router cannot be resolved, with a multi-module project structure?
Hey!
I wanted to write a modular site on Phalcon.
Sketched a frame, everything works except Router.
Here is a structure
Fatal error: Uncaught exception 'Phalcon\Di\Exception' with message 'Service 'router' cannot be resolved' in C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php:262
Stack trace:
#0 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault))
#1 [internal function]: Phalcon\Di->get('router', NULL)
#2 [internal function]: Phalcon\Di->getShared('router')
#3 C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php(262): Phalcon\Di->offsetGet('router')
#4 C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php(113): VTransfer\Bootstrap->dispatch(Object(Phalcon\Di\FactoryDefault))
#5 C:\OpenServer\domains\Vtransfer.app\public\index.php(20): VTransfer\Bootstrap->run()
#6 {main}
thrown in C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php on line 262
private function dispatch($di)
{
$router = $di['router'];
$router->handle();
$view = $di['view'];
$dispatcher = $di['dispatcher'];
$response = $di['response'];
$dispatcher->setModuleName($router->getModuleName());
$dispatcher->setControllerName($router->getControllerName());
$dispatcher->setActionName($router->getActionName());
$dispatcher->setParams($router->getParams());
$moduleName = $this->camelize($router->getModuleName());
$ModuleClassName = $moduleName . '\Module';
if (class_exists($ModuleClassName))
{
$module = new $ModuleClassName;
$module->registerAutoloaders();
$module->registerServices($di);
}
$view->start();
if (true)
{
$debug = new \Phalcon\Debug();
$debug->listen();
$dispatcher->dispatch();
}
else
{
try {
$dispatcher->dispatch();
} catch (\Phalcon\Exception $e) {
// Errors catching
$view->setViewsDir(__DIR__ . '/modules/Index/views/');
$view->setPartialsDir('');
$view->e = $e;
if ($e instanceof \Phalcon\Mvc\Dispatcher\Exception) {
$response->setHeader(404, 'Not Found');
$view->partial('error/error404');
} else {
$response->setHeader(503, 'Service Unavailable');
$view->partial('error/error503');
}
$response->sendHeaders();
echo $response->getContent();
return;
}
}
$view->render(
$dispatcher->getControllerName(),
$dispatcher->getActionName(),
$dispatcher->getParams()
);
$view->finish();
$response = $di['response'];
$response->sendHeaders();
echo $response->getContent();
}
use Phalcon\Mvc\Router;
class BaseRouter extends Router
{
public function __construct()
{
parent::__construct();
$this->setDefaultModule('index');
$this->setDefaultController('index');
$this->setDefaultAction('index');
$this->add('/:module/:controller/:action/:params', array(
'module' => 1,
'controller' => 2,
'action' => 3,
'params' => 4
))->setName('default');
$this->add('/:module/:controller', array(
'module' => 1,
'controller' => 2,
'action' => 'index',
))->setName('default_action');
$this->add('/:module', array(
'module' => 1,
'controller' => 'index',
'action' => 'index',
))->setName('default_controller');
$this->removeExtraSlashes(true);
}
/**
* @param $group \Phalcon\Mvc\Router\Group
*/
public function addGroup($group)
{
$this->mount($group);
}
}
use Phalcon\Mvc\Router\Group as Group;
class Routes
{
/**
* @param $router \Phalcon\Mvc\Router
* @return mixed
*/
public function init($router)
{
/*
$index = new Group(array(
'module' => 'admin',
'controller' => 'index'
));
$index->setPrefix('/admin');
$index->add('/admin', array(
'controller' => 'index',
'action' => 'index'
));
$index->add('/save', array(
'controller' => 'users'
'action' => 'save'
));
$index->add('/edit/user/{id}', array(
'controller' => 'users'
'action' => 'edit'
));
// Добавление группы в общие правила маршрутизации
$router->mount($index);
*/
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question