Php Yii:restapi不工作

Php Yii:restapi不工作,php,windows,yii,Php,Windows,Yii,我是yii的初学者,在第105至113页的yii应用程序开发手册(第二版)中,当我运行此代码时,“添加”按钮不起作用,而所有内容都与本书的文本一致,问题在哪里?请帮帮我。 有些代码很容易运行,但像这样的其他代码则不然 控制器为: <?php class TodoController extends Controller { public function actionIndex() { $task = new Task(); $th

我是yii的初学者,在第105至113页的yii应用程序开发手册(第二版)中,当我运行此代码时,“添加”按钮不起作用,而所有内容都与本书的文本一致,问题在哪里?请帮帮我。 有些代码很容易运行,但像这样的其他代码则不然

控制器为:

<?php

class TodoController extends Controller
{
     public function actionIndex()
     {
         $task = new Task();
         $this->render('index', array(
         'task' => $task,
         ));
     }

     public function actionTask()
     {
         $req = Yii::app()->request;
            if($req->isPostRequest) {
                $this->handlePost($req->getPost('id'), 
                $req->getPost('Task'));
            }
            elseif($req->isPutRequest) {
                $this->handlePut($req->getPut('Task'));

            }
            elseif($req->isDeleteRequest) {
                $this->handleDelete($req->getDelete('id'));
            }
            else {
             $this->handleGet($req->getParam('id'));
            }
     }

     private function handleGet($id)
     {
         if($id) {
             $task = $this->loadModel($id);
             $this->sendResponse($task->attributes);
         }
         else {
             $data = array();
             $tasks = Task::model()->findAll(array('order' => 'id'));
             foreach($tasks as $task) {
                 $data[] = $task->attributes;
             }
            $this->sendResponse($data);
         }
     }

     private function handlePut($data)
     {
         $task = new Task();
         $this->saveTask($task, $data);
     }

     private function handlePost($id, $data)
     {
         $task = $this->loadModel($id);
         $this->saveTask($task, $data);
     }

     private function saveTask($task, $data)
     {
         if(!is_array($data)){
             $this->sendResponse(array(), 400, array('No data
                provided.'));
         }
         // $task->setAttributes($data);
         $task->attributes = $data;
         if($task->save()) {
            $this->sendResponse($task->attributes);
         } else {
            $errors = array();
                foreach($task->errors as $fieldErrors) {
                    foreach($fieldErrors as $error) {
                        $errors[] = $error;
                    }
                }
            $this->sendResponse(array(), 400, $errors);
         }
     }

     private function handleDelete($id)
     {
         $task = $this->loadModel($id);
         if($task->delete()) {
             $this->sendResponse('OK');
         }
         else {
             $this->sendResponse(array(), 500, array('Unable to
             delete task.'));
         }
     }

     private function loadModel($id)
     {
         $task = Task::model()->findByPk($id);
         if(!$task) {
             $this->sendResponse(array(), 404, array('Task not
             found.'));
         }
         return $task;
     }

     private function sendResponse($data, $responseCode = 200,
    $errors = array())
     {
         $messages = array(
         200 => 'OK',
         400 => 'Bad Request',
         404 => 'Not Found',
         500 => 'Internal Server Error',
         );
         if(in_array($responseCode, array_keys($messages))) {
             header("HTTP/1.0 $responseCode ".$messages[$responseCode],
            true, $responseCode);
         }
         echo json_encode(array(
             'errors' => $errors,
             'data' => $data,
             ));
         Yii::app()->end();
     }
}

?>

这一观点是:

<?php
Yii::app()->clientScript->registerPackage('todo');
    $options = json_encode(array(
    'taskEndpoint' => $this->createUrl('todo/task'),
));
Yii::app()->clientScript->registerScript('todo', "todo.
init($options);", CClientScript::POS_READY);
?>
<div class="todo-index">
    <div class="status"></div>
    <div class="tasks"></div>
    <div class="new-task">
    <?php echo CHtml::beginForm()?>
    <?php echo CHtml::activeTextField($task, 'title')?>
    <?php echo CHtml::submitButton('Add')?>
    <?php echo CHtml::endForm()?>
    </div>
</div>
<script id="template-task" type="text/x-dot-template">
    <div class="task{{? it.done==1}} done{{?}}" data-id="{{!it.
id}}">
    <input type="checkbox"{{? it.done==1}}checked {{?}}/>
    <input type="text" value="{{!it.title}}" />
    <a href="#delete" class="delete">Remove</a>
    </div>
</script>


我想在单击添加按钮时,添加任务

你能添加对你不起作用的特定代码,并告诉我们你希望它如何工作吗?布莱斯先生,你能帮我吗?你能比添加按钮“不起作用”更具体一些吗?它应该做什么?
生成了什么HTML输出?很抱歉,如果您测试此代码并使用gii创建任务表,您将看到“添加”按钮不起作用,即当您键入名称并单击“添加”按钮时,任务将不会被添加。