Elaine Marley
Elaine Marley

Reputation: 2213

Routing to a file on public folder with Laravel 4

I have a route like this:

 Route::any("/operations/xml", array("as"=>"operations.xml", "uses"=>"OperationsController@generateXml"));

And my controller generates an xml that saves inside the public folder:

OperationsController.php:

public function generateXml(){

    $doc = new DomDocument("1.0", "UTF-8");
    $doc->formatOutput = true;

    //I add my stuff inside de xml from the database here...

    $doc->save("xml/test.xml");

}

But what I need is that when I navigate to the route http://example.com/operations/xml I will be able to download the xml. How can I do this?

Upvotes: 0

Views: 321

Answers (1)

Matt Burrow
Matt Burrow

Reputation: 11057

Add the following to the end of the function.

return Response::download(public_path() . "xml/test.xml", 'name.xml', array('Content-Type' => 'application/xml'));

The parameters are;

  • 1st is the file path.
  • 2nd is the name of the file.
  • 3rd are the headers.

Upvotes: 1

Related Questions