I am creating the REST API using codeigniter. I downloaded this library and installed in my application. It works great! https://github.com/chriskacerguis/codeigniter-restserver
But I have an doubt. As per this library. I have 4 functoins
- GET
- POST
- PUT
- DELETE
I understand the functionality of each POST method but what I concern is how to write the validation and other things in the REST API.
Example: I have created the controller User.php
class User extends REST_Controller {
public function __construct()
{
parent::__construct();
}
// Get multile / individual users records and perform search also.
public function index_get()
{
$this->response('get / search users', REST_Controller::HTTP_OK);
}
// User Registration / Login
public function index_post()
{
echo 'create';
}
// User Information Update
public function index_put()
{
echo 'update';
}
// User Delete
public function index_delete()
{
echo 'delete';
}
}
I tested in postman post method works fine.
Here is my question. I have the following functionality for user.
- Register
- Login
- Forgot Password
- Change Password
- Update Profile
I think except Register other functions are posted with PUT method? In the PUT / POST method do I have to write all validations and database operation buy identity the post method variable using action likes register, login, forgot-password etc.,
In admin I want to get active or inactive users, find by email or name. this kind of operations where we have to write?
Thanks to all.
1 Answer 1
You can do all the basic validation on client-side, and if you need something to validate on server-side from the database you can manage the same in rest API.
- Register - this should be done on client-side
- Login - This can be done at server side by reading post parameter and validating same from the database.
- Change Password - Here again, password policy can be validated and client-side and other validation like do no use same password can be managed at server side
- Update Profile - Client-side validation
For writing server-side validation
class User extends REST_Controller {
public function __construct()
{
parent::__construct();
}// User Registration / Login
public function index_post()
{
$username = $this->input->post('username');
$password = $this->security->xss_clean($this->input->post("password"));
$message = $this->Login_model->check_login($username,$password);
$this->response($message);
}
}