uploadcare/uploadcare-zend2

此包的最新版本(v1.0.1)没有可用的许可信息。

官方 Uploadcare for Zend Framework 2 包

v1.0.1 2013-08-15 06:16 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:55:34 UTC


README

这是一个用于与 Zend Framework 2 一起使用 Uploadcare 的模块

它基于 uploadcare-php 库。

要求

  • Zend 2.0+
  • PHP 5.3+
  • php-curl

安装

GitHub

从 git 克隆模块到您的 vendor 目录

git clone git://github.com/uploadcare/uploadcare-zend2.git vendor/Uploadcare --recursive

Composer

更新您的 composer.json

"require": {
  "uploadcare/uploadcare-zend2": "dev-master"
}   

安装包

composer update

从 github 获取子模块

cd vendor/uploadcare/uploadcare-zend2/ && git submodule init && git submodule update   

编辑您的 config/application.config.php 并添加新的模块。它应该看起来像这样

return array(
    'modules' => array(
        'Application',
        'Uploadcare',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),  

);

在您的 config/autoload/global.php 中添加

return array(
    'uploadcare' => array(
      'public_key' => 'demopublickey',
      'secret_key' => 'demoprivatekey',
    ),
);

用法

您可以在控制器中这样访问 uploadcare api 服务

$uploadcare = $this->getServiceLocator()->get('uploadcare');

它将返回一个 UploadcareZend 对象。这个类扩展了 Uploadcare\Api 类。

创建一个表单以显示 Uploadcare 小部件。使用 UploadcareInput 类作为字段。

在您的控制器中

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Uploadcare\Form\UploadcareInput;
use Zend\Form\Form;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $uploadcare = $this->getServiceLocator()->get('uploadcare');
        
        //file_id is name of input for widget. 
        //You will recieve File ID at CDN from this field.
        $uploadcare_widget = new UploadcareInput('file_id');
        
        $form = new Form();
        $form->add($uploadcare_widget);
        $form->add(array('name' => 'submit',
          'attributes' => array(
            'type'  => 'submit',
            'value' => 'Upload!'
        )));
        
        return array(
          'form' => $form,
          'uploadcare' => $uploadcare,
        );
    }
}

现在我们可以在视图中显示包含小部件的表单

<?php
$this->form->prepare();

echo $this->uploadcare->widget->getScriptTag();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

"echo $this->uploadcare->widget->getScriptTag();" 将显示您需要的所有 <script> 部分。

现在您可以使用 Uploadcare 小部件上传文件。

让我们处理 file_id 并显示文件。更新您的控制器如下

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $uploadcare = $this->getServiceLocator()->get('uploadcare');

        $uploadcare_widget = new UploadcareInput('file_id');
        
        $form = new Form();
        $form->add($uploadcare_widget);
        $form->add(array('name' => 'submit',
          'attributes' => array(
            'type'  => 'submit',
            'value' => 'Upload!'
        )));
    
        $file = null;
        $request = $this->getRequest();
        if ($request->isPost()) {
          $form->setData($request->getPost()->toArray());
          if ($form->isValid()) {
            $data = $form->getData();
            $file_id = $data['file_id'];
            $file = $uploadcare->getFile($file_id); //get file from API
            $file->store(); //store file
          }
        }
        
        return array(
          'form' => $form,
          'uploadcare' => $uploadcare,
          'file' => $file,
        );
    }
}

现在我们有一个 Uploadcare\File 的对象。让我们在视图中显示它

echo $this->file->scaleCrop(300, 300, true)->getImgTag();