Welcome Guest, Not a member yet? Register   Sign In
A Complete REST API Tutorial
#1

Hello, is there a fully completed example project or tutorial?

I'm trying to build REST API test in CI4. 

I've tried the steps described here : https://codeigniter4.github.io/userguide...stful.html 

But unfortunately when I try to reach controller, it's returning as an empty XML file. What do I missing? 

I was expecting a JSON file that contains data. 

I've never worked with APIs before. So I would be happy if you could explain it simply. 

I've used to following code: (I'm connected to database and I can see the raw data with "print_r")


Controller
PHP Code:
<?php 

namespace App\Controllers;

use 
CodeIgniter\RESTful\ResourceController;

class 
Categories extends ResourceController {

    public function 
__construct() {
        
$this->modelName 'App\Models\Categories';
    }

    public function 
index()    {
        
$this->respond($this->model->findAll(),200);
    }
    



Model
PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
Categories extends Model {
    protected 
$table 'categories';


Routes
PHP Code:
$routes->resource('categories'); 
Reply
#2

No full tutorial yet, sorry.
It's on my to-do list, though ... https://github.com/codeigniter4/CodeIgni...ssues/2186
Reply
#3

You need set JSON response in your request. From the browser it will always be XML.
You can JSON only leave in app/Config/Format.php, comment line 23 and 39:


PHP Code:
'application/xml'// machine-readable XML
'application/xml'  => \CodeIgniter\Format\XMLFormatter::class 
Reply
#4

See if this can be useful https://www.youtube.com/watch?v=419WfgND-c8&t=
Codeigniter 4 - Docker Image [github] [docker hub]
Reply
#5

Hi

Do we have any complete solution for Codeigniter 4 with REST API on github?
Reply
#6

(03-25-2020, 03:58 AM)myo Wrote: Hi

Do we have any complete solution for Codeigniter 4 with REST API on github?

Hello, you can use the cli-create plugin.

Using this plugin you will be able to create rest-ful api controller and any files in the command line.

After my testing, in the Codeigniter  current version v4.0.2 will be able to automatically create a functioning APIs.

Click-me to visit github.
Reply
#7

Maybe this can be helpful

https://github.com/anmar-dev/ci4_RESTful_API
I enjoy coding in codeigniter4. SurveyIgniter a project where i learn about codeigniter4.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB