How can I access Cloudinary's helper method in Symfony2 Controller?

579 Views Asked by At

I'm using Cloudinary to manage direct image uploads in my Sf2 application.

There's a Cloudinary helper function called "cl_image_upload_tag" that generates the upload form.

I need to run the function in my controller and display the results as raw code in my template. But I'm not able to access the function in my Controller

$cloud_form = \Cloudinary\Uploader::cl_image_upload_tag('image_id', array("callback" => $this->get("router")->generate("cloudinary_callback")));

(I will output $cloud_form in Twig as {{ cloud_form|raw }} )

I think this is a namespace issue but I can't make it work, it get "Error: Call to undefined method". Thanks!

Update: here's the autoload file:

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/**
 * @var $loader ClassLoader
 */
$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;
2

There are 2 best solutions below

0
On

Try to modify your app/autoloader.php file as follows

$loader->registerNamespaces(array(
    'Cloudinary' => __DIR__.'/../vendor/path/to/Cloudinary/source/file',
));

From now on you should be able to refer to your class as Cloudinary

use Cloudinary; // example
use Cloudinary\Uploader; //other example
4
On

cl_image_upload_tag isn't a method of the Uploader class, it's just a function defined in Uploader.php, which is the same file that holds the Uploader class.

Assuming that you include Uploader.php at the top of you controller, you can just call the function directly:

$cloud_form = \cl_image_upload_tag('image_id', array("callback" => $this->get("router")->generate("cloudinary_callback")));

If you are using composer to vendor the Cloudinary package, Composer may take care of adding those functions to your application without the need to include the file in your controller.