Hi I was trying to inject Router to Micro application like it is written in the docs (project structure fits to PSR-4). Below is simplified code:
abstract class AbstractBootstrap { ... protected function initDi() { $this->diContainer = new Di\FactoryDefault(); Di::setDefault($this->diContainer); }
protected function initLoader()
{
require_once ROOT_PATH . '/vendor/autoload.php';
}
protected function initRouter()
{
$router = new Router();
$router->add(
'/login',
[
'namespace' => '\Some\NameSpace\Controllers',
'controller' => 'Auth',
'action' => 'login'
]
);
$this->diContainer->set('router',$router,true);
}
protected function runApplication()
{
return $this->application->handle();
}
}
And get exception: Matched route doesn't have an associated handler
After looking up source code of micro.zep i found the following:
let router = <RouterInterface> dependencyInjector->getShared("router");
/**
* Handle the URI as normal
*/
router->handle(uri);
/**
* Check if one route was matched
*/
let matchedRoute = router->getMatchedRoute();
if typeof matchedRoute == "object" {
if !fetch handler, this->_handlers[matchedRoute->getRouteId()] {
throw new Exception("Matched route doesn't have an associated handler");
}
I guess that when i inject router it doesn't update internal _handlers array
Is it a bug? In docs it is writen that router injection is possible for micro app:
use Phalcon\Mvc\Micro; use Phalcon\Mvc\Router;
$router = new Router();
$router->addGet( '/orders/display/{name}', 'OrdersClass::display'; } );
$app = new Micro(); $app->setService('router', $router, true);