aslamdoctor
aslamdoctor

Reputation: 3935

How to read PUT and DELETE request using codeigniter framework?

I am creating an API server using codeigniter framework and I was wondering if it is possible to read PUT and DELETE requests from any client side form submissions?

The user guide doesn't explain much regarding this.

Upvotes: 1

Views: 1142

Answers (4)

edgarbrignoni
edgarbrignoni

Reputation: 1

Codeigniter 3

If you want to utilize the PUT, DELETE, PATCH or other exotic request methods, they can only be accessed via a special input stream, that can only be read once.

See documentation...

https://codeigniter.com/userguide3/libraries/input.html#using-the-php-input-stream

Make sure your request has...

Content-Type: application/x-www-form-urlencoded

Also, if sending inputs you can grab them via input_stream() method like this...

$this->input->input_stream('user_id');

(i.e. change user_id to which ever input name you are using on form)

Upvotes: 0

Faysal Hafidi
Faysal Hafidi

Reputation: 21

In codeigniter you can get values of PUT or DELETE with this : $this->input->raw_input_stream; and you can split the string url into array and put the values of array into object with this methode set_array() you must to create this method in model to set each value to right attribute and you can use this code by customize it with your project :

        $method = $this->input->server('REQUEST_METHOD');
    if($method === 'PUT')
    {
        $this->input->raw_input_stream;

        $input_data = $this->input->raw_input_stream;
        
        $exploded = array();
        parse_str($input_data, $exploded);
        $daman = new damancom_model();
        $daman->set_array($exploded);
        $daman->ID = $exploded["ID"];
        $daman->edit_damancom();
    }

Upvotes: 0

munsifali
munsifali

Reputation: 1733

The HTML standard does not support PUT inside the <form method=""> attribute and also browser doesn't support it. Browsers don't generally support methods other than GET and POST in HTML forms. if you put anything else other than POST or GET it should be sent as a GET request according to specification.

Use hidden method field in your form and put the actual HTTP.Use this library

Upvotes: 1

Archangels
Archangels

Reputation: 87

You can try to detect the method type first and seperate the different cases:

switch($_SERVER['REQUEST_METHOD']){
   case 'GET':

      ...
      break;
   case 'POST':

      ...
      break;
   case 'PUT':

      ...
      break;
   case 'DELETE':

      ...
      break;
   default:
      echo "Unknown Request.";
}

Upvotes: 1

Related Questions