kolyunya / yii2-partial-content
Yii2 partial content filter.
Installs: 56
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Type:yii2-extension
This package is auto-updated.
Last update: 2024-11-10 23:57:48 UTC
README
#Yii2 partial content filter
##Description
A Yii2 filter which empowers a controller with a functionality of a partial HTTP response.
The widget is composer-enabled. You can aquire the latest available version from the packagist repository.
##Usage example
The controller currently has four options to set the content to be sent to the client. Those options are contentData
, contentResource
, contentStream
and the contentFile
. The use of those properties is pretty straightforward. One of those properties will be used by the filter to send data to the client completly or partially depending on it's request. The filter should be added to the controller just like any other Yii2 action filter. You may also specify actions which should be processed by the filter.
The controller may also set the contentType
property of the filter which will be used by the filter as a value of the Content-Type
header. The default value is application/octet-stream
.
The controller may also set the contentName
property of the filter which will be used as a filename value of the Content-Disposition
header. If the controller does not set this property this header will not be sent.
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 }