2
When using some ready-made frameworks (such as Laravel and Codeigniter), I noticed that Models are usually extended from other classes that usually connect directly to the database, so I understood this is not mandatory, as the Model should contain the "business logic" ("business rules") and not necessarily connections to databases.
If what I understand is correct, frameworks only make the connection to the auto-connection for ease, so when we use the Model the database settings are automatically loaded by the framework at the time the Model is "built" by connecting to the database, ie is just to automate.
My question is this, I can use a webservice within Model, as being "equivalent" to the database?
For example (Note that this example is completely fictitious):
class MyModel
{
public function putItem($preco, $descricao)
{
$rest = Rest('ws.example.com', 'PUT /', array(
'price' => $price,
'description' => $descricao
));
//Se o produto foi adicionado o HTTP status é 200 então putItem retorna TRUE
return $response->status === 200;
}
public function deleteItem($id)
{
Rest('ws.example.com', 'DELETE /items/{id}', array(
'id' => $id
));
$response = json_decode($rest->getRespose());
//Se o produto foi deletado o HTTP status é 200 então putItem retorna TRUE
return $response->status === 200;
}
public function getItem($id)
{
$rest = Rest('ws.example.com', 'GET /items/{id}', array(
'id' => $id
));
$response = json_decode($rest->getRespose());
if ($response->status === 200) {
//Se o produto existe o HTTP status é 200 então retorna informações dele
return $response->data;
}
//Se não retorna FALSE
return NULL;
}
}
Or should I do otherwise and what would be this way?
Thanks for the reply, sorry for the delay to answer, this question of MVC is still strange to me, yes I understood the reason of "not use" within the Model, if I understand correctly the Model is to "represent the bank" in the application, so the use of REST should be within the action directly, got it right? Please correct me if I’m wrong.
– Guilherme Nascimento