2017-08-31 98 views
0

这是我的代码:Laravel 5.5自定义帮助文件类成为不存在

//helpers.php 
if (!function_exists('image_helper')) { 
    function image_helper($file = NULL, $path = NULL) 
    { 
     $image = app('image_helper'); 

     if (!is_null($file) && is_null($path)) { 
      return $image->generateTmp($file); 
     } 

     return $image; 
    } 
} 

// ImageHelper.php 
<?php 

namespace App\Helpers; 

use Illuminate\Http\File; 
use Illuminate\Support\Facades\DB; 
use Illuminate\Support\Facades\Storage; 
use Intervention\Image\Facades\Image; 
use Spatie\ImageOptimizer\OptimizerChainFactory; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 

class ImageHelper 
{ 
    private $fileName, $sizes; 

    function __construct() 
    { 
     $this->fileName = md5(time() . openssl_random_pseudo_bytes(16)); 
    } 

    public function get($query, $path, $type) 
    { 
     $arr = []; 
     $sizes = getImageSizes($type); 

     $picture = $query->picture()->first(); 

     if ($picture) { 
      foreach ($sizes as $key => $size) { 
       $url = filter_var($picture->file, FILTER_VALIDATE_URL) ? 
        $picture->file : 
        Storage::url($path . $key . '/' . $picture->file); 

       $arr = array_add($arr, $key, $url); 
      } 
     } 

     return $arr; 
    } 
?> 

我已经加我composer.json:

"autoload": { 
"classmap": [ 
    "database/seeds", 
    "database/factories", 
    "app/Helpers/helpers.php" 
], 
"psr-4": { 
    "App\\": "app/" 
}, 
"files": [ 
    "app/Helpers/helpers.php" 
] 
}, 

当我运行这段代码: image_helper()->get($query, '/images');

ReflectionException (-1) Class image_helper does not exist

已经做composer dump-autoloadphp artisan optimize它仍然是一样的。

以前在Laravel 5.4上它完美的工作。

任何解决方案?

回答

0

问题是在这条线在您的辅助函数:

$image = app('image_helper');

它改成这样:

$image = app('App\Helpers\ImageHelper');

而现在它的工作

+1

'的应用程序(ImageHelper ::类)'是平等的,但在我看来看起来更好 – mimo