Upload images through own Controller
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
-
- Posts: 26
- Joined: 22 Jun 2015, 09:12
Upload images through own Controller
Hello, and sorry to bother you once again!
I'm trying to upload images through my own controller and my custom admin, so far I'm not having luck with getting the images uploaded. I found a _createItem() function, but I don't think I can access this through my custom Controller.
here's a bit of code:
Could you maybe point me in the right direction?
Thanks a lot!
I'm trying to upload images through my own controller and my custom admin, so far I'm not having luck with getting the images uploaded. I found a _createItem() function, but I don't think I can access this through my custom Controller.
here's a bit of code:
Code: Select all
$mediaManager = \MShop_Factory::createManager($context, 'media');
$mediaItem = $mediaManager->createItem(array());
$img = $request->file('image');
$mediaItem->setTypeId(1); //Product Type
$mediaItem->setDomain('product');
$mediaItem->setUrl($img);
$mediaItem->setPreview($img);
$mediaItem->setStatus($request->get('status'));
$mediaItem->setMimeType($img->getMimeType());
$mediaManager->saveItem($mediaItem, true);
Thanks a lot!
Re: Upload images through own Controller
Don't excuse yourself, it's nice to see that you are still working on your project and already achieved so much!Bananamoon wrote:Hello, and sorry to bother you once again!
If this is all code, you forgot to move your uploaded file to the new directory (somewhere in ./public/) first:Bananamoon wrote: I'm trying to upload images through my own controller and my custom admin, so far I'm not having luck with getting the images uploaded. I found a _createItem() function, but I don't think I can access this through my custom Controller.
Code: Select all
$mediaManager = \MShop_Factory::createManager($context, 'media'); $mediaItem = $mediaManager->createItem(array()); $img = $request->file('image'); $mediaItem->setTypeId(1); //Product Type $mediaItem->setDomain('product'); $mediaItem->setUrl($img); $mediaItem->setPreview($img); $mediaItem->setStatus($request->get('status')); $mediaItem->setMimeType($img->getMimeType()); $mediaManager->saveItem($mediaItem, true);
Code: Select all
$request->file('image')->move($destinationPath, $fileName);
Some more hints: createItem() accepts no parameters and the second parameter of saveItem() is true by default. To retrieve the dynamic type ID for any items, you should use this code instead:
Code: Select all
/**
* Returns the attribute type item specified by the code.
*
* @param string $prefix Domain prefix for the manager, e.g. "media/type"
* @param string $domain Domain of the type item
* @param string $code Code of the type item
* @return MShop_Common_Item_Type_Interface Type item
* @throws Exception If no item is found
*/
protected function getTypeItem( $prefix, $domain, $code )
{
$manager = MShop_Factory::createManager( $this->_getContext(), $prefix ); // you need the context from somewhere
$prefix = str_replace( '/', '.', $prefix );
$search = $manager->createSearch();
$expr = array(
$search->compare( '==', $prefix . '.domain', $domain ),
$search->compare( '==', $prefix . '.code', $code ),
);
$search->setConditions( $search->combine( '&&', $expr ) );
$result = $manager->searchItems( $search );
if( ( $item = reset( $result ) ) === false ) {
throw new Exception( sprintf( 'No type item for "%1$s/%2$s" in "%3$s" found', $domain, $code, $prefix ) );
}
return $item;
}
Code: Select all
getTypeItem( 'media/type', 'product', 'default' )->getId();
-
- Posts: 26
- Joined: 22 Jun 2015, 09:12
Re: Upload images through own Controller
Superb! Thanks for the help