To integrate Filepicker with Zend Framework 2 follow these steps:
Copy the assets
folder to your ZF2 public
directory and the uploader
folder into the root directory.
Edit your composer.json
file and add the following line to the psr-4
autoload:
"Hazzard\\Filepicker\\": "uploader/src"
It should look like this.
Then run:
composer require hazzard/config
composer require intervention/image
composer require symfony/http-foundation ~2.7
composer dumpautoload
Create a FilepickerController
controller and add:
<?php
namespace Application\Controller;
use Hazzard\Filepicker\Handler;
use Hazzard\Filepicker\Uploader;
use Intervention\Image\ImageManager;
use Hazzard\Config\Repository as Config;
use Symfony\Component\HttpFoundation\Request;
use Zend\Mvc\Controller\AbstractActionController;
class FilepickerController extends AbstractActionController
{
public function indexAction()
{
$basepath = $this->getServiceLocator()
->get('ViewHelperManager')->get('basepath');
$handler = new Handler(
new Uploader($config = new Config, new ImageManager)
);
$config['debug'] = true;
// Path to the files directory (public/files).
$config['upload_dir'] = 'public/files';
// Url to the files directory.
$config['upload_url'] = $basepath('files');
$handler->handle()->send();
return $this->getResponse();
}
}
In your module.config.php
file, add to the controllers > invokables
array:
'Application\Controller\Filepicker' => Controller\FilepickerController::class,
And under the routes
array add:
'filepicker' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/filepicker',
'defaults' => array(
'controller' => 'Application\Controller\Filepicker',
'action' => 'index',
),
),
),
Notice: Make sure you use your proper namespace for the controller.
Then in your view make sure to set the url option to your controller route, like this:
url: '<?php echo $this->url("filepicker") ?>',
If you are using CSRF validation, make sure to set the token using the data option:
data: {
csrf: '<?php echo $csrf->getValue(); ?>', // $csrf -> Zend\Form\Element\Csrf
}