我对 zend 框架相当陌生。我一直在尝试使用 Zend_Rest_Controller 编写 RESTful 控制器。我从一个很好的教程 http://www.techchorus.net/create-restful-applications-using-zend-framework构建了一个 它完美地工作。所以我继续将它添加到现有的 zend 应用程序中。我只希望一个控制器是 RESTful 的,所以在引导程序中进行了必要的更改。
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);
}
当我尝试使用应该从MyserviceController调用 index 方法的 url http://localhost/projectname/public/index.php/myservice访问服务时,服务器会引发 500 内部服务器错误。该应用程序具有以下文件夹结构
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);
}
}
?>