Create mind-blowing REST APIs with CodeIgniter 4 using Shield authentication! In this tutorial, we’ll tackle the add new project API. After nailing user authentication APIs, we dive into project development. By decoding user input, we ensure data security and seamless API functionality. Cheers to successful project creation! π #CodeIgniter #RESTAPIs #ShieldAuthentication
Table of Contents
ToggleIntroduction
In this video, we will create the "Add New Project" API, continuing our development of RESTful APIs using Shield authentication. We have previously completed the user authentication APIs for registration, login, profile, and log out. Now, we will focus on developing the API for the "Project Controller".
Development Process
To create the "Add New Project" API, we will work on the "add project" method inside the "project controller.php" file. This method will receive project data as well as the authorization token value inside the header.
Data Requirements
For creating a new project, we need data for user ID, name, and budget. The user ID value will be obtained from the authorization token, while the project name and budget will be obtained as form data.
API Development
We start by defining the validation rules for creating a project. All the fields are required, so the validation rules for "name" and "budget" will be "required".
Field | Validation Rule |
---|---|
Name | Required |
Budget | Required |
If the validation fails, we prepare the response with status as false, an error message, and the validation errors. If the validation passes, we proceed to interact with the project table using the model.
Inserting Project Data
We retrieve the user ID from the authorization token using the "or" helper function and create a project object to interact with the project table. The project data is prepared with the user ID, name, and budget, and then inserted into the project table.
Access Control
The "Add New Project" API is a protected route that requires the authorization token to be passed inside the header.
Testing the API
We test the API using Postman, passing the authorization token in the header and the project name and budget in the body. We receive appropriate responses for valid and invalid inputs, confirming that the API is functioning correctly.
Successful Creation
Upon successful creation, the project data is saved in the project table.
Conclusion
In this session, we have successfully developed the "Add New Project" API and tested its functionality using Postman. This API is now ready for use in our CodeIgniter 4 RESTful application. Thank you for watching, and stay tuned for the next video on listing user-wise projects.
Related posts:
- “How to utilize CancellationToken in ASP.NET C# API for better control over asynchronous operations?”
- Deploying a backend on Vercel for APIs and Functions.
- Discover Nuxt.js from the Beginning Part Two.
- I created a feature from scratch in an internal project using Next.js and OpenAI.
- Comparison between Xiaomi HyperOS and Google Android 14
- The insider story of FrankenPHP: Could it be a game-changer for PHP?