title: RESTful routing in Zend 1.x
body: <p>I am new to Zend Framework and am trying to implement a RESTful controller in Zend and trying to write unit tests for my controller. I am following the REST API available at: <a href="https://github.com/codeinchaos/restful-zend-framework" rel="nofollow">https://github.com/codeinchaos/restful-zend-framework</a> but for every kind of test, PHPUnit always hits 404 (observable by asserting the response codes - the one I expect and the one that PHPUnit receives), thus failing the tests.</p>
  
  <p>I have added all code in respective files as this REST API's readme instructs:</p>
  
  <p>• I have added a module specific for REST: /application/modules/api.
  • In my application.ini, I have the following:</p>
  
  <pre><code>autoloaderNamespaces[] = "REST_"
  
  rest.default = "xml"
  rest.formats[] = "json"
  rest.formats[] = "xml"
  
  resources.frontController.plugins.ErrorHandler.class = "Zend_Controller_Plugin_ErrorHandler"
  resources.frontController.plugins.ErrorHandler.options.module = "default"
  resources.frontController.plugins.ErrorHandler.options.controller = "error"
  resources.frontController.plugins.ErrorHandler.options.action = "error"
  
  resources.router.routes.rest.type = Zend_Rest_Route
  </code></pre>
  
  <p>• In my application/bootstrap.php: </p>
  
  <pre><code>public function _initREST()
  {
      $frontController = Zend_Controller_Front::getInstance();
  
      // set custom request object
      $frontController-&gt;setRequest(new REST_Request);
      $frontController-&gt;setResponse(new REST_Response);
      $frontController-&gt;addModuleDirectory(APPLICATION_PATH . '/modules');
  
      // Add the REST route for the API module only
      $restRoute = new Zend_Rest_Route($frontController, array(), array('api'));
      $frontController-&gt;getRouter()-&gt;addRoute('rest', $restRoute);
  }
  </code></pre>
  
  <p>• In my /application/modules/api/Bootstrap.php:</p>
  
  <pre><code>public function _initREST()
      {
          $frontController = Zend_Controller_Front::getInstance();
  
          // Register the RestHandler plugin
          $frontController-&gt;registerPlugin(new REST_Controller_Plugin_RestHandler($frontController));
  
          // Add REST contextSwitch helper
          $contextSwitch = new REST_Controller_Action_Helper_ContextSwitch();
          Zend_Controller_Action_HelperBroker::addHelper($contextSwitch);
  
          // Add restContexts helper
          $restContexts = new REST_Controller_Action_Helper_RestContexts();
          Zend_Controller_Action_HelperBroker::addHelper($restContexts);
      }
  </code></pre>
  
  <p>• Out of my own experiments, I have also added a line $autoloader->registerNamespace('REST_') to my /public/index.php where $autoloader refers to an instance of Zend_Loader_Autoloader.</p>
  
  <p>• My test's location: /tests/application/modules/api/controllers/RestClassTest.php
  • This is my one of my tests for getAction(): </p>
  
  <pre><code>public function testGetAction()
  {
      $this-&gt;request-&gt;setMethod('GET');
      // Have tried other variations of URI too but all bear same result (getting 404) in tests indicating that routing is not set at the fundamental level. 
      $this-&gt;dispatch('/api/learn-to-rest?id=51fc287fc55ffc4006410bca');   
  
      $body = $this-&gt;getResponse()-&gt;getBody();
      $json = json_decode($body, true);
      $this-&gt;assertEquals('John Doe', $json['name']);    
  
      $httpResponse = $this-&gt;getResponse()-&gt;getHttpResponseCode();
      $this-&gt;assertEquals(200, $httpResponse);
  }
  </code></pre>
  
  <p>• And this is my getAction():</p>
  
  <pre><code>public function getAction() 
  {
      $request = $this-&gt;getRequest();
      $objectId = $request-&gt;getParam('id'); 
  
      // Validation for ID
      $this-&gt;_validateMongoObjectIdAction($objectId);
  
      // Perform DB record selection
      $records = new Db_Collection_Class(new Db_Mongo_Class);
      $result = $records-&gt;findInCollection(array('_id' =&gt; new MongoId($objectId)));     
      if ($result === null) {
          // Source: http://www.w3.org/Protocols/HTTP/HTRESP.html
          $this-&gt;getResponse()-&gt;setHttpResponseCode(404);
      }
  
      // Dispatch records in JSON format
      $this-&gt;_helper-&gt;json($result);
  }
  </code></pre>
  
  <p>It is true that I am setting a 404 for no-result-found situation but confining specifically at getAction, it is hitting 404 with test for a recognized valid ID as well.</p>
  
  <p>I checked numerous resources (including the example provided by this API and Matthew O'Phinney's post: <a href="http://www.mwop.net/blog/228-Building-RESTful-Services-with-Zend-Framework.html" rel="nofollow">http://www.mwop.net/blog/228-Building-RESTful-Services-with-Zend-Framework.html</a>) and all instruct to follow the same things that I am already doing. It is evident that I am missing something they are telling or am not comprehending some factor. I'll be obliged if anyone guides me where I'm doing what wrong. </p>
  
  <p>Thanks in advance.</p>