Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] response headers (cors) #990

Closed
lucatamtam opened this issue Nov 16, 2016 · 3 comments
Closed

[BUG] response headers (cors) #990

lucatamtam opened this issue Nov 16, 2016 · 3 comments

Comments

@lucatamtam
Copy link

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)

@patrickbrouwers
Copy link
Member

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])

@esteban-serfe
Copy link

Please, add this to the documentation.

@ivanherve
Copy link

ivanherve commented Apr 2, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants