-
Notifications
You must be signed in to change notification settings - Fork 2k
docs: Created a new Make an RESTful API guide #9808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.7
Are you sure you want to change the base?
Conversation
neznaika0
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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**: |
There was a problem hiding this comment.
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:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo .. contents::
|
You have moved the pages to the directory. Some error correction paths need to be updated. |
michalsn
left a comment
There was a problem hiding this 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.
| public function putIndex(int $id) | ||
| { | ||
| $data = $this->request->getRawInput(); | ||
| $model = model('BookModel'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $model = model('BookModel'); |
| php spark make:controller Api/Books | ||
| This creates ``app/Controllers/Api/Books.php``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - ``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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
| ******* | ||
| Guides | ||
| ******* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ******* | |
| Guides | |
| ******* | |
| ****** | |
| Guides | |
| ****** |
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.