kolyunya / yii2-partial-content
Yii2 部分内容过滤器。
dev-master
2014-11-14 14:44 UTC
This package is auto-updated.
Last update: 2024-09-10 23:41:51 UTC
README
#Yii2 部分内容过滤器
##描述
一个Yii2 过滤器,它使控制器具备部分 HTTP 响应的功能。
小部件支持composer。您可以从Packagist 仓库获取最新版本。
##使用示例
控制器当前有四种设置要发送给客户端的内容的选项。这些选项是 contentData
、contentResource
、contentStream
和 contentFile
。这些属性的使用非常简单。其中一个属性将由过滤器用于将数据完全或部分地发送给客户端,具体取决于其请求。应像添加任何其他 Yii2 动作过滤器一样将过滤器添加到控制器中。您还可以指定由过滤器处理的活动。
控制器还可以设置过滤器的 contentType
属性,该属性将被过滤器用作 Content-Type
标头的值。默认值是 application/octet-stream
。
控制器还可以设置过滤器的 contentName
属性,该属性将被用作 Content-Disposition
标头的文件名值。如果控制器不设置此属性,则不会发送此标头。
public function behaviors() { return [ // Add a partial content filter 'partial-content' => [ // Specify filter class name 'class' => 'kolyunya\yii2\filters\PartialContent', // Specify which actions it will be applied to 'only' => [ 'get' ] ] ]; } public function actionGet() { // Get a reference to the filter $behavior = $this->getBehavior('partial-content'); // Now you have four options to set the data // [0] - Either set the data itself $behavior->contentData = $this->data; // [1] - Or specify the data resource $behavior->contentResource = $this->resource; // [2] - Or specify the data stream $behavior->contentStream = $this->stream; // [3] - Or specify the file name $behavior->contentFile = $this->file; // Optionally set the content type $behavior->contentType = 'audio/mpeg'; // Optionally set the content name $behavior->contentName = 'My new song'; // The filter will do the rest itself }