使用$ this-> app-> share()在Laravel 4.1中正确注册服务提供商

时间:2014-09-09 02:12:20

标签: php laravel-4 facade service-provider

我已经写过这个图片上传服务,但问题是我一直收到以下错误。 我已经尝试了一些建议,但我似乎没有克服它。

错误类型ReflectionException,消息类upload.image不存在“,”文件“:”C:\ xampp \ htdocs \ tippy \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php“,第501行

ImageUploadService.php

namespace Tippy\Services\Upload;

use Intervention\Image\Image;

use Illuminate\Filesystem\Filesystem;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class ImageUploadService
{
protected $directory = 'assets/img/uploads/temp';

protected $extension = 'jpg';

protected $size = 160;

protected $quality = 65;

protected $filesystem;

public function __construct(Filesystem $filesystem)
{
    $this->filesystem = $filesystem;
}   

public function enableCORS($origin)
{
    $allowHeaders = [
        'Origin',
        'X-Requested-With',
        'Content-Range',
        'Content-Disposition',
        'Content-Type'
    ];

    header('Access-Control-Allow-Origin: ' . $origin);
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Allow-Headers: ' . implode(', ', $allowHeaders));
}

protected function getFullPath($path)
{
    return  public_path() . '/' . $path;
}
protected function makeFileName()
{
    return Sha1(time() . time()) . '.{$this->extension}';
}

protected function getFile($path)
{
    $this->filesytem->get($path);
}

protected function getFileSize($path)
{
    return $this->filesytem->size($path);
}

protected function getDataUrl($mime, $path)
{   
    $base = base64_encode($this->getFile($path));

    return 'data:' .  $mime . ';base64,'  $base;
}

protected function getJsonBody($filename, $mime, $path)
{
    return [
        'images' => [
            'filename'  => $filename,
            'mime'      => $mime,
            'size'      => $this->getFileSize($path),
            'dataURL'   => $this->getDataUrl($mime, $path)
        ]
    ];
}

public function handle(UploadedFile $file)
{
    $mime       = $file->getMimeType();
    $filename   = $this->makeFileName();
    $path       = $this->getFullPath($this->directory . '/' . $filename);

    $success = Image::make($file->getRealPath())
                    ->resize($this->size, $this->size, true, false)
                    ->save($path, $this->quality);

    if (! $success) {
        return false;
    }

    return $this->getJsonBody($filename, $mime, $path);
}
}

这是UploadServiceProvider.php

namespace Tippy\Providers;

use Illuminate\Support\ServiceProvider;

use Tippy\Services\Upload\ImageUploadService;

class UploadServiceProvider extends ServiceProvider
{
public function register()
{
$this->app['upload.image'] = $this->app->share(function ($app) {
return new ImageUploadService($app['files']);
});
}
}

这是我的ImageUpload.php Facade

protected static function getFacadeAccessor()
{
    return 'upload.image';  
}

最后我自动加载了提供商

'Tippy\Providers\UploadServiceProvider',

为这个类别名

'ImageUpload'     => 'Tippy\Facades\ImageUpload',

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在深入了解Facades&的世界之前服务,

你必须看看PSR-0惯例。 PSR-0告诉作曲家如何加载你的课程(外墙,服务和地段)。

链接到有用的资源:

What is PSR-0

Using a PSR-0 Directory Structure

Setup your own library