認証がいらないならpublic
におけばよいが、必要な場合はstorage/app/pdf/
なんかに置いたりして、認証チェックしてから出力したい。
Controller
app/Http/Controllers/PdfController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class PdfController extends Controller
{
protected static $pdfPath = 'pdf/';
public function __construct()
{
$this->middleware('auth');
}
public function show($fileName)
{
$filePath = self::$pdfPath . $fileName;
abort_if(!Storage::exists($filePath), 404);
return response()->make(Storage::get($filePath), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $fileName . '"'
]);
}
}
Routing
routes/web.php
Route::get('pdf/{fileName}', 'PdfController@show')->name('pdf.show');
View
<a href="{{ route('pdf.show', ['fileName' => 'hoge.pdf']) }}" target="_blank">PDF</a>