zend休息控制器路由到特定控制器

时间:2012-02-24 23:45:57

标签: zend-framework zend-route zend-rest zend-router zend-rest-route

我对zend框架很新。我一直在尝试使用Zend_Rest_Controller编写RESTful控制器。我从一个很好的教程中构建了一个 http://www.techchorus.net/create-restful-applications-using-zend-framework 它完美地运作。所以我继续将它添加到现有的zend应用程序中。我只是想让一个控制器成为RESTful,所以在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);
}

当我尝试使用url http://localhost/projectname/public/index.php/myservice访问服务时服务器抛出500内部服务器错误,该URL应该从 MyserviceController 调用索引方法。该应用程序具有以下文件夹结构

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

这是项目的 application.ini

[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

这是 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 个答案:

答案 0 :(得分:-1)

在“localhost / projectname / public / index.php / myservice”中为什么使用 index.php ,为什么不只是 / index /

ZF的默认网址结构是“ http:// hostname / controller / action / parametes