CodeIgniter Forums
Using Codeigniter as RestAPI (using Axios) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: Using Codeigniter as RestAPI (using Axios) (/showthread.php?tid=77662)



Using Codeigniter as RestAPI (using Axios) - shinseiten - 10-01-2020

Hello, I'm trying to use Codeigniter 4 as a RestAPI, but I'm encountering an high number of problems. Some of them are strangely omitted in the docs (and I can't search in the forum because there's a bug in the search at the moment).

I'm using Axios to contact the server.

1) How does validate() work? I can't find the details in the docs and I can't understand what data validates and if there's a way to submit custom data to validate (and I need this really hard)

2) I can't work with axios.put. For some reason it doesn't populate the $_POST correctly. 
 - If I send a JSON with 'Content-Type': 'application/json' , php://input has the JSON but $this->request->getRawInput() give me this: $varname[{"lorem":"ipsum"}] => null
 - If I send a FormData, I get this monstrosity: [------WebKitFormBoundarygAazo6HBmB477LPA Content-Disposition:_form-data;_name] => "lorem"

3) to do some tests with localhost to real server I had to add some extra junk code because header('Access-Control-Allow-Origin: *'); in the public/index.php doesn't work alone. I need to use $this->response->setHeader in the Control too.


P.S: My code is a total mess of tests, I don't really have something to show.


RE: Using Codeigniter as RestAPI (using Axios) - nc03061981 - 10-04-2020

I think, you only need using Post
-> in data post, you define
. type: in [get, insert, delete, update,...]
. Auth:
. Data:,...
...
And route to api/v12345
... and then in data post, get type and process...

https://codeigniter.com/user_guide/general/ajax.html

https://codeigniter.com/user_guide/incoming/incomingrequest.html


RE: Using Codeigniter as RestAPI (using Axios) - shinseiten - 10-12-2020

I don't understand what you've said sorry...

I tryed to create a route to update with POST

PHP Code:
$routes->post('events''Events::update/$1');
$routes->resource('events'); 

but for some reason this doesn't work.

So, still with axios.post, I used "_method: PUT" and it worked out...
but for some reason the validator fails because it doesn't see the data (even if the $_POST is populated, at this point I don't understand what data it checks).

I'm really frustrated with this codeigniter version, I'm finding it really difficult and buggy or with lacking documentation.
There are so many alias and different ways to do the same thing, but some are really strange or difficult to understand.

Like where it takes the data to test "$this->validate($rules)", I don't get it! To have some control I have to use this version: "$validation = \Config\Services::validation()", thats different but do the same thing (only with more control). It's all so strange...


RE: Using Codeigniter as RestAPI (using Axios) - connectgrid - 10-13-2020

Reading the documentation, $this->validate() will validate the input data based on the request type. So if you have a GET request, it will validate the $_GET data. If you have a POST (or PUT?) request, it will validate the $_POST data.

This seems to happen transparently. You simply use the $this->validate() call as a gateway to control the flow. You don't need to see the data at this point, only set up your validation rules that must pass.

Read about this here:
http://codeigniter.com/user_guide/incoming/controllers.html#validating-data

The boolean logic would be:
  • If validation passes, do something.
  • If validation fails, do something else.


If you need access to the request data after the validation runs, you can call $this->request->getPost(), for example.

Read about that here:
http://codeigniter.com/user_guide/incoming/incomingrequest.html#


RE: Using Codeigniter as RestAPI (using Axios) - shinseiten - 10-14-2020

(10-13-2020, 06:17 AM)connectgrid Wrote: [...]

Strange, I knew that it would target the $_POST and checked it (even tryed to manually populate it for testing purposes) but it wouldn't work. I'll check it again. Thank you!

Got a little frustrated there.


RE: Using Codeigniter as RestAPI (using Axios) - nicojmb - 10-14-2020

Hi, in my app i use this code:

PHP Code:
if ($this->request->getMethod() === 'patch')
  {

       $form $this->request->getPost();

       
$validation = \Config\Services::validation();

       
$rules = [
         
'password'     => "strong_password",
         
'pass_confirm' => 'matches[password]'
       
];

       
$validation->setRules($rules);

       if (
$validation->run($form) === FALSE)
       {

           return 
$this->failValidationError(json_encode($validation->getErrors()));

       }

       
// Omited code to save $model
        
       
return $this->respond(['status' => true'message' => $form]);
  } 


Check this article if it can help you, i need to add in my form to works.

https://codeigniter.com/user_guide/incoming/methodspoofing.html