• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Codeigniter as RestAPI (using Axios)

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.

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



Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams


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

I tryed to create a route to update with POST

PHP Code:

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

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:

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:

(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.

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]'


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



// 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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.