PHP Class CsvView\View\CsvView

By setting the '_serialize' key in your controller, you can specify a view variable that should be serialized to CSV and used as the response for the request. This allows you to omit views + layouts, if your just need to emit a single view variable as the CSV response. In your controller, you could do the following: $this->set(['posts' => $posts, '_serialize' => 'posts']); When the view is rendered, the $posts view variable will be serialized into CSV. When rendering the data, the data should be a single, flat array. If this is not the case, then you should also specify an _extract variable: $_extract = [ ['id', '%d'], // Hash-compatible path, sprintf-compatible format 'description', // Hash-compatible path function ($row) { // Callable return value } ]; You can also define '_serialize' as an array. This will create a top level object containing all the named view variables: $this->set(compact('posts', 'users', 'stuff')); $this->set('_serialize', array('posts', 'users')); Each of the viewVars in _serialize would then be output into the csv If you don't use the _serialize key, you will need a view. You can use extended views to provide layout like functionality. When not using custom views, you may specify the following view variables: - array $_header: (default null) A flat array of header column names - array $_footer: (default null) A flat array of footer column names - string $_delimiter: (default ',') CSV Delimiter, defaults to comma - string $_enclosure: (default '"') CSV Enclosure for use with fputscsv() - string $_eol: (default '\n') End-of-line character the csv
Inheritance: extends Cake\View\View
Show file Open project: friendsofcake/cakephp-csvview

Public Properties

Property Type Description
$layoutPath string CSV layouts are located in the csv sub directory of Layouts/
$subDir string CSV views are always located in the 'csv' sub directory for a controllers views.

Protected Properties

Property Type Description
$_resetStaticVariables boolean Whether or not to reset static variables in use
$_specialVars array List of special view vars.

Public Methods

Method Description
__construct ( Cake\Network\Request $request = null, Response $response = null, Cake\Event\EventManager $eventManager = null, array $viewOptions = [] ) Constructor
loadHelpers ( ) : void Skip loading helpers if this is a _serialize based view.
render ( string | null $view = null, string | null $layout = null ) : string Render a CSV view.

Protected Methods

Method Description
_generateRow ( array | null $row = null ) : string | false Generates a single row in a csv from an array of data by writing the array to a temporary file and returning it's contents
_renderContent ( ) : void Renders the body of the data to the csv
_renderRow ( array | null $row = null ) : null | string Aggregates the rows into a single csv
_serialize ( ) : string Serialize view vars.
_setupViewVars ( ) : void Setup defaults for CsvView view variables

Method Details

__construct() public method

Constructor
public __construct ( Cake\Network\Request $request = null, Response $response = null, Cake\Event\EventManager $eventManager = null, array $viewOptions = [] )
$request Cake\Network\Request Request instance.
$response Cake\Network\Response Response instance.
$eventManager Cake\Event\EventManager EventManager instance.
$viewOptions array An array of view options

_generateRow() protected method

Generates a single row in a csv from an array of data by writing the array to a temporary file and returning it's contents
protected _generateRow ( array | null $row = null ) : string | false
$row array | null Row data
return string | false String with the row in csv-syntax, false on fputscv failure

_renderContent() protected method

Renders the body of the data to the csv
protected _renderContent ( ) : void
return void

_renderRow() protected method

Aggregates the rows into a single csv
protected _renderRow ( array | null $row = null ) : null | string
$row array | null Row data
return null | string CSV with all data to date

_serialize() protected method

Serialize view vars.
protected _serialize ( ) : string
return string The serialized data

_setupViewVars() protected method

The following variables can be retrieved from '$this->viewVars' for use in configuring this view: - array '_header': (default null) A flat array of header column names - array '_footer': (default null) A flat array of footer column names - array '_extract': (default null) An array of Hash-compatible paths or callable with matching 'sprintf' $format as follows: $_extract = [ [$path, $format], [$path], $path, function () { ... } // Callable ]; If a string or unspecified, the format default is '%s'. - '_delimiter': (default ',') CSV Delimiter, defaults to comma - '_enclosure': (default '"') CSV Enclosure for use with fputscsv() - '_newline': (default '\n') CSV Newline replacement for use with fputscsv() - '_eol': (default '\n') End-of-line character the csv - '_bom': (default false) Adds BOM (byte order mark) header - '_setSeparator: (default false) Adds sep=[_delimiter] in the first line
protected _setupViewVars ( ) : void
return void

loadHelpers() public method

Skip loading helpers if this is a _serialize based view.
public loadHelpers ( ) : void
return void

render() public method

Uses the special '_serialize' parameter to convert a set of view variables into a CSV response. Makes generating simple CSV responses very easy. If you omit the '_serialize' parameter, and use a normal view + layout as well. Also has support for specifying headers and footers in '_header' and '_footer' variables, respectively.
public render ( string | null $view = null, string | null $layout = null ) : string
$view string | null The view being rendered.
$layout string | null The layout being rendered.
return string The rendered view.

Property Details

$_resetStaticVariables protected property

Whether or not to reset static variables in use
protected bool $_resetStaticVariables
return boolean

$_specialVars protected property

List of special view vars.
protected array $_specialVars
return array

$layoutPath public property

CSV layouts are located in the csv sub directory of Layouts/
public string $layoutPath
return string

$subDir public property

CSV views are always located in the 'csv' sub directory for a controllers views.
public string $subDir
return string