Skip to content

Commit 26c666c

Browse files
lonnieezellpaulbalandan
authored andcommitted
Created a new Make an RESTful API guide
1 parent 1b377d1 commit 26c666c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+980
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Controllers\Api;
4+
5+
use App\Controllers\BaseController;
6+
use CodeIgniter\API\ResponseTrait;
7+
8+
class Ping extends BaseController
9+
{
10+
use ResponseTrait;
11+
12+
public function getIndex()
13+
{
14+
return $this->respond(['status' => 'ok'], 200);
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Controllers\Api;
4+
5+
use App\Controllers\BaseController;
6+
use CodeIgniter\API\ResponseTrait;
7+
8+
class Ping extends BaseController
9+
{
10+
use ResponseTrait;
11+
12+
protected $format = 'json';
13+
14+
public function getIndex()
15+
{
16+
return $this->respond(['status' => 'ok'], 200);
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Controllers\Api;
4+
5+
use App\Controllers\BaseController;
6+
use CodeIgniter\API\ResponseTrait;
7+
use CodeIgniter\CodeIgniter;
8+
9+
class Ping extends BaseController
10+
{
11+
use ResponseTrait;
12+
13+
public function getIndex()
14+
{
15+
return $this->respond([
16+
'status' => 'ok',
17+
'time' => date('c'),
18+
'version'=> CodeIgniter::CI_VERSION,
19+
]);
20+
}
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Database\Migrations;
4+
5+
use CodeIgniter\Database\Migration;
6+
7+
class CreateAuthorsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
$this->forge->addField([
12+
'id' => [
13+
'type' => 'INTEGER',
14+
'unsigned' => true,
15+
'auto_increment' => true,
16+
],
17+
'name' => [
18+
'type' => 'VARCHAR',
19+
'constraint' => '255',
20+
'null' => false,
21+
],
22+
'created_at' => [
23+
'type' => 'DATETIME',
24+
'null' => true,
25+
],
26+
'updated_at' => [
27+
'type' => 'DATETIME',
28+
'null' => true,
29+
],
30+
]);
31+
32+
$this->forge->addPrimaryKey('id');
33+
$this->forge->addUniqueKey('name');
34+
$this->forge->createTable('authors');
35+
}
36+
37+
public function down()
38+
{
39+
$this->forge->dropTable('authors');
40+
}
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Database\Migrations;
4+
5+
use CodeIgniter\Database\Migration;
6+
7+
class CreateBooksTable extends Migration
8+
{
9+
public function up()
10+
{
11+
$this->forge->addField([
12+
'id' => [
13+
'type' => 'INTEGER',
14+
'unsigned' => true,
15+
'auto_increment' => true,
16+
],
17+
'title' => [
18+
'type' => 'VARCHAR',
19+
'constraint' => '255',
20+
'null' => false,
21+
],
22+
'author_id' => [
23+
'type' => 'INTEGER',
24+
'unsigned' => true,
25+
'null' => false,
26+
],
27+
'year' => [
28+
'type' => 'INTEGER',
29+
'null' => true,
30+
],
31+
'created_at' => [
32+
'type' => 'DATETIME',
33+
'null' => true,
34+
],
35+
'updated_at' => [
36+
'type' => 'DATETIME',
37+
'null' => true,
38+
],
39+
]);
40+
41+
$this->forge->addPrimaryKey('id');
42+
$this->forge->addForeignKey('author_id', 'authors', 'id');
43+
$this->forge->createTable('books');
44+
}
45+
46+
public function down()
47+
{
48+
$this->forge->dropTable('books');
49+
}
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Database\Seeds;
4+
5+
use CodeIgniter\Database\Seeder;
6+
7+
class BookSeeder extends Seeder
8+
{
9+
public function run()
10+
{
11+
// Define author data and insert
12+
$authorData = [
13+
['name' => 'Frank Herbert'],
14+
['name' => 'William Gibson'],
15+
['name' => 'Ursula K. Le Guin'],
16+
];
17+
18+
$this->db->table('authors')->insertBatch($authorData);
19+
20+
// Get all inserted authors, keyed by name for easy lookup
21+
$authors = $this->db->table('authors')
22+
->get()
23+
->getResultArray();
24+
25+
$authorsByName = array_column($authors, 'id', 'name');
26+
27+
// Define books with author references
28+
$books = [
29+
[
30+
'title' => 'Dune',
31+
'author_id' => $authorsByName['Frank Herbert'],
32+
'year' => 1965,
33+
],
34+
[
35+
'title' => 'Neuromancer',
36+
'author_id' => $authorsByName['William Gibson'],
37+
'year' => 1984,
38+
],
39+
[
40+
'title' => 'The Left Hand of Darkness',
41+
'author_id' => $authorsByName['Ursula K. Le Guin'],
42+
'year' => 1969,
43+
],
44+
];
45+
46+
$this->db->table('books')->insertBatch($books);
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
7+
class AuthorModel extends Model
8+
{
9+
protected $table = 'authors';
10+
protected $primaryKey = 'id';
11+
protected $allowedFields = ['name'];
12+
protected $useTimestamps = true;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
7+
class BookModel extends Model
8+
{
9+
protected $table = 'books';
10+
protected $primaryKey = 'id';
11+
protected $allowedFields = ['title', 'author_id', 'year'];
12+
protected $useTimestamps = true;
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Controllers\Api;
4+
5+
use CodeIgniter\Api\ResponseTrait;
6+
use App\Controllers\BaseController;
7+
8+
class Books extends BaseController
9+
{
10+
use ResponseTrait;
11+
12+
protected $format = 'json';
13+
14+
/**
15+
* List one or many resources
16+
* GET /api/books
17+
* and
18+
* GET /api/books/{id}
19+
*/
20+
public function getIndex(?int $id = null)
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Update a book
27+
*
28+
* PUT /api/books/{id}
29+
*/
30+
public function putIndex(int $id)
31+
{
32+
//
33+
}
34+
35+
/**
36+
* Create a new book
37+
*
38+
* POST /api/books
39+
*/
40+
public function postIndex()
41+
{
42+
//
43+
}
44+
45+
/**
46+
* Delete a book
47+
*
48+
* DELETE /api/books/{id}
49+
*/
50+
public function deleteIndex(int $id)
51+
{
52+
//
53+
}
54+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Transformers;
4+
5+
use CodeIgniter\API\BaseTransformer;
6+
7+
class AuthorTransformer extends BaseTransformer
8+
{
9+
public function toArray(mixed $resource): array
10+
{
11+
return [
12+
'id' => $resource['id'],
13+
'name' => $resource['name'],
14+
];
15+
}
16+
}

0 commit comments

Comments
 (0)