acelaya / expressive-slim-router

A router for Zend Expressive based on Slim framework's implementation
MIT License
7 stars 2 forks source link

setOptions method does not work #2

Closed shishi666 closed 7 years ago

shishi666 commented 7 years ago

Hello,

I try to use expressive-slim-router in my expressive v2 application.

I create a modular expressive application with zend-service-manager, plates-renderer and whoops.

I add this dependencies in my composer.json and i update dependencies.

"require" : {
    "php" : "^5.6 || ^7.0",
    "roave/security-advisories" : "dev-master",
    "zendframework/zend-component-installer" : "^1.0 || ^0.7.0",
    "zendframework/zend-config-aggregator" : "^0.2.0",
    "zendframework/zend-expressive" : "^2.0.2",
    "zendframework/zend-expressive-helpers" : "^4.0",
    "zendframework/zend-stdlib" : "^3.1",
    "zendframework/zend-servicemanager" : "^3.3",
    "zendframework/zend-expressive-platesrenderer" : "^1.3",
    "acelaya/expressive-slim-router" : "v3.0.0"
  },

And i change my config/autoload/router.global.php :

return [
    'dependencies' => [
        'factories' => [
            RouterInterface::class => Acelaya\Expressive\Factory\SlimRouterFactory::class,
        ],
    ],
];

Then i modify config/routes.php like this :

$app->get('/', App\Action\HomePageAction::class, 'home')
->setOptions([
    'defaults' => [
        'test' => 'test', 
    ]]);
$app->get('/api/ping', App\Action\PingAction::class, 'api.ping');

Finally when I var_dump Routes I can not see options...

Have you an idea to help me?

Thanks in advance

acelaya commented 7 years ago

That's out of this library's control.

Think that routing helper methods in tha app object (like get, post, put and such) are factory methods that create and register routes.

Those methods return a new Expressive Route object, but updating that object once it has been registered won't affect the registered route, because internally, the Slim router has a Slim Route instance that's not tied with that object in any way.

If you need to set route options while working with a programmatic approach like this, you have to set the options while registering the route, like this.

$route = new Zend\Expressive\Router\Route('/', App\Action\HomePageAction::class, 'GET', 'home');
$route->setOptions([
     'defaults' => [
        'test' => 'test', 
     ]
]);
$app->route($route);
shishi666 commented 7 years ago

Thanks for your explanations

acelaya commented 7 years ago

I'm happy to help :)