1

I have build a Rest webservice that has a mvc structure, but I need some suggestion on how to make it better.

What I have right know is :

all the request come to the front controller (index.php)

  $request = new Request();  //parse all the elements of the request

// spl_api_autoload takes care for loading the classes

  $controller_name = ucfirst($request->url_elements[1]) . 'Controller';

  if (class_exists($controller_name)) {
    $controller = new $controller_name();
    $action_name = strtolower($request->verb) . 'Action';
     $result = $controller->$action_name($request);

  }  // this acts like a sort of router 

The url looks like this:

index.php/tree/addnode/4/testnode

tree -> Resource

addnode -> action

4 -> parent id

testnode -> node name

What I need a suggestion is in this part down here (controller) :

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

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

      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;
      } 

   } 

I want to skip the switch cases , Is there anyway I can do this ?

Thank you very very much for your time

Klidi Spiro
  • 49
  • 1
  • 11
  • 2
    What you describe is not actually Restful API, though. Restful API rely strictly on HTTP architect to perform operation. You can refer it here: `http://stackoverflow.com/questions/671118/what-exactly-is-restful-programming` Also, please don't invent another framework, what you actually want to implement a web service are already out there with much better improvement and document so it's always better to look out for a solution first. – Dat Pham May 20 '16 at 10:29
  • Thank you for taking time to answer . First i didn't invent it Lorna Jane did, 2nd it makes more sense to me to be like a mvc - the view just encodes and echos the response no template rendering or other things.It responds in Json , XML or Html depending on the req ... so whats wrong with this ? where do u see the problem with it ? Thank you very much !!! P.s I did the research , looked at slim frameworks and others like slim but for learning i thought this example was better!!! – Klidi Spiro May 20 '16 at 10:54

1 Answers1

0

When doing something like this I used the html methods like so

$app = App::init();
try{
   //Pass the information here so data can be handled
   //This makes it easier for other people who are new to oop, but not really correct
   $app->$method($target, $data)
}catch(Exception e){ //handle error }

App Class
public function get($target, $data); get resource from db
public function post($target, $data); post resource to db 
etc.

Sample of post
function post($target, $data)
{
     $control = new $target();
     return $control->handle($data);


}
TheStart101
  • 76
  • 1
  • 10