Laravel-debugbar: Debug bar output appearing in CSV output

Created on 24 Mar 2017  路  5Comments  路  Source: barryvdh/laravel-debugbar

Hey,

I'm seeing debug bar output when using League's CSV Writer http://csv.thephpleague.com/

My code (abbreviated):

            $writer = Writer::createFromFileObject(new \SplTempFileObject());
            $writer->insertAll($rows);
            $writer->output(date('Y-m-d') . '.csv');

This output appears on the last row of the CSV

<link rel='stylesheet' type='text/css' property='stylesheet' href='//example.com/_debugbar/assets/stylesheets?v=1484813989'><script type='text/javascript' src='//example.com/_debugbar/assets/javascript?v=1483605979'></script><script type="text/javascript">jQuery.noConflict(true);</script>
<script type="text/javascript">
var phpdebugbar = new PhpDebugBar.DebugBar();
phpdebugbar.addTab("messages"
phpdebugbar.addIndicator("time"
phpdebugbar.addTab("timeline"
phpdebugbar.addIndicator("memory"
phpdebugbar.addTab("exceptions"
phpdebugbar.addTab("views"
phpdebugbar.addTab("route"
phpdebugbar.addIndicator("currentroute"
phpdebugbar.addTab("queries"
phpdebugbar.addTab("emails"
phpdebugbar.addTab("session"
phpdebugbar.addTab("request"
phpdebugbar.setDataMap({
messages: ["messages.messages"
messages:badge: ["messages.count"
time: ["time.duration_str"
timeline: ["time"
memory: ["memory.peak_usage_str"
exceptions: ["exceptions.exceptions"
exceptions:badge: ["exceptions.count"
views: ["views"
views:badge: ["views.nb_templates"
route: ["route"
currentroute: ["route.uri"
queries: ["queries"
queries:badge: ["queries.nb_statements"
emails: ["swiftmailer_mails.mails"
emails:badge: ["swiftmailer_mails.count"
session: ["session"
request: ["request"
});
phpdebugbar.restoreState();
phpdebugbar.ajaxHandler = new PhpDebugBar.AjaxHandler(phpdebugbar);
phpdebugbar.ajaxHandler.bindToXHR();
phpdebugbar.setOpenHandler(new PhpDebugBar.OpenHandler({"url":"http:\/\/example.com\/_debugbar\/open"}));
phpdebugbar.addDataSet({"__meta":{"id":"d1bb4c12da1dda871c00f04227b4cc3a"

</script>

I tried adding
Config::set('laravel-debugbar::config.enabled', false); before the output but this has not resolved the issue.

Most helpful comment

Yes, never use echo or header in Laravel.

Use the output function with filename to just return the output as string, the create a response object with the headers.

        $filename = date('Y-m-d') . '.csv';
        return response((string) $writer, 200, [
            'Content-Type' => 'text/csv',
            'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' => 'attachment; filename="'.$filename.'"',
        ]);

All 5 comments

Don't echo the output, return it as a proper response with headers.

@barryvdh I'm not echoing any output, I'm using the library's output function:
$writer->output(date('Y-m-d') . '.csv'); which outputs headers:

            header('Content-Type: text/csv');
            header('Content-Transfer-Encoding: binary');
            header("Content-Disposition: attachment; filename=\"$filename\"");

so the file is downloaded in the browser.

Yes, never use echo or header in Laravel.

Use the output function with filename to just return the output as string, the create a response object with the headers.

        $filename = date('Y-m-d') . '.csv';
        return response((string) $writer, 200, [
            'Content-Type' => 'text/csv',
            'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' => 'attachment; filename="'.$filename.'"',
        ]);

Sorry, you should do (string) $writer instead of $writer->output()

That's fixed it. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

farshadff picture farshadff  路  4Comments

innerdev picture innerdev  路  5Comments

lozadaOmr picture lozadaOmr  路  6Comments

ddmo picture ddmo  路  3Comments

hookover picture hookover  路  4Comments