You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/guides/api/controller.rst
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Run the Spark command:
20
20
21
21
php spark make:controller Api/Books
22
22
23
-
This creates ``app/Controllers/Api/Books.php``.
23
+
This creates **app/Controllers/Api/Books.php**.
24
24
25
25
Open it and replace its contents with the following stubbed out class:
26
26
@@ -30,7 +30,7 @@ Since we're using auto-routing, we need to use the ``index`` method names so it
30
30
31
31
.. tip::
32
32
33
-
If you prefer a different naming scheme, you would need to define routes explicitly in ``app/Config/Routes.php`` and turn auto-routing off.
33
+
If you prefer a different naming scheme, you would need to define routes explicitly in **app/Config/Routes.php** and turn auto-routing off.
34
34
35
35
API Transformers
36
36
=================
@@ -45,7 +45,7 @@ Create the transformers with the generator command:
45
45
46
46
The transormer requires a single method, ``toArray`` to be present and accept a mixed data type called ``$resource``. This method is responsible for transforming the resource into an array format suitable for API responses. The returned array is what is then encoded as JSON or XML for the API response.
47
47
48
-
Edit the Book transformer at ``app/Transformers/BookTransformer.php``. This one is a little more complex since it includes related author data:
48
+
Edit the Book transformer at **app/Transformers/BookTransformer.php**. This one is a little more complex since it includes related author data:
49
49
50
50
.. literalinclude:: code/011.php
51
51
@@ -133,7 +133,7 @@ If you see JSON data from your seeder, congratulations—your API is live!
133
133
Implement the remaining methods
134
134
===============================
135
135
136
-
Edit ``app/Controllers/Api/Book.php`` to include the remaining methods:
136
+
Edit **app/Controllers/Api/Book.php** to include the remaining methods:
137
137
138
138
.. literalinclude:: code/012.php
139
139
@@ -142,13 +142,13 @@ Each method uses helpers from :php:trait:`ResponseTrait` to send proper HTTP sta
142
142
And that's it! You now have a fully functional RESTful API for managing books, complete with proper HTTP methods, status codes, and data transformation. You can further enhance this API by adding authentication, validation, and other features as needed.
143
143
144
144
A More Semantic Name scheme
145
-
============================
145
+
===========================
146
146
147
147
In the previous examples, we used method names like ``getIndex``, ``putIndex``, etc because we wanted to solely rely on the HTTP verb to determine the action. With auto-routing enabled, we have to use the ``index`` method name to avoid conflicts with URI segments. However, if you prefer more semantic method names, you could change the method names so that they reflect the action being performed, such as ``getList``, ``postCreate``, ``putUpdate``, and ``deleteDelete``. This would then make each method's purpose clearer at a glance. And would just add one new segment to the URI.
Copy file name to clipboardExpand all lines: user_guide_src/source/guides/api/database-setup.rst
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
.. _ci47-rest-part3:
2
2
3
3
Creating the Database and Model
4
-
################################
4
+
###############################
5
5
6
6
.. contents::
7
7
:local:
@@ -21,15 +21,15 @@ Run the Spark command:
21
21
php spark make:migration CreateAuthorsTable
22
22
php spark make:migration CreateBooksTable
23
23
24
-
This creates a new file under ``app/Database/Migrations/``.
24
+
This creates a new file under **app/Database/Migrations/**.
25
25
26
26
Edit the **CreateAuthorsTable.php** file to look like this:
27
27
28
28
.. literalinclude:: code/004.php
29
29
30
30
Each author simply has a name for our purposes. We have made the name a uncommented unique key to prevent duplicates.
31
31
32
-
Now edit the CreateBooksTable file to look like this:
32
+
Now, edit the **CreateBooksTable.php** file to look like this:
33
33
34
34
.. literalinclude:: code/005.php
35
35
@@ -44,7 +44,7 @@ Now run the migration:
44
44
Now the database has the necessary structure to hold our books and authors.
45
45
46
46
Create a seeder
47
-
================
47
+
===============
48
48
49
49
Seeders let you load sample data for development so you have something to work with right away. In this case, we’ll create a seeder to add some example books and their authors.
50
50
@@ -54,7 +54,7 @@ Run:
54
54
55
55
php spark make:seeder BookSeeder
56
56
57
-
Edit the file at ``app/Database/Seeds/BookSeeder.php``:
57
+
Edit the file at **app/Database/Seeds/BookSeeder.php**:
Copy file name to clipboardExpand all lines: user_guide_src/source/guides/api/first-endpoint.rst
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ In this section, we will enable CodeIgniter's *Improved Auto Routing* feature an
12
12
Why Auto-Routing?
13
13
==================
14
14
15
-
The previous tutorial introduced you to defined routes manually in ``app/Config/Routes.php``. While this is a powerful and flexible way to define your application's routing, it can be tedious for building RESTful APIs where you may have many endpoints that follow a common pattern. Auto-Routing simplifies this by automatically mapping URL patterns to controller classes and methods based on conventions, and the focus on HTTP verbs works quite well for RESTful APIs.
15
+
The previous tutorial introduced you to defined routes manually in **app/Config/Routes.php**. While this is a powerful and flexible way to define your application's routing, it can be tedious for building RESTful APIs where you may have many endpoints that follow a common pattern. Auto-Routing simplifies this by automatically mapping URL patterns to controller classes and methods based on conventions, and the focus on HTTP verbs works quite well for RESTful APIs.
16
16
17
17
Enable Improved Auto Routing
18
18
============================
@@ -36,15 +36,15 @@ Then, in **app/Config/Routing.php** confirm auto-routing is **enabled**:
36
36
That's all you need for CodeIgniter to automatically map your controller classes and to URIs like ``GET /api/ping`` or ``POST /api/ping``.
37
37
38
38
Create a Ping controller
39
-
=========================
39
+
========================
40
40
41
41
To understand how a basic API endpoint works, let's generate a controller to serve as our first API endpoint. This will provide a simple "ping" response to confirm our setup is correct.
42
42
43
43
.. code-block:: console
44
44
45
45
php spark make:controller Api/Ping
46
46
47
-
This creates ``app/Controllers/Api/Ping.php``.
47
+
This creates **app/Controllers/Api/Ping.php**.
48
48
49
49
Edit the file so it looks like this:
50
50
@@ -100,7 +100,7 @@ Here's how other verbs would map if you added them later:
By default, CodeIgniter uses the :php:class:`CodeIgniter\\Format\\Format` class to automatically negotiate the response format. It can return responses in either JSON or XML depending on what the client requests.
Copy file name to clipboardExpand all lines: user_guide_src/source/guides/api/index.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ Getting Started with REST APIs
7
7
:local:
8
8
:depth: 2
9
9
10
-
This tutorial will guide you through building a simple RESTful API to manage books using Codeigniter 4. Along the way, you'll leanr the basics of setting up a CodeIgniter project, configuring a database, and creating API endpoints, as well as understand what makes a RESTful API.
10
+
This tutorial will guide you through building a simple RESTful API to manage books using Codeigniter4. Along the way, you'll leanr the basics of setting up a CodeIgniter project, configuring a database, and creating API endpoints, as well as understand what makes a RESTful API.
11
11
12
12
This tutorial will primarily focus on:
13
13
@@ -85,7 +85,7 @@ Open ``.env`` and **uncomment** the database section, then set:
85
85
database.default.hostname =
86
86
database.default.port =
87
87
88
-
CodeIgniter will automatically create the SQLite database file if it doesn't exist, but you need to ensure that the ``writable/`` directory is writable by the web server.
88
+
CodeIgniter will automatically create the SQLite database file if it doesn't exist, but you need to ensure that the **writable/** directory is writable by the web server.
89
89
90
90
.. warning::
91
91
@@ -96,7 +96,7 @@ At this point, you should have a working CodeIgniter 4 project with SQLite confi
96
96
97
97
- The app starts with ``php spark serve``
98
98
- ``CI_ENVIRONMENT`` is set to ``development`` in ``.env``
Copy file name to clipboardExpand all lines: user_guide_src/source/guides/index.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
Guides
3
3
######
4
4
5
-
This section contains various guides to help you get started with CodeIgniter 4 in a project-based manner.
5
+
This section contains various guides to help you get started with CodeIgniter4 in a project-based manner.
6
6
7
7
.. toctree::
8
8
:titlesonly:
@@ -20,4 +20,4 @@ This project walks you through building a basic news application. You will begin
20
20
Build a RESTful API
21
21
*******************
22
22
23
-
This guide will help you build a simple RESTful API using CodeIgniter 4. You will learn how to use auto-routing, setup controllers, and handle requests and responses in a RESTful manner. By the end of this guide, you will have a functional API that can perform CRUD operations on a resource. This will introduce you to the basic concepts of a RESTful API and the tools that CodeIgniter provides to facilitate its development.
23
+
This guide will help you build a simple RESTful API using CodeIgniter4. You will learn how to use auto-routing, setup controllers, and handle requests and responses in a RESTful manner. By the end of this guide, you will have a functional API that can perform CRUD operations on a resource. This will introduce you to the basic concepts of a RESTful API and the tools that CodeIgniter provides to facilitate its development.
0 commit comments