Skip to content

Commit 39a9794

Browse files
committed
Fix various files
1 parent 61d4471 commit 39a9794

File tree

20 files changed

+129
-131
lines changed

20 files changed

+129
-131
lines changed

user_guide_src/source/guides/api/code/001.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use App\Controllers\BaseController;
66
use CodeIgniter\API\ResponseTrait;
7+
use CodeIgniter\HTTP\ResponseInterface;
78

89
class Ping extends BaseController
910
{
1011
use ResponseTrait;
1112

12-
public function getIndex()
13+
public function getIndex(): ResponseInterface
1314
{
1415
return $this->respond(['status' => 'ok'], 200);
1516
}

user_guide_src/source/guides/api/code/002.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
use App\Controllers\BaseController;
66
use CodeIgniter\API\ResponseTrait;
7+
use CodeIgniter\HTTP\ResponseInterface;
78

89
class Ping extends BaseController
910
{
1011
use ResponseTrait;
1112

12-
protected $format = 'json';
13-
14-
public function getIndex()
13+
public function getIndex(): ResponseInterface
1514
{
1615
return $this->respond(['status' => 'ok'], 200);
1716
}

user_guide_src/source/guides/api/code/003.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use App\Controllers\BaseController;
66
use CodeIgniter\API\ResponseTrait;
77
use CodeIgniter\CodeIgniter;
8+
use CodeIgniter\HTTP\ResponseInterface;
89

910
class Ping extends BaseController
1011
{
1112
use ResponseTrait;
1213

13-
public function getIndex()
14+
public function getIndex(): ResponseInterface
1415
{
1516
return $this->respond([
1617
'status' => 'ok',

user_guide_src/source/guides/api/code/007.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class AuthorModel extends Model
88
{
9-
protected $table = 'authors';
9+
protected $table = 'author';
1010
protected $primaryKey = 'id';
1111
protected $allowedFields = ['name'];
1212
protected $useTimestamps = true;

user_guide_src/source/guides/api/code/008.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class BookModel extends Model
88
{
9-
protected $table = 'books';
9+
protected $table = 'book';
1010
protected $primaryKey = 'id';
1111
protected $allowedFields = ['title', 'author_id', 'year'];
1212
protected $useTimestamps = true;

user_guide_src/source/guides/api/code/009.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class Books extends BaseController
99
{
1010
use ResponseTrait;
1111

12-
protected $format = 'json';
13-
1412
/**
1513
* List one or many resources
1614
* GET /api/books

user_guide_src/source/guides/api/code/012.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
use App\Controllers\BaseController;
66
use App\Transformers\BookTransformer;
77
use CodeIgniter\Api\ResponseTrait;
8+
use CodeIgniter\HTTP\ResponseInterface;
89

910
class Books extends BaseController
1011
{
1112
use ResponseTrait;
1213

13-
protected $format = 'json';
14-
1514
/**
1615
* List one or many resources
1716
* GET /api/books
1817
* and
1918
* GET /api/books/{id}
2019
*/
21-
public function getIndex(?int $id = null)
20+
public function getIndex(?int $id = null): ResponseInterface
2221
{
2322
$model = model('BookModel');
2423
$transformer = new BookTransformer();
@@ -45,10 +44,9 @@ public function getIndex(?int $id = null)
4544
*
4645
* PUT /api/books/{id}
4746
*/
48-
public function putIndex(int $id)
47+
public function putIndex(int $id): ResponseInterface
4948
{
50-
$data = $this->request->getRawInput();
51-
$model = model('BookModel');
49+
$data = $this->request->getRawInput();
5250

5351
$rules = [
5452
'title' => 'required|string|max_length[255]',
@@ -78,7 +76,7 @@ public function putIndex(int $id)
7876
*
7977
* POST /api/books
8078
*/
81-
public function postIndex()
79+
public function postIndex(): ResponseInterface
8280
{
8381
$data = $this->request->getPost();
8482

@@ -105,7 +103,7 @@ public function postIndex()
105103
*
106104
* DELETE /api/books/{id}
107105
*/
108-
public function deleteIndex(int $id)
106+
public function deleteIndex(int $id): ResponseInterface
109107
{
110108
$model = model('BookModel');
111109

user_guide_src/source/guides/api/code/013.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66

77
class BookModel extends Model
88
{
9-
protected string $table = 'book';
10-
protected array $allowedFields = ['title', 'author_id', 'year'];
9+
protected $table = 'book';
10+
protected $allowedFields = ['title', 'author_id', 'year'];
1111

1212
/**
1313
* Include author_id and author_name
1414
* in the results.
1515
*/
1616
public function withAuthorInfo()
1717
{
18-
return $this->select('books.*, authors.id as author_id, authors.name as author_name')
19-
->join('authors', 'books.author_id = authors.id');
18+
return $this
19+
->select('book.*, author.id as author_id, author.name as author_name')
20+
->join('author', 'book.author_id = author.id');
2021
}
2122
}

user_guide_src/source/guides/api/code/014.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class BookModel extends Model
88
{
99
public function withAuthorInfo()
1010
{
11-
return $this->select('books.*, authors.name as author_name')
12-
->join('authors', 'books.author_id = authors.id');
11+
return $this
12+
->select('book.*, author.name as author_name')
13+
->join('author', 'book.author_id = author.id');
1314
}
1415
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Conclusion
22
==========
33

4-
This guide provided an overview of using CodeIgniter 4 to build a simple RESTful API for managing books and authors. You learned how to set up a CodeIgniter project, configure a database, create models, and implement API endpoints using controllers and auto-routing, along with the ResponseTrait for consistent API responses.
4+
This guide provided an overview of using CodeIgniter4 to build a simple RESTful API for managing books and authors. You learned how to set up a CodeIgniter project, configure a database, create models, and implement API endpoints using controllers and auto-routing, along with the ``ResponseTrait`` for consistent API responses.
55

66
From here, you can expand your API by adding more resources, implementing authentication and authorization using `CodeIgniter Shield <https://shield.codeigniter.com/>`__, and exploring advanced features like :doc:`../../libraries/throttler`, :doc:`../../incoming/controller_attributes`, and :doc:`../../general/caching`. CodeIgniter's flexibility and powerful features make it an excellent choice for building robust APIs.

0 commit comments

Comments
 (0)