Skip to content

Conversation

@lonnieezell
Copy link
Member

Description
This creates a new "Getting Started with RESTful APIs" guide that steps through using the auto-routing and ResponseTrait, plus the new transformers and API response pagination to create a simple API.

It moves the existing tutorial under a new "Guides" section.

@lonnieezell lonnieezell changed the title Created a new Make an RESTful API guide docs: Created a new Make an RESTful API guide Nov 21, 2025
Copy link
Contributor

@neznaika0 neznaika0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guides are a great idea. We can discuss on the forum what built-in mechanics are possible using the framework. I tried to tell you about the Validator, but you didn't approve.

I looked it up from phone.


.. code-block:: console
php spark make:controller Api/Books
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Book or Books? You use a different naming convention below the text.
It is recommended to name endpoints in the plural /books/123


class BookModel extends Model
{
protected string $table = 'books';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many people recommend naming tables in the singular: book, author

I can't check everything, but could it affect the internal automation? Is there a dependence on the ending "s"?

This creates a new file under ``app/Database/Migrations/``.

Edit the CreateAuthorsTable file to look like this:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file here is ``CreateAuthorsTable.php`` For visual perception.


Each author simply has a name for our purposes. We have made the name a uncommented unique key to prevent duplicates.

Now edit the CreateBooksTable file to look like this:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file here is ``CreateBooksTable.php`` For visual perception.

The "Improved" auto router is more secure and reliable than the legacy version, so it's recommended for all new projects.

Then, in ``app/Config/Routing.php`` confirm auto routing is **on**:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On? Maybe "enabled"?

Getting Started with REST APIs
##############################

.. content::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo .. contents::

@neznaika0
Copy link
Contributor

You have moved the pages to the directory. Some error correction paths need to be updated.
../outgoing/* >> ../../outgoing/*

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great - it's a really nice introduction to the topic and a solid guide overall.

As mentioned, there are a few issues with the links (since we reference the tutorial in several places), and some of the images appear to be broken.

The only thing I'm not happy with is the current navigation. Subpages aren't visible in the left menu when you enter a specific guide category, which makes it difficult to move between different parts of the guide. Setting 'navigation_depth': 3 in conf.py should fix this.

Other than that, everything looks very good.

@michalsn michalsn added documentation Pull requests for documentation only 4.7 labels Nov 30, 2025
public function putIndex(int $id)
{
$data = $this->request->getRawInput();
$model = model('BookModel');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$model = model('BookModel');

php spark make:controller Api/Books
This creates ``app/Controllers/Api/Books.php``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This creates ``app/Controllers/Api/Books.php``.
This creates **app/Controllers/Api/Books.php**.


.. tip::

If you prefer a different naming scheme, you would need to define routes explicitly in ``app/Config/Routes.php`` and turn auto-routing off.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you prefer a different naming scheme, you would need to define routes explicitly in ``app/Config/Routes.php`` and turn auto-routing off.
If you prefer a different naming scheme, you would need to define routes explicitly in **app/Config/Routes.php** and turn auto-routing off.

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.

Edit the Book transformer at ``app/Transformers/BookTransformer.php``. This one is a little more complex since it includes related author data:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Edit the Book transformer at ``app/Transformers/BookTransformer.php``. This one is a little more complex since it includes related author data:
Edit the Book transformer at **app/Transformers/BookTransformer.php**. This one is a little more complex since it includes related author data:

Implement the remaining methods
===============================

Edit ``app/Controllers/Api/Book.php`` to include the remaining methods:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Edit ``app/Controllers/Api/Book.php`` to include the remaining methods:
Edit **app/Controllers/Api/Book.php** to include the remaining methods:

database.default.hostname =
database.default.port =
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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.


- The app starts with ``php spark serve``
- ``CI_ENVIRONMENT`` is set to ``development`` in ``.env``
- ``writable/database.db`` exists and is writable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- ``writable/database.db`` exists and is writable
- **writable/database.db** exists and is writable

Guides
######

This section contains various guides to help you get started with CodeIgniter 4 in a project-based manner.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This section contains various guides to help you get started with CodeIgniter 4 in a project-based manner.
This section contains various guides to help you get started with CodeIgniter4 in a project-based manner.

Build a RESTful API
*******************

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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.

Comment on lines +21 to +23
*******
Guides
*******
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*******
Guides
*******
******
Guides
******

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.7 documentation Pull requests for documentation only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants