从控制器传递参数到Cakephp中的Compnent

问题描述:

我已经给了一个任务来维护一个cakephp应用程序。将文件上传到服务器存在问题。所有这些时间我们都在使用AWS S3 Bucket,现在我们要将它上传到我们自己的文件服务器。编码部分由我的前同事完成。从控制器传递参数到Cakephp中的Compnent

我正在尝试一个简单的东西,我想从我的控制器发送fileName到S3组件文件。

压缩编码做的是这样的:

$this->S3->uploadFile($this->request->data('upload.tmp_name'),$fileKey); 

在S3Component文件:

我写了下面的:

public function uploadFile($filePath, $fileKey, $metaData = []) 
    { 
      $fileUtility = new FileUtility(1024 * 1024, array("ppt", "pdf")); 
      return $fileUtility->uploadFile($_FILES[$fileKey], $filePath); 
    } 

现在我该怎样打发值S3- > uploadFile正确反映S3Compontent文件的uploadfile函数。

谢谢!

如果您对重构感到困扰,您可以尝试Flysystem,这是一个可通过作曲者安装的文件管理软件包。 https://flysystem.thephpleague.com/

它的好处在于,您只需将本地适配器,ftp适配器等S3适配器交换(有几个!),并且您的任何代码都不需要更改!

例如:

use Aws\S3\S3Client; 
use League\Flysystem\AwsS3v3\AwsS3Adapter; 
use League\Flysystem\Filesystem; 

$client = S3Client::factory([ 
    'credentials' => [ 
     'key' => 'your-key', 
     'secret' => 'your-secret', 
    ], 
    'region' => 'your-region', 
    'version' => 'latest|version', 
]); 

$adapter = new AwsS3Adapter($client, 'your-bucket-name', 'optional/path/prefix'); 

并创建一个本地适配器:

use League\Flysystem\Filesystem; 
use League\Flysystem\Adapter\Local; 

$adapter = new Local(__DIR__.'/path/to/root'); 
$filesystem = new Filesystem($adapter); 

然后,你可以让相同的呼叫$filesystem这样的:

$filesystem->write('path/to/file.txt', 'contents'); 
$contents = $filesystem->read('path/to/file.txt'); 
$filesystem->delete('path/to/file.txt'); 
$filesystem->copy('filename.txt', 'duplicate.txt'); 

时退房API在这里:https://flysystem.thephpleague.com/api/