There are different general types of recipes, and each general type has different methods. The database queries the ID provided in the URL to determine the type and a different class is used.
One option is:
$c['recipeFactory'] = function ($c) {
return new RecipeFactory($this->get('pdo'));
};
$app->put('/recipes/{id:[0-9]+}', function (Request $request, Response $response, $args) {
//Factory will query DB and create and return object
$obj=$this->recipeFactory->create($args['id']);
$obj->update($request->getParsedBody());
});
Seems like more often than not, a static method is used to implement a factory, so maybe I should do the following:
$app->put('/recipes/{id:[0-9]+}', function (Request $request, Response $response, $args) {
$obj=RecipeFactory::create($args['id'], $this->get('pdo'));
$rs=$obj->update($request->getParsedBody());
});
But then, I am not using the container, but the following will not work as $args['id']
not defined.
$c['recipeFactory'] = function ($c) {
return new RecipeFactory($args['id'], $c->get('pdo'));
};
Should static methods be used for the factory method?
How should the factory pattern be implemented with Slim Framework?