• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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=
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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