Laravel 5 - 存储适配器扩展不起作用

时间:2015-03-20 13:43:29

标签: php laravel ftp

出于某种原因,我不能像文档中的示例一样扩展存储类。

我一直在尝试创建一个FTP适配器,因为它是我正在处理的项目所必需的。

这是我创建的服务提供商

<?php namespace herbals\Providers;

use Storage;
use Illuminate\Filesystem\FilesystemServiceProvider as ServiceProvider;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FTPAdapter;

class FTPFileSystemServiceProvider extends ServiceProvider {

public function boot()
{
    Storage::extend('ftp', function($app, $config)
    {
        $client = new FTPAdapter(array(
            'host' => env('FTP_HOST') || '',
            'username' => env('FTP_USERNAME') || '',
            'password' => env('FTP_PASSWORD') || '',

            'port' => env('FTP_PORT') || '',
            'root' => env('FTP_ROOT') || '/',
            'passive' => true,
            'ssl' => true,
            'timeout' => 30,
        ));
        $filesystem = new Filesystem($client);
        return $filesystem;
    });
}

public function register()
{
    //
}

}

这是我一直得到的错误

ErrorException in FilesystemManager.php line 232:
call_user_func_array() expects parameter 1 to be a valid callback,     class 'Illuminate\Filesystem\FilesystemAdapter' does not have a method   'createDriver'
in FilesystemManager.php line 232
at HandleExceptions->handleError('2', 'call_user_func_array() expects     parameter 1 to be a valid callback, class    'Illuminate\Filesystem\FilesystemAdapter' does not have a method 'createDriver'',    '/home/vagrant/Code/herbals/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php', '232', array('method' => 'createDriver',   'parameters' => array(null)))
at call_user_func_array(array(object(FilesystemAdapter),    'createDriver'), array(null)) in FilesystemManager.php line 232
at FilesystemManager->__call('createDriver', array(null)) in FilesystemManager.php line 97
at FilesystemManager->createDriver(null) in FilesystemManager.php line 97
at FilesystemManager->resolve('ftp') in FilesystemManager.php line 79
at FilesystemManager->get('ftp') in FilesystemManager.php line 68
at FilesystemManager->disk('ftp') in Facade.php line 210
at Facade::__callStatic('disk', array('ftp')) in routes.php line 26
at Storage::disk('ftp') in routes.php line 26

1 个答案:

答案 0 :(得分:3)

我认为你在这里犯了一个小错误,确保你在/config/filesystem.php中有ftp驱动程序及其详细信息(主机,用户名,密码),并使用$ config [&#39]在你的提供程序中引用它们;主持人&#39;等等......

使用api一段时间后,我能够弄明白,现在按照以下说明操作:在/config/filesystem.php中,添加新的驱动程序设置,如:

'ftp' => [
             'driver'   => 'ftp',
             'host'     =>'ftp-host',
             'username' => 'ftp-username',
             'password' => 'ftp-pass',
             'port'     => 21,
             'ssl'      => false,
        ],

接下来,我们将在/ app / Providers中创建一个Ftp Filemanager驱动程序服务提供程序,保存文件FtpFilesystemServiceProvider.php。以下是我的样子

<?php 

namespace App\Providers;

use Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FtpAdapter;
use Illuminate\Support\ServiceProvider as ServiceProvider;

    class FtpFilesystemServiceProvider extends  ServiceProvider {

        //boot 
        public function boot()
        {
        Storage::extend('ftp',function($app, $config){

        //init the client 
        $client = new FtpAdapter($config);

        //lets now call the obj 
        $ftpFileSystem = new Filesystem($client);

        //return 
        return $ftpFileSystem;
        }); 

        }//end boot 


         public function register()
        {
            //
        }


    }

之后我们将编辑/config/app.php并将服务提供者添加到providers数组中,以便您将以下代码添加到提供者列表中:

'App\Providers\FtpFilesystemServiceProvider',

我们完成了,你要离开了,你可以像这样打电话给存储

Storage::disk("ftp");

或将ftp设置为默认驱动程序,....