Download the PHP package friendsofcake/cakephp-csvview without Composer

On this page you can find all versions of the php package friendsofcake/cakephp-csvview. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cakephp-csvview

CI Coverage Status Total Downloads Latest Stable Version

CsvView Plugin

Quickly enable CSV output of your model data.

This branch is for CakePHP 5.x. For details see version map.

Background

I needed to quickly export CSVs of stuff in the database. Using a view class to iterate manually would be a chore to replicate for each export method, so I figured it would be much easier to do this with a custom view class, like JsonView or XmlView.

Installation

Enable plugin

Load the plugin by running command

bin/cake plugin load CsvView

Usage

To export a flat array as a CSV, one could write the following code:

All variables that are to be included in the csv must be specified in the serialize view option, exactly how JsonView or XmlView work.

It is possible to have multiple variables in the csv output:

If you want headers or footers in your CSV output, you can specify either a header or footer view option. Both are completely optional:

You can also specify the delimiter, end of line, newline, escape characters and byte order mark (BOM) sequence using delimiter, eol, newline, enclosure and bom respectively:

The defaults for these options are:

The eol option is the one used to generate newlines in the output. newline, however, is the character that should replace the newline characters in the actual data. It is recommended to use the string representation of the newline character to avoid rendering invalid output.

Some reader software incorrectly renders UTF-8 encoded files which do not contain byte order mark (BOM) byte sequence. The bom option is the one used to add byte order mark (BOM) byte sequence beginning of the generated CSV output stream. See Wikipedia article about byte order mark for more information.

The setSeparator option can be used to set the separator explicitly in the first line of the CSV. Some readers need this in order to display the CSV correctly.

If you have complex model data, you can use the extract view option to specify the individual Hash::extract()-compatible paths or a callable for each record:

If your model data contains some null values or missing keys, you can use the null option, just like you'd use delimiter, eol, and enclosure, to set how null values should be displayed in the CSV.

null defaults to ''.

Automatic view class switching

You can use the controller's content negotiation feature to automatically have the CsvView class switched in as follows.

Enable csv extension parsing using $routes->addExtensions(['csv']) within required scope in your app's routes.php.

With the above controller you can now access /posts.csv or use Accept header text/csv to get the data as csv and use /posts to get normal HTML page.

For really complex CSVs, you can also use your own view files. To do so, either leave serialize unspecified or set it to null. The view files will be located in the csv subdirectory of your current controller:

Setting a different encoding to the file

If you need to have a different encoding in you csv file you have to set the encoding of your data you are passing to the view and also set the encoding you want for the csv file. This can be done by using dataEncoding and csvEncoding:

The defaults are:

** Only if those two variable are different your data will be converted to another encoding.

CsvView uses the iconv extension by default to encode your data. You can change the php extension used to encode your data by setting the transcodingExtension option:

The currently supported encoding extensions are as follows:

Setting the downloaded file name

By default, the downloaded file will be named after the last segment of the URL used to generate it. Eg: example.com/my-controller/my-action would download my-action.csv, while example.com/my-controller/my-action/first-param would download first-param.csv.

In IE you are required to set the filename, otherwise it will download as a text file.

To set a custom file name, use the Response::withDownload() method. The following snippet can be used to change the downloaded file from export.csv to my-file.csv:

Using a specific View Builder

In some cases, it is better not to use the current controller's View Builder $this->viewBuilder() as any call to $this->render() will compromise any subsequent rendering.

For example, in the course of your current controller's action, if you need to render some data as CSV in order to simply save it into a file on the server.

Do not forget to add to your controller:

So you can create a specific View Builder:


All versions of cakephp-csvview with dependencies

PHP Build Version
Package Version
Requires cakephp/cakephp Version ^5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package friendsofcake/cakephp-csvview contains the following files

Loading the files please wait ....