Laravel 包引入详解

Vendor包

    • Vendor包
      • 模板vendor中自带的包
      • 自己用composer安装的包
      • 其它相对规范的包
      • 只在本地使用的包

找了很多资料,都没有怎么比较详细的说明vendor中的包怎么用,自己试验总结了一下,在此说明。

模板vendor中自带的包

  • 类似于laravel,composer之类的包,模板已经把Provider,aliases之类的写好了,可以直接用,在此不详说了。

  • 别名设置(aliases)在config/app.php中可以找到

自己用composer安装的包

  • 比如在项目中我想用一个封装好的curl类,比如anlutro/curl,那么我们用composer安装好之后,要顺利的用这个类,需要在config/app.php中的aliases数组加上* ‘cURL’ => ‘anlutro\cURL\Laravel\cURL’ *

为什么要这么设置呢,其实涉及到antoload中的psr-4规范,可以看看该例子中的anlutro文件夹中的composer.json

"autoload": {
        "psr-4": {
            "anlutro\\cURL\\": "src/"
        }
    },
  • 这里就说明anlutro\cURL 相当于访问其下的src/下的子目录,所以anlutro\cURL\Laravel\cURL其实就相当于引用anlutro文件夹下的src文件夹下的Laravel文件夹下的cURL.php文件
/**
 * PHP OOP cURL
 * 
 * @author   Andreas Lutro @gmail.com>
 * @license  http://opensource.org/licenses/MIT
 * @package  PHP cURL
 */

namespace anlutro\cURL\Laravel;

use Illuminate\Support\Facades\Facade;

/**
 * cURL facade class.
 */
class cURL extends Facade
{
    public static function getFacadeAccessor()
    {
        return 'anlutro\cURL\cURL';
    }
}

看到这里的代码就不难理解了

  • 在代码中只要use anlutro\cURL\Laravel\cURL即可使用已经定义好的aliases了

其它相对规范的包

  • 这里举一个七牛云的例子

  • 这些包都是非常符合Laravel的依赖注入的思想的,拥有一个服务提供者的类

 namespace zgldh\QiniuStorage;
use League\Flysystem\Filesystem;
use Illuminate\Support\ServiceProvider;
use zgldh\QiniuStorage\Plugins\DownloadUrl;
use zgldh\QiniuStorage\Plugins\ImageExif;
use zgldh\QiniuStorage\Plugins\ImageInfo;
use zgldh\QiniuStorage\Plugins\ImagePreviewUrl;
use zgldh\QiniuStorage\Plugins\PersistentFop;
use zgldh\QiniuStorage\Plugins\PersistentStatus;
use zgldh\QiniuStorage\Plugins\PrivateDownloadUrl;
use zgldh\QiniuStorage\Plugins\UploadToken;
use zgldh\QiniuStorage\Plugins\PrivateImagePreviewUrl;
use zgldh\QiniuStorage\Plugins\VerifyCallback;
class QiniuFilesystemServiceProvider extends ServiceProvider
{
    public function boot()
    {
        \Storage::extend(
            'qiniu',
            function ($app, $config) {
                if (isset($config['domains'])) {
                    $domains = $config['domains'];
                } else {
                    $domains = [
                        'default' => $config['domain'],
                        'https'   => null,
                        'custom'  => null
                    ];
                }
                $qiniu_adapter = new QiniuAdapter(
                    $config['access_key'],
                    $config['secret_key'],
                    $config['bucket'],
                    $domains,
                    $config['notify_url']?$config['notify_url']:null
                );
                $file_system = new Filesystem($qiniu_adapter);
                $file_system->addPlugin(new PrivateDownloadUrl());
                $file_system->addPlugin(new DownloadUrl());
                $file_system->addPlugin(new ImageInfo());
                $file_system->addPlugin(new ImageExif());
                $file_system->addPlugin(new ImagePreviewUrl());
                $file_system->addPlugin(new PersistentFop());
                $file_system->addPlugin(new PersistentStatus());
                $file_system->addPlugin(new UploadToken());
                $file_system->addPlugin(new PrivateImagePreviewUrl());
                $file_system->addPlugin(new VerifyCallback());
                return $file_system;
            }
        );
    }
    public function register()
    {
        //
    }
}
  • 可以看到一个事件监听器,用于执行注册后的服务,所以我们根据手册就可以知道要在app.php中的Provider数组中注册提供者,于是就有了官网上的zgldh\QiniuStorage\QiniuFilesystemServiceProvider
"autoload": {
        "psr-4": {
            "zgldh\\QiniuStorage\\": "src/"
        }
    }
  • 同理这里有着psr-4规范,所以自己建包也可以按照这种思路来

只在本地使用的包

  • 我们这以laracasts/generators,应该很多Laravel开发者都用过,非常的好用,这个包的特点就是丰富php artisan的基本功能,如果产品上线,肯定就不需要这个功能了,所以按照官方的配置
public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
    }
}
  • 区别就是这里并不是在config/app.php中配置了,而是在app/Providers/AppAppServiceProvider.php中配置了,我们注意看config/app.php的providers数组下也有引入这个文件的注册信息
/*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
  • 对于这种包用这种处理方式处理就很棒,类似的包还有”sven/artisan-view”(生成视图文件的包)

大致就是这样,作者学Laravel时间也不算长,所以有问题还请大神指出,因为以上都是根据包特点看源码分析出来的,难免有考虑不周之处,更多请看博客

                                                        by vampirebitter

你可能感兴趣的:(php)