samedi 9 mai 2015

Yii: REST api problems

i'm beginner in yii and in the Yii Application Development CookBook(2nd edition) on pages 105 to 113 when i'm running this codes, add button does not work, While everything is in accordance with the text of the book, where is problem? please help me. Some code easily run but others like this are not.

controller is:

<?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();
     }
}

?>

and view is:

<?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>

I want when click on the add button, task added.

Aucun commentaire:

Enregistrer un commentaire