PHP code example of locaine / lcn-image-uploader-bundle
1. Go to this page and download the library: Download locaine/lcn-image-uploader-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
locaine / lcn-image-uploader-bundle example snippets
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Lcn\ImageUploaderBundle\LcnImageUploaderBundle(),
);
// ...
}
// ...
}
class Demo implements \Lcn\ImageUploaderBundle\Entity\ImageGallery {
...
/**
* Return the relative path to the directory
* where the image uploads should be stored.
*
* The path should be relative to the directory defined
* in parameter "lcn_file_uploader.file_base_path"
*
* @return String
*/
public function getImageGalleryUploadPath() {
$id = $this->getId();
//
namespace Lcn\ImageUploaderBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class DemoController extends Controller
{
...
/**
* Edit Uploads for the given entity id or create new entity with uploads.
*
* In a real world scenario you might want to check edit permissions
*
* @param Request $request
* @param $entityId
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, $entityId)
{
$entity = new Demo($entityId); //in a real world scenario you would retrieve the entity from a repository.
$galleryName = 'demo'; //the galleryName has to match a defined gallery in parameter "lcn.image_uploader.galleries"
$imageUploader = $this->get('lcn.image_uploader');
$form = $this->createFormBuilder()
->setAction($this->generateUrl('lcn_image_uploader_demo_edit', array('entityId' => $entity->getId())))
->setMethod('POST')
->getForm();
if ($request->getMethod() == 'POST') {
$form->submit($request);
if ($form->isValid()) {
$imageUploader->syncFromTemp($entity, $galleryName);
return $this->redirect($this->generateUrl('lcn_image_uploader_demo_show', array('entityId' => $entity->getId())));
}
} else {
$imageUploader->syncToTemp($entity, $galleryName);
}
return $this->render('LcnImageUploaderBundle:Demo:edit.html.twig', array(
'entity' => $entity,
'galleryName' => $galleryName,
'uploadUrl' => $this->generateUrl('lcn_image_uploader_demo_handle_file_upload', array('entityId' => $entity->getId())),
'form' => $form->createView(),
));
}
/**
* Store the uploaded file.
*
* In a real world scenario you might probably want to check
* if the user is allowed to store uploads for the given entity id.
*
* Delegates to LcnImageUploader which implements a REST Interface and handles file uploads as well as file deletions.
*
* This action must not return a response. The response is generated in native PHP by LcnFileUploader.
*
* @param Request $request
* @param int $userId
*/
public function handleFileUploadAction(Request $request, $entityId)
{
$entity = new Demo($entityId); //in a real world scenario you would retrieve the entity from a repository.
$galleryName = 'demo'; //the galleryName has to match a defined gallery in parameter "lcn.image_uploader.galleries"
$this->get('lcn.image_uploader')->handleFileUpload($entity, $galleryName);
}
...
}