zend rest controller routing to specific controller

2k Views Asked by At

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);
    }
}
?>
1

There are 1 best solutions below

1
Filkor On

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"