我是相当新的zend框架。我一直在尝试使用Zend_Rest_Controller编写RESTful控制器。我从一个很好的教程建立了一个 http://www.techchorus.net/create-restful-applications-using-zend-framework 它完美的工作。所以我继续将它添加到现有的zend应用程序中。我只想让一个控制器成为RESTful,所以在引导程序中进行了必要的更改。zend休息控制器路由到特定的控制器
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索引方法的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);
}
}
?>
如果您设置APPLICATION_ENV在你的'.htaccess'文件“发展”,其中Zend框架重写规则是,您应该看到错误的完整堆栈跟踪,而不是500内部服务器错误。您还可以检查公共目录中的Apache错误日志或error_log文件,以查看是否有任何错误信息输出。 – drew010 2012-02-25 00:28:29
@ drew010谢谢。只要我将其切换到开发状态,我认为我添加的代码或其他配置设置没有任何问题,失败是由于现有应用程序中的其他问题。是的,因为我学到了很多关于Zend_Rest_Controller的知识。现在完美。 – biker46s 2012-02-27 23:20:27