4

I am fairly new to zend framework. I have been trying to write RESTful controller using Zend_Rest_Controller. I built one from a good tutorial http://www.techchorus.net/create-restful-applications-using-zend-framework It works perfectly. So I went ahead added it to the existing zend application. I just wanted one controller to be RESTful so did the necessary changes in the bootstrap.

protected function _initRestRoute()
{
    $this->bootstrap('frontController');
    $frontController = Zend_Controller_Front::getInstance();
    $restRoute = new Zend_Rest_Route($frontController, array() , array('default' =>                   array('MyserviceController')));
    $frontController->getRouter()->addRoute('rest', $restRoute);
}

the server throws up a 500 internal server error when i try to access the service using the url http://localhost/projectname/public/index.php/myservice which is supposed to invoke the index method from the MyserviceController. The application has the following folder structure

application/
    configs/
        application.ini
    controllers/
        IndexContoller
        OneController
        TwoController
        Myservice
    forms
    layouts
    models
    modules/
        app1module
        app2module
        app3module
     views
    Bootstrap.php   

this is the application.ini for the project

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/New_York"

includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

appnamespace = "Application"

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
includePaths.helpers = APPLICATION_PATH "/views/helpers"    

;Module support
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "default"
resources.modules[] = ""

resources.db.adapter = PDO_MYSQL
resources.db.params.host = ********
resources.db.params.username = *********
resources.db.params.password = *********
resources.db.params.dbname = *********

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1  
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

and this the code for MyserviceController.php

<?php
class MyserviceController extends Zend_Rest_Controller
{
    public function init()
    {
    parent::init();
        $this->_helper->viewRenderer->setNoRender(true);
    }

    public function indexAction() {
        $this->getResponse()->setBody('Hello World');
        $this->getResponse()->setHttpResponseCode(200);
    }

    public function getAction() {
    $this->getResponse()->setBody('Foo!');
        $this->getResponse()->setHttpResponseCode(200);
    }

    public function postAction() {
    $this->getResponse()->setBody('resource created');
        $this->getResponse()->setHttpResponseCode(200);
    }

    public function putAction() {
    $this->getResponse()->setBody('resource updated');
        $this->getResponse()->setHttpResponseCode(200);
    }

    public function deleteAction() {
    $this->getResponse()->setBody('resource deleted');
        $this->getResponse()->setHttpResponseCode(200);
    }
}
?>
biker46s
  • 43
  • 5
  • If you set your APPLICATION_ENV to "development" in your `.htaccess` file where the Zend Framework rewrite rules are, you should see full stack traces of the error instead of the 500 Internal Server Error. You could also check the Apache error log or the error_log file in your public directory to see if any error information was output there. – drew010 Feb 25 '12 at 00:28
  • @drew010 thank you. As soon as I switched it to development I figured there was nothing wrong with the code I added or other configuration settings, failure was due to other problems in the existing application. And yes due this I have learnt quite a lot about Zend_Rest_Controller which is a plus point. Now works perfect. – biker46s Feb 27 '12 at 23:20

1 Answers1

-1

In "localhost/projectname/public/index.php/myservice" why you using index.php, why not just /index/ ?

ZF's default URL structure is the "http://hostname/controller/action/parametes"

Filkor
  • 642
  • 6
  • 18
  • yes you are right the ZF's URL structure is "http://hostname/controller/action/parameters" but I can also instead explicitly mention my application entry point i.e. index.php and then the controller I want. – biker46s Feb 27 '12 at 23:16