0

In my project, I am trying to use thephpleague/glide for URL based image manipulation which works absolutely fine with the codes mentioned below.

But the problem is that I am unable to figure out how this piece of code is going to affect the performance of my project as I have never added a single line of code in bootstrap/app.php file before. I want to know how this piece of code is going to affect my app's performance and is this the right way to do it or is there any better way.

I will be highly thankful if anyone suggests any other or better way to add this piece of code in config or anywhere else.

Code update in bootstrap/app.php

$app->singleton('League\Glide\Server', function ($app) {

  $filesystem = $app->make('Illuminate\Contracts\Filesystem\Filesystem');
  return \League\Glide\ServerFactory::create([
    'source'                => Storage::disk('s3')->getDriver(),
    'cache'                 => Storage::disk('local')->getDriver(),
    //'source_path_prefix'    => '',
    'cache_path_prefix'     => 'uploads/images/.cache',
    'base_url'              => 'img',
    'useSecureURLs'         => false,
  ]);
});

Route

Route::get('img/{path}', function (League\Glide\Server $server, 
    Illuminate\Http\Request $request, $path) {
    $server->outputImage($path, $request->all());
})->where('path', '.*');

In the original code which I found on the GitHub it was mentioned to use this code in config instead of bootstrap/app.php, but I have no idea how to do it.

Any kind of help or any alternate and better suggestion is highly appreciated.

Prateek
  • 1,229
  • 17
  • 31
  • 1
    `$filesystem = $app->make('Illuminate\Contracts\Filesystem\Filesystem');` this line doesn't seem to do anything in the context of that singleton. What the rest of the code does is telling Laravel that everytime it has to resolve `League\Glide\Server` (which should be something abstract like an interface), it should return `\League\Glide\ServerFactory::create([...])`. – IGP Feb 09 '21 at 10:41
  • @IGP You are right, I have removed the line mentioned and it is working fine. But my concern is that in the documentation it was written that we should not change bootstrap/app.php file, but in order to get this code working I have to place it there. Cann you suggest any other way to add this code apart from bootstrap/app.php – Prateek Feb 09 '21 at 11:35
  • OK, get it. moved the file to the controller and it is working fine. thank you @IGP – Prateek Feb 09 '21 at 11:41
  • 1
    Singletons can be declared in ServiceProvider classes with the syntax `app()->singleton(....)` – IGP Feb 09 '21 at 13:50

0 Answers0