Laravel-excel: [BUG] response headers (cors)

Created on 16 Nov 2016  路  3Comments  路  Source: Maatwebsite/Laravel-Excel

Package version, Laravel version

laravel 5.1
laravel-excel 2.1.0
barryvdh/laravel-cors 0.8.0

Expected behaviour

I expected that laravel-excel works fine with laravel-cors

Actual behaviour

laravel-excel ignores laravel-cors and not set in the response the
headers "Access-Control-Allow-Origin" and "Vary"

in download and export methods laravel-excel not use Illuminate\Http\Response
this is what broken all?

Steps to reproduce the behaviour

Make a request from www.example-a.com in javascript (in my case angularjs application) to www.example-b.com (server with laravel + laraver-cors + laravel-excel)

Most helpful comment

This package doesn't support usage of Response, as PHPExcel handles the actual download. You will have to pass the headers yourself (2nd param)

->download('xls', [yourheader])

All 3 comments

This package doesn't support usage of Response, as PHPExcel handles the actual download. You will have to pass the headers yourself (2nd param)

->download('xls', [yourheader])

Please, add this to the documentation.

Hi, i'm actually using Laravel-Excel in a Lumen project and I am trying to export data in an excel file and download it as below:

My UsersExport:

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class UsersExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::all();
    }
}

My controller:

public function excel()
    {
        return Excel::download(new UsersExport(),'users.xlsx');
    } 

web.php:
$router->get('/download', 'CommunicationController@excel');

And when I'm trying to execute the excel() function i'm getting this error

image

However, I already created a CorsMiddleware.php and add it in my bootstrap/app.php long time ago and all the other functions works very well thanks of it.

app.php:

$app->middleware([
    App\Http\Middleware\CorsMiddleware::class
]);

CorsMiddleware.php:

<?php

namespace App\Http\Middleware;

use Closure;

class CorsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With, api_token'
        ];

        if ($request->isMethod('OPTIONS'))
        {
            return response()->json('{"method":"OPTIONS"}', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
        {
            $response->header($key, $value);
        }

        return $response;
    }
}

Did I forget something ? I'm stuck on it since yesterday and I really would like your help :'(

Here you can see the versions of Lumen I use and also maatwebsite/excel:

"require": {
        "php": ">=7.1.3",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/lumen-framework": "5.7.*",
        "maatwebsite/excel": "^3.1",
        "phpoffice/phpspreadsheet": "^1.6",
        "vlucas/phpdotenv": "~2.2"
    },

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

contifico picture contifico  路  3Comments

ellej16 picture ellej16  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

kertul picture kertul  路  3Comments

vandolphreyes picture vandolphreyes  路  3Comments