RestFul API field with anonymous function , how to pass function parameters

from here:

we know that for Yii 2.0 restful API resources has fields, which can be represented by anonymous functions.

For example: ‘name’ in the following official example.

// explicitly list every field, best used when you want to make sure the changes

// in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).

public function fields()


    return [

        // field name is the same as the attribute name


        // field name is "email", the corresponding attribute name is "email_address"

        'email' => 'email_address',

        // field name is "name", its value is defined by a PHP callback

        'name' => function ($model) {    <=================== here, the parameter  $model for fields 'name'

            return $model->first_name . ' ' . $model->last_name;




my question is when using this field, how to pass function parameters (i.e. $model) into the function?



$object->name($the_model) ??

Thanks in advance

A late update, to answer it for other people who might also wondering

the anonymous function has pre-defined signature


  mixed function (ActiveRecord $model, String $field) {


$model is an instance of ActiveRecord associated with ActiveController, as specified by $modelClass

$field is the name of this special field, in this case it is ‘name’

class UserController extends \yii\rest\ActiveController


	public $modelClass = 'app\models\Users';    <============= here is class definition


the call stack is as follows:

	app\models\{closure}(): /yii2-basic/models/Users.php at line 57	<====== break point at the anonymous function in question

	toArray(): /yii2-basic/vendor/yiisoft/yii2/base/ArrayableTrait.php at line 120	  <====== shown below

	serializeModels(): /yii2-basic/vendor/yiisoft/yii2/rest/Serializer.php at line 278	

	serializeDataProvider(): /yii2-basic/vendor/yiisoft/yii2/rest/Serializer.php at line 174	

	serialize(): /yii2-basic/vendor/yiisoft/yii2/rest/Serializer.php at line 143	

	serializeData(): /yii2-basic/vendor/yiisoft/yii2/rest/Controller.php at line 97	

	afterAction(): /yii2-basic/vendor/yiisoft/yii2/rest/Controller.php at line 75	

	runAction(): /yii2-basic/vendor/yiisoft/yii2/base/Controller.php at line 153	

	runAction(): /yii2-basic/vendor/yiisoft/yii2/base/Module.php at line 455	

	handleRequest(): /yii2-basic/vendor/yiisoft/yii2/web/Application.php at line 84	

	run(): /yii2-basic/vendor/yiisoft/yii2/base/Application.php at line 375	

	/yii2-basic/web/index.php at line 12	

here is the signature found in Yii’s source code:

  public function toArray(array $fields = [], array $expand = [], $recursive = true)


        $data = [];

        foreach ($this->resolveFields($fields, $expand) as $field => $definition) {

            $data[$field] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field);  <==== ArrayableTrait.php at line 120	


        if ($this instanceof Linkable) {

            $data['_links'] = Link::serialize($this->getLinks());


        return $recursive ? ArrayHelper::toArray($data) : $data;


hope that helps.