我是相當新的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