Okay, there are some (possible a lot) concepts of Routing/Controller/View that I have difficulties with.
I use https://github.com/mrjgreen/phroute as router and creates a route like this:
$router->get([‘/users/view/{id}', 'viewUser’], ['controllers\UserController','view'])
Now in the UserController view function I have the $id available and pass that along to the view. In the controller and in the view I need access to other objects as well. Should I create those objects at router level and pass them along to the controller, should I create those objects at controller level and pass them along to the view or should I do something completely different? I guess the use of global vars is a no-go.
I need those objects in a lot of controllers and creating them in the router seems like a way to initiate them just once instead of repeating it in every controller.
This seems to work just fine but it doesn’t seem quite right
$objects_I_need = [
'object1' => new Object,
'object2' => new Object
];
$router->get(['/users/view/{id}', 'viewUser'], function ($id) use ($object_I_need)
{
return (new Controller\UserController()->view($id, $objects_I_need);
});
Perhaps a Factory as described here is the way to go?
Thanks in advance for inputs