samedi 27 juin 2015

How to use REST with MVC when there is no Database?

I have two servers, the first works using REST and other Webs pages that are built using MVC.

I usually always run REST commands in the Controller layer, however I am with a doubt, assuming that my project (using MVC) does not use database and I'm using Controllers only to send commands to the webservice to send and receive information.

  • The REST in this case would be equivalent to Model?

    In this case I should call the Rest within the Controllers and not create Models. eg .:

    public function createProductAction() {
        $rest = Rest('ws.example.com', 'PUT /items/', array(
                                            'price' => $price,
                                            'description' => $descricao
                                        ));
    
        if ($response->status === 200) {
            View::show('success.tpl');
        } else {
            View::show('error.tpl', $rest->error);
        }
    }
    
    

or

  • I would have to create Models to make the calls to REST?

    For example:

    class ProductsModel
    {
        public function putItem($preco, $descricao)
        {
            $rest = Rest('ws.example.com', 'PUT /items/', array(
                                                'price' => $price,
                                                'description' => $descricao
                                            ));
    
            //If status=200 new product is added
            return $response->status === 200;
        }
    
        public function deleteItem($id)
        {
            Rest('ws.example.com', 'DELETE /items/{id}', array(
                                                'id' => $id
                                            ));
    
            $response = json_decode($rest->getRespose());
    
            //If status=200 product is deleted
            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) {
                //If status=200 return data
                return $response->data;
            }
    
            return NULL;
        }
    }
    
    

How should I proceed?

Aucun commentaire:

Enregistrer un commentaire