I added the code to make things more clear.
DOMPDF and loading routed images
I'm using DOMPDF (Barry package) to generate .pdf files, for a new module i'm required to place images in the generated .pdf document. All my images are in the storage folder, and i use a route to return me the image i request.
The thing is, this images route is behind my auth route group, because only authenticated users are allowed to retrieve the images, but DOMPDF does not seem able to fetch the images. When i place the image route outside of the auth group, the images get loaded, but when i place it backinse the auth group they won't load.
Has anyone faced this issue aswell?
P.s. my auth group is a custom middleware, checking for a session called auth instead of the regular Laravel Auth::check() method.
Middleware:
public function handle($request, Closure $next, $guard = null)
{
if (Session::has('auth')) {
return $next($request);
}
return redirect()->route('login');
}
Routes:
Route::group(['middleware' => 'myMiddleware'], function() {
Route::get('photo/{lead}/{type}/{name}/{plain?}', ['as' => 'inventory.photo', function ($lead, $type, $name) {
switch($type) {
case 1:
$folder = 'roof';
break;
case 2:
$folder = 'meterboard';
break;
case 3:
$folder = 'access';
break;
case 4:
$folder = 'converter';
break;
case 5:
$folder = 'cable';
break;
}
return Image::make(storage_path() . '/app/photo/'. $lead .'/'. $folder .'/'. $name)->response();
}]);
}]);
Controller:
public function workletter($link)
{
return PDF::loadView('pdf.workletter', [
'link' => $link
])->stream();
}
}
Template:
{!! Html::image(route('inventory.photo', [$link]), 'Photo alt name') !!}
@Qlic your problem is you put your code inside your route closure and attach middleware to it. Instead create a separate class or helper function that you can call from your route and your controller..
function getImage($foo, $bar, $whatever)
{
// image getting code
return $image;
}
Route::group(['middleware' => 'myMiddleware'], function() {
Route::get('photo/{lead}/{type}/{name}/{plain?}', ['as' => 'inventory.photo', function ($lead, $type, $name) {
return getImage($foo, $bar, $whatever);
}]);
}]);
{!! Html::image(route('inventory.photo', [getImage($foo, $bar, $whatever)]), 'Photo alt name') !!}
This is not a working code, just an example to show that if you use helper functions you can re-use your code in different parts of your application.
Please or to participate in this conversation.