Router::run does not work

I tried this function:

https://getkirby.com/docs/toolkit/api/router/run

kirby()->routes(array( 
	array(
		'pattern' => 'test',
		'action' => function () {
			echo 'test';
		}
	),
	array(
		'pattern' => 'test2',
		'action' => function() {
			router::run('test');
		}
	)
));

I got these errors:

Strict standards: Non-static method Router::run() should not be called statically, assuming $this from incompatible context in C:\wamp\www\megastore\site\plugins\megastore\routes.php on line 58

Fatal error: Call to undefined method Kirby::filterer() in C:\wamp\www\megastore\kirby\toolkit\lib\router.php on line 239

Is it not possible to run a route from another route this way? I could get around it by create a function or class but the less code the better I think.

The router::run is not supposed to work like this?

A non-static method should be called like this $router->run().

I change it to:

'action' => function() {
    $router = new router();
    $router->run('test');
}

Now there are no errors but it does not run the functions in the test-route anyway.

I tried to change the route syntax to:

$router = new Router();
$router->register(array(
// Routes
));

But then no routes run anymore.

From what I understand you need to register the routes and then run the router and then call the route… You can register the routes in the constructor as well.

$router = new \Router($routes);

$router->filter(‘auth’,authFilter());
$router->filter(‘isInstalled’,isInstalledFilter());

$route = $router->run(kirby()->path());

call($route->action(), $route->arguments());

see here

It’s not intended to run routes from other routes. You may however export the function to a variable:

$testFunction = function () {
	echo 'test';
};

kirby()->routes(array( 
	array(
		'pattern' => 'test',
		'action' => $testFunction
	),
	array(
		'pattern' => 'test2',
		'action' => function() use($testFunction) {
			$testFunction();
		}
	)
));

Alright. I did something like that. Thanks! :slight_smile: