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

why not overwrite router?

/**
 * Return array of the Collections, which define a group of routes, from
 * routes/collections.  These will be mounted into the app itself later.
 */
$di->set('router', function(){
    $oRouter = new \Phalcon\Mvc\Router();

    //Define a route
    $oRouter->addGet(
        "/admin/users/my-profile",
        array(
            "controller" => "users",
            "action"     => "profile",
        )
    );

    //Another route
    $oRouter->addGet(
        "/admin/users/change-password",
        array(
            "controller" => "users",
            "action"     => "changePassword",
        )
    );

    return $oRouter;
});

i trying to use overwrite the router in micro app. (intend to using modify the cmoore4/phalcon-rest)

However, the results came out the route has not been added.

{"_meta":{"status":"SUCCESS","count":7},"records":{"gET":["\/"],"pOST":[],"pUT":[],"pATCH":[],"dELETE":[],"hEAD":[],"oPTIONS":[]}}

do i have a problem with this my codes ?

Have you tried to use a shared service?

Or maybe this works:

$di->getShared('router')->add(
    '/your-route',
    array(
        'controller' => 'yourController',
        'action' => 'yourAction'
    )
);
edited Dec '14
$di->setShared('router', function() use ($di){
    $oRouter = new \Phalcon\Mvc\Router();

    //Define a route
    $oRouter->addGet(
        "/example",
        array(
            "controller" => "\PhalconRest\Controllers\ExampleController",
            "action"     => "get",
        )
    );

    return $oRouter;
});

$this->setDI( $di);
// $this => \Phalcon\Mvc\Micro

if( $this->getRouter() === $this->getDI('router'))
{
var_dump('same');
}
else
{
var_dump('different');
}

// result => string(9) "different"

Is it unavailable to overwrite router in micro?