2017 © Pedro Peláez
 

library uploadcare-symfony2

Official Uploadcare package for Symfony2

image

uploadcare/uploadcare-symfony2

Official Uploadcare package for Symfony2

  • Thursday, August 15, 2013
  • by grayhound
  • Repository
  • 7 Watchers
  • 0 Stars
  • 74 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

Uploadcare Symfony2 Bundle

This is a bundle for [Symfony2][5] to work with Uploadcare, (*1)

It's based on a uploadcare-php library., (*2)

Requirements

  • Symfony 2.1+
  • PHP 5.3+
  • php-curl

Install

GitHub

Clone bundle from git to your vendor directory:, (*3)

git clone git://github.com/uploadcare/uploadcare-symfony2.git vendor/uploadcare/uploadcare-symfony2 --recursive

Composer

Update "require" section inside your composer.json:, (*4)

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

Now tell composer to download package:, (*5)

php composer.phar update

Add fetch git submodules:, (*6)

cd vendor/uploadcare/uploadcare-symfony2 && git init && git update 

Edit your app/autoload.php. Add this:, (*7)

$loader->add('Uploadcare', __DIR__.'/../vendor/uploadcare/uploadcare-symfony2/uploadcare/src');

Inside your app/config/config.yml add:, (*8)

parameters:
    uploadcare.public_key: demopublickey
    uploadcare.secret_key: demoprivatekey

services:
    uploadcare:
      class: Uploadcare\UploadcareBundle\UploadcareSymfony
      arguments: [%uploadcare.public_key%, %uploadcare.secret_key%]

This will add Uploadcare Bundle as a service., (*9)

Usage

You can access this service inside a controller like this:, (*10)

$this->get('uploadcare');

It will return a UploadcareSymfony object. This class extends Uploadcare\Api class., (*11)

Create some entity:, (*12)

namespace UploadcareTest\UploadcareTestBundle\Entity;

class UCFile
{
  public $file_id;
}

Create form inside your controller:, (*13)

namespace UploadcareTest\UploadcareTestBundle\Controller;

use Uploadcare\UploadcareBundle\Form\Type\UploadcareWidgetType;

public function indexAction()
{
    $uc_file = new UCFile();
    $form = $this->createFormBuilder($uc_file)
                 ->add('file_id', new UploadcareWidgetType())
                 ->getForm();

    return $this->render('UploadcareTestBundle:Default:index.html.twig', array(
        'form' => $form->createView(),
        'uploadcare' => $this->get('uploadcare'),
    ));
}

You can see a UploadcareWidgetType. It's a hidden field with special parameters to activate widget, (*14)

To display widget and forms create a view for controller:, (*15)

{% extends '::base.html.twig' %}
{% block body %}
<form action="{{ path('uploadcare_test_homepage') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}

    <input type="submit" />
</form>
{% endblock %}
{% block javascripts %}
  {{ uploadcare.widget.getScriptTag|raw }}
{% endblock %}

Now you should be able to see a widget. Just upload some file and submit form., (*16)

To process file edit a controller to look like this:, (*17)

public function indexAction()
{
    $uc_file = new UCFile();
    $form = $this->createFormBuilder($uc_file)
                 ->add('file_id', new UploadcareWidgetType())
                 ->getForm();

    $file = null;
    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
      $form->bind($request);

      if ($form->isValid()) {
        $data = $request->request->get('form');
        $file_id = $data['file_id'];
        $file = $this->get('uploadcare')->getFile($file_id);
        $file->store();
      }
    }

    return $this->render('UploadcareTestBundle:Default:index.html.twig', array(
        'form' => $form->createView(),
        'uploadcare' => $this->get('uploadcare'),
        'file' => $file,
    ));
}

The main part is using a "getFile" method to create "$file" and then "store" method on file., (*18)

By calling "store" method you told Uploadcare to store file and it will be available at CDN., (*19)

You can pass "$file" to the twig template. "$file" is and object of Uploadcare\File., (*20)

With use of this object you can call any operations with file and display the file inside template like this:, (*21)

{{ file.resize(400, 400).getImgTag|raw }}

The Versions

15/08 2013

dev-master

9999999-dev https://github.com/uploadcare/uploadcare-symfony2

Official Uploadcare package for Symfony2

  Sources   Download

The Requires

  • php >=5.3.0

 

15/08 2013

v1.0.1

1.0.1.0 https://github.com/uploadcare/uploadcare-symfony2

Official Uploadcare package for Symfony2

  Sources   Download

The Requires

  • php >=5.3.0