简体   繁体   English

Laravel 5.5自定义帮助程序文件类变为不存在

[英]Laravel 5.5 custom helper file Class become doesn't exists

This is my code: 这是我的代码:

//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;
    }
?>

I've already add at my composer.json: 我已经在我的composer.json中添加了:

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

When I run this code: image_helper()->get($query, '/images'); 当我运行此代码时: image_helper()->get($query, '/images');

I got ReflectionException (-1) Class image_helper does not exist 我得到了ReflectionException (-1) Class image_helper does not exist

Already do composer dump-autoload , php artisan optimize it still the same. 已经做过composer dump-autoloadphp artisan optimize

Previously on Laravel 5.4 it works perfectly. 以前在Laravel 5.4上可以完美地工作。

Any solution? 有什么办法吗?

Problem is on this line in your helper function: 问题出在您的辅助函数中:

$image = app('image_helper');

Change it to this: 更改为此:

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

And now its working 现在它的工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM