mercredi 11 mai 2016

Proxy pattern in Rest web service

I have build a web service that has a MVC structure and I am asked to add a proxy pattern and a repository pattern to it.

My questions are : 1.Is it possible to add a proxy pattern to a Restful web service , if so , where should I place it.

2.How can I save Proxy objects so the next time the same request comes i don't have to go to persistence.

What i have right now is: Client->Request processing->Controller -> Model.

Controller -> View -> Client.

Controller (all controllers are in one folder, one separated controller for each resource , required depending on the request by an api outoload function

(All Classes folder, organized in files for each resource , required depending on the request by an api autoload function.This Classes retrive from database and create objects from raw data )

This is how my Rest web service looks right now :

All the requests come to index.php.

index.php file :

$request = new Request();

request.php file:

class Request {
 public $url_elements;
 public $verb;
 public $parameters;
 public $headers;

 public function __construct() {
    $this->verb = $_SERVER['REQUEST_METHOD'];
    $this->url_elements = explode('/', $_SERVER['PATH_INFO']);

    $this->headers = apache_request_headers();



    $this->parseIncomingParams();

    $this->format = 'json';
    if(isset($this->parameters['format'])) {
        $this->format = $this->parameters['format'];
    }
    return true;
}


 public function parseIncomingParams() {
....... etc.

resourceController.php file:

class TreeController {



public function postAction($request) {


   $data = $request->parameters;



   if(isset($request->url_elements[2])) {

    switch ($request->url_elements[2]) {
      case 'addroot':

      $data['message'] = "This data was submitted";

      if(isset($request->url_elements[3])) {


        $name = $request->url_elements[3];


        $nested = new NestedSetClass();

        $nested->createRootNode($name);


      }
      return $data;

      break;

      case 'addnode':


     if(isset($request->url_elements[3])) {

      if(isset($request->url_elements[4])) { 

        $parent = $request->url_elements[3];

        $name = $request->url_elements[4];


      $nested = new NestedSetClass();

      $nested->insertChildNode($name,$parent);
       }
      } 
      break;
      default:
            # code...
            break;
      } 

   }


 }

 public function getAction($request) {


    $data = $request->parameters;


         if(isset($request->url_elements[2])) {

           switch ($request->url_elements[2]) {

            case 'path':

             if(isset($request->url_elements[3])) {


              $id = (int)$request->url_elements[3];

              $nested = new NestedSetClass();
              $content = $nested->getPath($id);

              $api = new JsonView();  

              $api->render($content); 


              }
              break;

            case 'depth';

and so on ...

What i was thinking was instead of Classes(Model) that i have right now place a repository that also has a gateway and a factory.The repository would implement the same interface as the proxy.

Client->Request processing->Controller -> Proxy (if Null) -> Repository

Repository->Gateway->Persitance

Repository->Factory (before the object is retured to the controller the repository will save it)

Controller->View->Client.

Thank you very much for your time , I am new to this things and maybe I got it all wrong!!!!

Aucun commentaire:

Enregistrer un commentaire