2016-08-05 79 views
5

我已经定义了自定义响应类,并试图在模块中使用它。Yii2模块自定义响应类

在控制器操作中,我返回一个结果数组,但不使用自定义响应类。

相反,所用的类是缺省YII \网络\响应

我的实现

在配置/ web.php模块配置:

'mymodule' => [ 
     'class' => 'app\modules\mymod\Mymod', 
     'components' => [     
      'response' => [ 
       'class' => 'app\modules\mymod\components\apiResponse\ApiResponse', 
       'format' => yii\web\Response::FORMAT_JSON, 
       'charset' => 'UTF-8', 
      ], 
     ], 
    ], 

在控制器我编辑行为方法:

public function behaviors() { 
    $behaviors = parent::behaviors();   
    $behaviors['contentNegotiator'] = [ 
     'class' => 'yii\filters\ContentNegotiator',    
     'response' => $this->module->get('response'),     
     'formats' => [ //supported formats 
      'application/json' => \yii\web\Response::FORMAT_JSON, 
     ], 
    ]; 
    return $behaviors; 
} 

在行动,如果我做的:

public function actionIndex() { 

    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 

    $dataList = [ 
     ['id' => 1, 'name' => 'John', 'surname' => 'Davis'], 
     ['id' => 2, 'name' => 'Marie', 'surname' => 'Baker'], 
     ['id' => 3, 'name' => 'Albert', 'surname' => 'Bale'], 
    ]; 
    return $dataList; 
} 

我得到这样的结果(从警予\网络\响应预期):

[ 
{ 
    "id": 1, 
    "name": "John", 
    "surname": "Davis" 
}, 
{ 
    "id": 2, 
    "name": "Marie", 
    "surname": "Baker" 
}, 
{ 
    "id": 3, 
    "name": "Albert", 
    "surname": "Bale" 
} 
] 

但是,如果我改变行动在:

$dataList = [ 
    ['id' => 1, 'name' => 'John', 'surname' => 'Davis'], 
    ['id' => 2, 'name' => 'Marie', 'surname' => 'Baker'], 
    ['id' => 3, 'name' => 'Albert', 'surname' => 'Bale'], 
]; 
//return $dataList; 

$resp = $this->module->get('response'); //getting the response component from the module configuration 
$resp->data = $dataList; 

return $resp; 

然后我得到预期的结果,这是这样的:

{ 
"status": { 
    "response_code": 0, 
    "response_message": "OK", 
    "response_extra": null 
}, 
"data": [ 
    { 
     "id": 1, 
     "name": "John", 
     "surname": "Davis" 
    }, 
    { 
     "id": 2, 
     "name": "Marie", 
     "surname": "Baker" 
    }, 
    { 
     "id": 3, 
     "name": "Albert", 
     "surname": "Bale" 
    } 
]} 

看来我定义的行为没有做任何事情。

我需要做些什么才能在操作中返回数组并使用自定义响应组件?

在此先感谢

回答

3

yii\base\Module没有响应组件,因此您的配置将无法正常工作。 而不是将response组件添加到您的模块您可以改变Yii::$app->responseMyMod::init()功能。

如果你想完全自己的组件替换Yii::$app->response

public function init() 
{ 
    parent::init(); 

    \Yii::configure(\Yii::$app, [ 
     'components' => [ 
      'response' => [ 
       'class' => 'app\modules\mymod\components\apiResponse\ApiResponse', 
       'format' => yii\web\Response::FORMAT_JSON, 
       'charset' => 'UTF-8', 
      ], 
     ] 
    ]); 
} 

但我认为这是一个坏主意,完全取代父应用程序的响应部件的模块中。更好的方法是修改您的需求的响应行为。例如,你可以使用EVENT_BEFORE_SEND并建立自己的数据结构响应:

public function init() 
{ 
    parent::init(); 

    // you can use ContentNegotiator at the level of module 
    // and remove this behavior declaration from controllers 
    \Yii::configure($this, [ 
     'as contentNegotiator' => [ 
      'class' => 'yii\filters\ContentNegotiator', 
      // if in a module, use the following IDs for user actions 
      // 'only' => ['user/view', 'user/index'] 
      'formats' => [ 
       'application/json' => Response::FORMAT_JSON, 
      ], 
     ], 
    ]); 


    // you can daclare handler as function in you module and pass it as parameter here 
    \Yii::$app->response->on(Response::EVENT_BEFORE_SEND, function ($event) { 
     $response = $event->sender; 
     // here you can get and modify everything in current response 
     // (data, headers, http status etc.) 
     $response->data = [ 
      'status' => 'Okay', 
      'data' => $response->data 
     ]; 
    }); 
} 
+0

是的,我不想通过模块取代了应用程序的响应组件。 只需在模块中使用自定义响应组件。 谢谢! – Jepi

+0

@Jepi然后使用第一个选项。如果您将应用程序响应组件替换为模块的init函数,那么它只会影响您的模块。要替换整个应用程序中的某个组件(包括其他组件),应该在引导过程中进行替换 – oakymax

+0

@Jepi我的意思是父应用程序可能已经实现了响应组件。例如,可以启动一些用于所有模块通用的记录或调试的服务。通过替换模块内的组件,您完全阻止了在父应用程序中为您的模块定制此组件的功能。因此,如果您希望您的模块将用于其他应用程序,请记住 – oakymax