Add array key with value to the Router->routes?


#1

Is it possible to add a array key with value to the Router->routes ? i need this for my navbar class so i can filter out the public & private routes and generate the navbars ?


#2

In dependencies:

$router = $container->get('router');
$router->bar = '1234';

Then pass $container[‘router’] to your controller,
you then have access to $this->router->bar.


#3

You can also use Route::setArgument to add an argument and Route::getArgument to fetch it. e.g.

$app->get('/route1', function($req, $res, $args) { /* ... */ })
        ->setName('route1')
        ->setArgument('nav.public', false);

$app->get('/route2', function($req, $res, $args) { /* ... */ })
        ->setName('route2')
        ->setArgument('nav.public', true);

$app->get('/route3', function($req, $res, $args) { /* ... */ })
        ->setName('route3');


var_dump($router->getNamedRoute('route1')->getArgument('nav.public') === true); // false
var_dump($router->getNamedRoute('route2')->getArgument('nav.public') === true); // true
var_dump($router->getNamedRoute('route3')->getArgument('nav.public') === true); // false

#4

Learning something thanks for sharing.