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

Adding extra variables to Route objects

I have a ExtendedRoute class that extends Phalcon's Phalcon\Mvc\Route class which includes some added variables. I want this extended class to be used by the Routing system, i.e. Phalcon\Mvc\Router.

The functions in Phalcon\Mvc\Router, add, addPost, addGet etc., have internally (in the .zep code) a new Router() line which is the original Router class and not my extended class so these functions cannot be used for my purpose.

Another way I've tried also did not succeed. I overrode the constructor from Phalcon\Mvc\Router and added my extra params .After using the constructor and mounting the created ExtendedRoute object I realised internally Router->attach() is called, which is not available in the RouterInterface.


class ExtendedRoute extends \Phalcon\Mvc\Router\Route {
    protected $param1;

    protected $param2;

    public function __construct(string $pattern, $paths = null, $httpMethods = null, $param1 = null, $param2 = null)
    {
        parent::__construct($pattern, $paths, $httpMethods);
        $this->param1 = $param1;
        $this->param2 = $param2;
    }
}

//Adding routes/groups

//Attempt 1
$group = new RouterGroup();
$group->addPost(); //Not possible as internally original Router class is created

//Attempt 2
$extendedRoute = new ExtendedRoute('/path1', ['action' => path1], 'Post', 'newParam1', 'newParam2');
$this->router->attach($extendedRoute) //This is not available in RouterInterface
$this->router->mount($routergroup); //Without attach being called this does not work

Any suggestions on how to realise this?

edited Jun '18

You can modify Zephir source (see https://zephir-lang.com) and then re-compile Phalcon for your needs.

As shown, those are internals - so overriding is cumbersome task in this particular case.



2.7k

Was afraid for this answer. Thx