How to crete user using Yii2 Web Service

I am following the documentation.

I am trying to do POST request to my UserController that extends from ActiveController ( I want to create user ).

Using Postman extension for Google Chrome I fire this request:




POST /api/users?username=asd HTTP/1.1

Host: localhost

Authorization: Basic bmVuYWR6aXY6c2hvbmVaNDI0

Cache-Control: no-cache

Postman-Token: 4784adf7-5143-8ce5-b90f-0487eae9709f

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW



I get this response:




[

  {

    "field": "username",

    "message": "Username cannot be blank."

  }

]



Few questions:

  1. In my User model I have rules that specify that username can not be blank. But why is this rule fireing when I have sent something ?

  2. If I create actionCreate() method in my UserController, and I return right at the start of method just to test, I can see that nothing happens. Yii totally ignores my code. How I am supposed to create my custom code ? I do not get it what is going on here.

  3. If I extend UserController from yii\rest\Controller THEN I can see that my code in actionCreate is executing. But now comes my other troubles. How do I fire model validations and how to I return messages/errors as a response ? First how do I return status code ? And then, how to attach messages somewhere in the body ?

Is there any guide that explains how you can actually do these things ? Like how to implement fully functional endpoint using yii2. Because yii2 docs are NOT useful.

Thanks

  1. According to REST specification any insert must use POST, you are using post but you are passing the username with a get



POST /api/users?username=asd HTTP/1.1



thats why your model do not find it, pass it as post parameter.

  1. usually rest api controller are empty, or at least do not contain any code for create, update, delete view and index. These are are made based upon the request.

if you do a POST is a create, if you do a PUT is an update etc etc this is called HATEOAS, https://en.wikipedia.org/wiki/HATEOAS

api action are atomic ie to a POST correspond 1 insert and not multiple insert

Yii2 make this process automatically.

http://www.yiiframew…uick-start.html

Look at "Trying it Out" for a list with example of what HATEOAS is

  1. you do not need anything special beside making rules in your model and load your data in the model and save it.

If something goes wrong, ie the rule for the password length does not pass, the model during data validation calls




$this->addError($attribute, 'error message');



which is returned to the user, in case of rest api as json message.

In model you can make your own validation rule, ie a validation rule for a booking date (you can’t book in the past)




	public function rules() {

    	return [

        	[['room_id'], 'required'],

        	[['booking_date'], 'validateBookingDate'],

    	];

	}



where validateBookingDate is a method name which is specified in the model as well




	public function validateBookingDate($attribute, $params) {

    	if ($this->$attribute < date('Y-m-d H:i')) {

        	$this->addError($attribute, 'You can book only from ' . date('d/m/Y H:i'));

    	}

	}



  1. How ? I do not see any options in Postman to do so.

  2. UserController is empty but some code is still executing. When I create my own actionCreate() nothing happens. Yii ignores my code. And I want to do something with data before/after it hits model validations. When I extend from ActiveController, it seems like I do not have any control over what is happening in controller actions.

  3. If I decide to extend from Controller and not "Active" one, how do I trigger model validations, and create responses with status codes and validation errors if any ?

[color="#006400"]/* Moved from "General Discussions" to "REST APIs" */[/color]

  1. How ? I do not see any options in Postman to do so.

In postman there is the text area were you type the url, just on the left there is [GET] just click on it to open the menu to change the method, select [POST]

Then below there is one tab which name is Body, it should be the 3rd

There are some radio options form-data is fine then here you can add field name and value

  1. UserController is empty but some code is still executing. When I create my own actionCreate() nothing happens. Yii ignores my code. And I want to do something with data before/after it hits model validations. When I extend from ActiveController, it seems like I do not have any control over what is happening in controller actions.

Depend what do you want to do you can do such actions either in the model or in the controller

models has beforeValidate() and afterValidate()

to do it in controller look at the answer of this post

http://www.yiiframew…newest-records/

The question was different but the answer is the same, adapt the code to your need

  1. If I decide to extend from Controller and not "Active" one, how do I trigger model validations, and create responses with status codes and validation errors if any ?

Model Validation trigger automatically when you load data in the model