kolyunya/yii2-partial-content

Yii2 partial content filter.

dev-master 2014-11-14 14:44 UTC

This package is auto-updated.

Last update: 2024-04-10 22:44:42 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

}