I'm trying to achieve a link in a menu bar, such that when a user clicks on the link, a pdf file will be downloaded automatically, and it should not navigate to other pages.
Inside my main.blade.php, consists of a menu bar, I have this link:
<a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/php/timetable/public/download">Help</a>
where $_SERVER['SERVER_NAME'] is localhost.
Inside my routes.php:
Route::get('/download', array('uses'=>'MainController@getDownloadHelp'));
Inside my controller called MainController:
public function getDownloadHelp()
{
$file= public_path(). "/public/download";
$filename = 'help.pdf';
$headers = array(
'Content-Type' => 'application/pdf',
);
return Response::download($file, $filename, $headers);
}
The PDF file is stored under /public/download/help.pdf
The problem I'm facing right now is, when I clicked the 'Help' link on the menu bar, it redirects me to localhost/download which is not what I wanted. And also, the pdf is not downloaded.
I really need some help here! Where and what did I went wrong?
You can try to use {{ URL::to('/download') }}
instead of $_SERVER['SERVER_NAME']...
or in your blade
echo link_to('download', $title, $attributes = array(), $secure = null);
it will write all the <a></a>
thing
for the path in the controller maybe you can remove the public in the path of the file like this
public_path() . '/download/';