We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Issue with using router in Micro application

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);

Java. Java decreased in popularity by about 6,000 job postings in 2018 compared to 2017, but is still extremely well-established. ... Python. Python grew in popularity by about 5,000 job postings over 2017. ... JavaScript. ... C++ ... C# ... PHP. ... Perl.

edited Aug '19

Sorry to necro this, but did you ever find a solution? I'm having the same problem.