如何构建与 PDF 相关的代码,使其不在路由文件中?
How to structure the PDF-related code so that it is not in routes file?
我正在使用 Laravel Snappy PDF 包 barryvdh/laravel-snappy
一切正常,但是路由文件现在有很多我想从路由文件中清除并放在其他地方的信息。
Route::get('services/{service}/overview', function ($id) {
$service = \App\Service::where('id', '=', $id)->first();
$service->load('sbo_name', 'sbm_name', 'stm_name', 'get_lifecycle_status', 'items', 'environments', 'agreements', 'supported_services', 'required_services', 'serviceDependencies', 'readiness');
$data['service'] = $service->toArray();
$pdf = PDF::loadView('reports.services.overview', $data)
->setPaper('a4')
->setOrientation('portrait')
->setOption('footer-right', 'Page [page] of [toPage] ')
->setOption('footer-left', ' Printed: [date]')
->setOption('footer-font-size', 8)
->setOption('footer-font-name', 'Arial')
->setOption('margin-top', 10)
->setOption('margin-left', 5)
->setOption('margin-right', 5)
->setOption('lowquality', false)
->setOption('dpi', 96)
->setOption('image-quality', 100);
return $pdf->inline('Service Overview.pdf');
});
构建这个的最佳方式是什么?我也不想为每个新报告重复 pdf 选项。
首先,通过将路由文件移动到控制器中来清理它们。
Route::get('services/{service}/overview', ExportServiceController@show);
// app/Http/Controllers/ExportServiceController.php
class ExportServiceController extends Controller
public function show($id) {
return (new App/ExportServicePdf)->execute($id);
}
通过将逻辑移动到它自己的 class 中进一步清理您的控制器,以便它可以在其他控制器中重复使用。
// for example app/ExportServicePdf.php
namespace App;
class ExportServicePdf
{
public function execute($id)
{
$service = \App\Service::where('id', '=', $id)->first();
$service->load('sbo_name', 'sbm_name', 'stm_name', 'get_lifecycle_status', 'items', 'environments', 'agreements', 'supported_services', 'required_services', 'serviceDependencies', 'readiness');
$data['service'] = $service->toArray();
$pdf = PDF::loadView('reports.services.overview', $data)
->setPaper('a4')
->setOrientation('portrait')
->setOption('footer-right', 'Page [page] of [toPage] ')
->setOption('footer-left', ' Printed: [date]')
->setOption('footer-font-size', 8)
->setOption('footer-font-name', 'Arial')
->setOption('margin-top', 10)
->setOption('margin-left', 5)
->setOption('margin-right', 5)
->setOption('lowquality', false)
->setOption('dpi', 96)
->setOption('image-quality', 100);
return $pdf->inline('Service Overview.pdf');
}
}
快速浏览一下库,有一个 public 方法 setOptions()
接受一组选项。
在你的ExportServicePdf
class中,你可以进一步重构为:
class ExportServicePdf
{
private $options = [
'footer-font-size' => 8,
'footer-font-name' => 'Arial',
//...
];
// Then you can do
public function execute($id)
{
$pdf = PDF::loadView('reports.services.overview', $data)
->setPaper('a4')
->setOrientation('portrait')
->setOptions($this->options)
}
}
我正在使用 Laravel Snappy PDF 包 barryvdh/laravel-snappy
一切正常,但是路由文件现在有很多我想从路由文件中清除并放在其他地方的信息。
Route::get('services/{service}/overview', function ($id) {
$service = \App\Service::where('id', '=', $id)->first();
$service->load('sbo_name', 'sbm_name', 'stm_name', 'get_lifecycle_status', 'items', 'environments', 'agreements', 'supported_services', 'required_services', 'serviceDependencies', 'readiness');
$data['service'] = $service->toArray();
$pdf = PDF::loadView('reports.services.overview', $data)
->setPaper('a4')
->setOrientation('portrait')
->setOption('footer-right', 'Page [page] of [toPage] ')
->setOption('footer-left', ' Printed: [date]')
->setOption('footer-font-size', 8)
->setOption('footer-font-name', 'Arial')
->setOption('margin-top', 10)
->setOption('margin-left', 5)
->setOption('margin-right', 5)
->setOption('lowquality', false)
->setOption('dpi', 96)
->setOption('image-quality', 100);
return $pdf->inline('Service Overview.pdf');
});
构建这个的最佳方式是什么?我也不想为每个新报告重复 pdf 选项。
首先,通过将路由文件移动到控制器中来清理它们。
Route::get('services/{service}/overview', ExportServiceController@show);
// app/Http/Controllers/ExportServiceController.php
class ExportServiceController extends Controller
public function show($id) {
return (new App/ExportServicePdf)->execute($id);
}
通过将逻辑移动到它自己的 class 中进一步清理您的控制器,以便它可以在其他控制器中重复使用。
// for example app/ExportServicePdf.php
namespace App;
class ExportServicePdf
{
public function execute($id)
{
$service = \App\Service::where('id', '=', $id)->first();
$service->load('sbo_name', 'sbm_name', 'stm_name', 'get_lifecycle_status', 'items', 'environments', 'agreements', 'supported_services', 'required_services', 'serviceDependencies', 'readiness');
$data['service'] = $service->toArray();
$pdf = PDF::loadView('reports.services.overview', $data)
->setPaper('a4')
->setOrientation('portrait')
->setOption('footer-right', 'Page [page] of [toPage] ')
->setOption('footer-left', ' Printed: [date]')
->setOption('footer-font-size', 8)
->setOption('footer-font-name', 'Arial')
->setOption('margin-top', 10)
->setOption('margin-left', 5)
->setOption('margin-right', 5)
->setOption('lowquality', false)
->setOption('dpi', 96)
->setOption('image-quality', 100);
return $pdf->inline('Service Overview.pdf');
}
}
快速浏览一下库,有一个 public 方法 setOptions()
接受一组选项。
在你的ExportServicePdf
class中,你可以进一步重构为:
class ExportServicePdf
{
private $options = [
'footer-font-size' => 8,
'footer-font-name' => 'Arial',
//...
];
// Then you can do
public function execute($id)
{
$pdf = PDF::loadView('reports.services.overview', $data)
->setPaper('a4')
->setOrientation('portrait')
->setOptions($this->options)
}
}