Проверка остальных API в codeigniter держать возврат false - PullRequest
0 голосов
/ 21 января 2020

Я выполняю проверку в моем put REST API, но почему-то он продолжает показывать false

<?php

use Restserver\Libraries\REST_Controller;

defined('BASEPATH') or exit('No direct script access allowed');

require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

class EventApi extends REST_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Model_basic');
        $this->load->library('form_validation');
        $this->load->helper('form');
    }

    public function Event_get()
    {
        $id = $this->get('id');
        if ($id === null) {
            $event = $this->Model_basic->select_all('px_events');
        } else {
            $event = $this->Model_basic->select_where('px_events', 'id', $id)->result_array();
        }

        if ($event) {
            $this->response([
                'status' => true,
                'message' => $event
            ], REST_Controller::HTTP_OK);
        } else {
            $this->response([
                'status' => False,
                'message' => 'Id Not Found'
            ], REST_Controller::HTTP_NOT_FOUND);
        }
    }

    public function Event_post()
    {
        //post
    }

    public function Event_put()
    {
        $id = $this->put('id');

        if($id === null){
            $this->response([
                'status' => False,
                'message' => 'Please Provide an Id'
            ], REST_Controller::HTTP_BAD_REQUEST);
        }elseif($id ===""){
            $this->response([
                'status' => False,
                'message' => 'Please Provide an Id'
            ], REST_Controller::HTTP_BAD_REQUEST);
        }else{

            $this->form_validation->set_rules('golf_course_id', 'golf_course_id', 'trim|required');
            $this->form_validation->set_rules('title', 'title', 'trim|required');
            $this->form_validation->set_rules('holes', 'holes', 'numeric|required');
            $this->form_validation->set_rules('prizel_pool', 'prize_pool', 'trim|required|numeric');

                $data = [
                    'golf_course_id'    => $this->put('golf_course_id'),
                    'title'             => $this->put('title'),
                    'date_start'        => $this->put('date_start'),
                    'date_end'          => $this->put('date_end'),
                    'holes'             => $this->put('holes'),
                    'prize_pool'        => $this->put('prize_pool'),
                    'date_created'      => $this->put('date_created'),
                    'date_modified'     => $this->put('date_modified')
                ];

            if($this->form_validation->run() === false) {
                $this->response([
                    'status' => False,
                    'message' => 'Please List The Field'
                ], REST_Controller::HTTP_BAD_REQUEST);
            }else{
                if ($this->Model_basic->update('px_events', $data, 'id', $id) > 0) {
                    $this->response([
                        'status' => True,
                        'message' => $data,
                    ], REST_Controller::HTTP_NO_CONTENT);
                } else {
                    $this->response([
                        'status' => false,
                        'message' => 'Update Fail',
                    ], REST_Controller::HTTP_BAD_REQUEST);
                }
            }
        }
    }

    public function Event_delete()
    {

       //Delete
}

1 Ответ

0 голосов
/ 21 января 2020

Проверка формы использует по умолчанию POST Данные. Вы должны заполнить его вашими PUT данными. Должно работать следующее:

public function Event_put()
{

    try
    {
        $this->form_validation->set_data($this->put());
        $this->form_validation->set_rules('id', 'Id', 'trim|required');
        $this->form_validation->set_rules('golf_course_id', 'golf_course_id', 'trim|required');
        $this->form_validation->set_rules('title', 'title', 'trim|required');
        $this->form_validation->set_rules('holes', 'holes', 'numeric|required');
        $this->form_validation->set_rules('prizel_pool', 'prize_pool', 'trim|required|numeric');

        if(!$this->form_validation->run()) throw new Exception(validation_errors());

        $data = [
            'golf_course_id'    => $this->put('golf_course_id'),
            'title'             => $this->put('title'),
            'date_start'        => $this->put('date_start'),
            'date_end'          => $this->put('date_end'),
            'holes'             => $this->put('holes'),
            'prize_pool'        => $this->put('prize_pool'),
            'date_created'      => $this->put('date_created'),
            'date_modified'     => $this->put('date_modified')
        ];

        if ($this->Model_basic->update('px_events', $data, 'id', $id) > 0) {
            $this->response([
                'status' => True,
                'message' => $data,
            ], REST_Controller::HTTP_NO_CONTENT);
        } else {
            throw new Exception('Update fail');
        }

    }
    catch(\Throwable $e)
    {
        $this->response([
            'status' => False,
            'message' => $e->getMessage(),
        ], REST_Controller::HTTP_BAD_REQUEST);

    }
}           
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...