本文整理汇总了PHP中Magento\Framework\Controller\Result\JsonFactory::create方法的典型用法代码示例。如果您正苦于以下问题:PHP JsonFactory::create方法的具体用法?PHP JsonFactory::create怎么用?PHP JsonFactory::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Framework\Controller\Result\JsonFactory
的用法示例。
在下文中一共展示了JsonFactory::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Check whether vat is valid
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$result = $this->_validate();
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(['valid' => (int) $result->getIsValid(), 'message' => $result->getRequestMessage()]);
}
示例2: execute
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$postItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
return $resultJson->setData(['messages' => [__('Please correct the data sent.')], 'error' => true]);
}
foreach (array_keys($postItems) as $groupId) {
/** @var \Ves\Brand\Model\Group $group */
$group = $this->_objectManager->create('Ves\\Brand\\Model\\Group');
$groupData = $postItems[$groupId];
try {
$group->load($groupId);
$group->setData(array_merge($group->getData(), $groupData));
$group->save();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$messages[] = $this->getErrorWithgroupId($group, $e->getMessage());
$error = true;
} catch (\RuntimeException $e) {
$messages[] = $this->getErrorWithgroupId($group, $e->getMessage());
$error = true;
} catch (\Exception $e) {
$messages[] = $this->getErrorWithgroupId($group, __('URL key already exists.'));
$error = true;
}
}
return $resultJson->setData(['messages' => $messages, 'error' => $error]);
}
示例3: execute
/**
* Add comment to creditmemo history
*
* @return \Magento\Framework\Controller\Result\Raw|\Magento\Framework\Controller\Result\Json
*/
public function execute()
{
try {
$this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id'));
$data = $this->getRequest()->getPost('comment');
if (empty($data['comment'])) {
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a comment.'));
}
$this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
$this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
$this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
$this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
$creditmemo = $this->creditmemoLoader->load();
$comment = $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
$comment->save();
$this->creditmemoCommentSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']);
$resultPage = $this->resultPageFactory->create();
$response = $resultPage->getLayout()->getBlock('creditmemo_comments')->toHtml();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$response = ['error' => true, 'message' => $e->getMessage()];
} catch (\Exception $e) {
$response = ['error' => true, 'message' => __('Cannot add new comment.')];
}
if (is_array($response)) {
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData($response);
return $resultJson;
} else {
$resultRaw = $this->resultRawFactory->create();
$resultRaw->setContents($response);
return $resultRaw;
}
}
示例4: execute
/**
* Global Search Action
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$items = [];
if (!$this->_authorization->isAllowed('Magento_Backend::global_search')) {
$items[] = ['id' => 'error', 'type' => __('Error'), 'name' => __('Access Denied'), 'description' => __('You need more permissions to do this.')];
} else {
if (empty($this->_searchModules)) {
$items[] = ['id' => 'error', 'type' => __('Error'), 'name' => __('No search modules were registered'), 'description' => __('Please make sure that all global admin search modules are installed and activated.')];
} else {
$start = $this->getRequest()->getParam('start', 1);
$limit = $this->getRequest()->getParam('limit', 10);
$query = $this->getRequest()->getParam('query', '');
foreach ($this->_searchModules as $searchConfig) {
if ($searchConfig['acl'] && !$this->_authorization->isAllowed($searchConfig['acl'])) {
continue;
}
$className = $searchConfig['class'];
if (empty($className)) {
continue;
}
$searchInstance = $this->_objectManager->create($className);
$results = $searchInstance->setStart($start)->setLimit($limit)->setQuery($query)->load()->getResults();
$items = array_merge_recursive($items, $results);
}
}
}
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($items);
}
示例5: execute
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$postItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
return $resultJson->setData(['messages' => [__('Please correct the data sent.')], 'error' => true]);
}
foreach (array_keys($postItems) as $tagId) {
/** @var \Mageplaza\Blog\Model\Tag $tag */
$tag = $this->tagFactory->create()->load($tagId);
try {
$tagData = $postItems[$tagId];
//todo: handle dates
$tag->addData($tagData);
$tag->save();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$messages[] = $this->getErrorWithTagId($tag, $e->getMessage());
$error = true;
} catch (\RuntimeException $e) {
$messages[] = $this->getErrorWithTagId($tag, $e->getMessage());
$error = true;
} catch (\Exception $e) {
$messages[] = $this->getErrorWithTagId($tag, __('Something went wrong while saving the Tag.'));
$error = true;
}
}
return $resultJson->setData(['messages' => $messages, 'error' => $error]);
}
示例6: aroundExecute
/**
* @param \Magento\Customer\Controller\Ajax\Login $subject
* @param \Closure $proceed
* @return $this
* @throws \Zend_Json_Exception
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function aroundExecute(\Magento\Customer\Controller\Ajax\Login $subject, \Closure $proceed)
{
$captchaFormIdField = 'captcha_form_id';
$captchaInputName = 'captcha_string';
/** @var \Magento\Framework\App\RequestInterface $request */
$request = $subject->getRequest();
$loginParams = [];
$content = $request->getContent();
if ($content) {
$loginParams = \Zend_Json::decode($content);
}
$username = isset($loginParams['username']) ? $loginParams['username'] : null;
$captchaString = isset($loginParams[$captchaInputName]) ? $loginParams[$captchaInputName] : null;
$loginFormId = isset($loginParams[$captchaFormIdField]) ? $loginParams[$captchaFormIdField] : null;
foreach ($this->formIds as $formId) {
$captchaModel = $this->helper->getCaptcha($formId);
if ($captchaModel->isRequired($username) && !in_array($loginFormId, $this->formIds)) {
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(['errors' => true, 'message' => __('Provided form does not exist')]);
}
if ($formId == $loginFormId) {
$captchaModel->logAttempt($username);
if (!$captchaModel->isCorrect($captchaString)) {
$this->sessionManager->setUsername($username);
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(['errors' => true, 'message' => __('Incorrect CAPTCHA')]);
}
}
}
return $proceed();
}
示例7: execute
/**
* Delete file from media storage
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
try {
if (!$this->getRequest()->isPost()) {
throw new \Exception('Wrong request.');
}
$files = $this->getRequest()->getParam('files');
/** @var $helper \Magento\Cms\Helper\Wysiwyg\Images */
$helper = $this->_objectManager->get('Magento\\Cms\\Helper\\Wysiwyg\\Images');
$path = $this->getStorage()->getSession()->getCurrentPath();
foreach ($files as $file) {
$file = $helper->idDecode($file);
/** @var \Magento\Framework\Filesystem $filesystem */
$filesystem = $this->_objectManager->get('Magento\\Framework\\Filesystem');
$dir = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
$filePath = $path . '/' . $file;
if ($dir->isFile($dir->getRelativePath($filePath))) {
$this->getStorage()->deleteFile($filePath);
}
}
return $this->resultRawFactory->create();
} catch (\Exception $e) {
$result = ['error' => true, 'message' => $e->getMessage()];
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($result);
}
}
示例8: executeInternal
/**
* Update items qty action
*
* @return \Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\Result\Raw
*/
public function executeInternal()
{
try {
$this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
$this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
$this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
$this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
$this->creditmemoLoader->load();
$resultPage = $this->resultPageFactory->create();
$response = $resultPage->getLayout()->getBlock('order_items')->toHtml();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$response = ['error' => true, 'message' => $e->getMessage()];
} catch (\Exception $e) {
$response = ['error' => true, 'message' => __('We can\'t update the item\'s quantity right now.')];
}
if (is_array($response)) {
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData($response);
return $resultJson;
} else {
$resultRaw = $this->resultRawFactory->create();
$resultRaw->setContents($response);
return $resultRaw;
}
}
示例9: execute
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$response = new \Magento\Framework\Object();
$response->setError(false);
$attributeCode = $this->getRequest()->getParam('attribute_code');
$frontendLabel = $this->getRequest()->getParam('frontend_label');
$attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
$attributeId = $this->getRequest()->getParam('attribute_id');
$attribute = $this->_objectManager->create('Magento\\Catalog\\Model\\Resource\\Eav\\Attribute')->loadByCode($this->_entityTypeId, $attributeCode);
if ($attribute->getId() && !$attributeId) {
if (strlen($this->getRequest()->getParam('attribute_code'))) {
$response->setAttributes(['attribute_code' => __('An attribute with this code already exists.')]);
} else {
$response->setAttributes(['attribute_label' => __('Attribute with the same code (%1) already exists.', $attributeCode)]);
}
$response->setError(true);
}
if ($this->getRequest()->has('new_attribute_set_name')) {
$setName = $this->getRequest()->getParam('new_attribute_set_name');
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
$attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set');
$attributeSet->setEntityTypeId($this->_entityTypeId)->load($setName, 'attribute_set_name');
if ($attributeSet->getId()) {
$setName = $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($setName);
$this->messageManager->addError(__('Attribute Set with name \'%1\' already exists.', $setName));
$layout = $this->layoutFactory->create();
$layout->initMessages();
$response->setError(true);
$response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml());
}
}
return $this->resultJsonFactory->create()->setJsonData($response->toJson());
}
示例10: execute
/**
* Add attribute to attribute set
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$request = $this->getRequest();
$resultJson = $this->resultJsonFactory->create();
try {
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute')->load($request->getParam('attribute_id'));
$attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->load($request->getParam('template_id'));
/** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection $attributeGroupCollection */
$attributeGroupCollection = $this->_objectManager->get('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Group\\Collection');
$attributeGroupCollection->setAttributeSetFilter($attributeSet->getId());
$attributeGroupCollection->addFilter('attribute_group_code', $request->getParam('group'));
$attributeGroupCollection->setPageSize(1);
$attributeGroup = $attributeGroupCollection->getFirstItem();
$attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet();
$attribute->setAttributeSetId($request->getParam('template_id'))->setAttributeGroupId($attributeGroup->getId())->setSortOrder('0')->save();
$resultJson->setJsonData($attribute->toJson());
} catch (\Exception $e) {
$response = new \Magento\Framework\DataObject();
$response->setError(false);
$response->setMessage($e->getMessage());
$resultJson->setJsonData($response->toJson());
}
return $resultJson;
}
示例11: executeInternal
/**
* Attributes validation action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function executeInternal()
{
$response = $this->_objectManager->create('Magento\\Framework\\DataObject');
$response->setError(false);
$attributesData = $this->getRequest()->getParam('attributes', []);
$data = $this->_objectManager->create('Magento\\Catalog\\Model\\Product');
try {
if ($attributesData) {
foreach ($attributesData as $attributeCode => $value) {
$attribute = $this->_objectManager->get('Magento\\Eav\\Model\\Config')->getAttribute('catalog_product', $attributeCode);
if (!$attribute->getAttributeId()) {
unset($attributesData[$attributeCode]);
continue;
}
$data->setData($attributeCode, $value);
$attribute->getBackend()->validate($data);
}
}
} catch (\Magento\Eav\Model\Entity\Attribute\Exception $e) {
$response->setError(true);
$response->setAttribute($e->getAttributeCode());
$response->setMessage($e->getMessage());
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$response->setError(true);
$response->setMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while updating the product(s) attributes.'));
$layout = $this->layoutFactory->create();
$layout->initMessages();
$response->setError(true);
$response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml());
}
return $this->resultJsonFactory->create()->setJsonData($response->toJson());
}
示例12: execute
/**
* @return mixed
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$postItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
return $resultJson->setData(['messages' => [__('Please correct the data sent.')], 'error' => true]);
}
foreach (array_keys($postItems) as $articleId) {
$article = $this->articleFactory->create()->load($articleId);
try {
$articleData = $this->filterData($postItems[$articleId]);
$article->addData($articleData);
$article->save();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$messages[] = $this->getErrorWithArticleId($article, $e->getMessage());
$error = true;
} catch (\RuntimeException $e) {
$messages[] = $this->getErrorWithArticleId($article, $e->getMessage());
$error = true;
} catch (\Exception $e) {
$messages[] = $this->getErrorWithArticleId($article, __('Something went wrong while saving the page.'));
$error = true;
}
}
return $resultJson->setData(['messages' => $messages, 'error' => $error]);
}
示例13: execute
/**
* {@inheritDoc}
*/
public function execute()
{
$this->initLayer();
$items = $this->getItems();
$result = $this->jsonResultFactory->create()->setData($items);
return $result;
}
示例14: execute
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
if ($this->getRequest()->getParam('isAjax')) {
$postItems = $this->getRequest()->getParam('items', []);
if (!count($postItems)) {
$messages[] = __('Please correct the data sent.');
$error = true;
} else {
foreach (array_keys($postItems) as $blockId) {
/** @var \Magento\Cms\Model\Block $block */
$block = $this->blockRepository->getById($blockId);
try {
$block->setData(array_merge($block->getData(), $postItems[$blockId]));
$this->blockRepository->save($block);
} catch (\Exception $e) {
$messages[] = $e->getMessage();
// $this->getErrorWithBlockId(
// $block,
// __($e->getMessage())
// );
$error = true;
}
}
}
}
return $resultJson->setData(['messages' => $messages, 'error' => $error]);
}
示例15: execute
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$postItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
return $resultJson->setData(['messages' => [__('Please correct the data sent.')], 'error' => true]);
}
foreach (array_keys($postItems) as $pageId) {
/** @var \Magento\Cms\Model\Page $page */
$page = $this->pageRepository->getById($pageId);
try {
$pageData = $this->filterPost($postItems[$pageId]);
$this->validatePost($pageData, $page, $error, $messages);
$extendedPageData = $page->getData();
$this->setCmsPageData($page, $extendedPageData, $pageData);
$this->pageRepository->save($page);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$messages[] = $this->getErrorWithPageId($page, $e->getMessage());
$error = true;
} catch (\RuntimeException $e) {
$messages[] = $this->getErrorWithPageId($page, $e->getMessage());
$error = true;
} catch (\Exception $e) {
$messages[] = $this->getErrorWithPageId($page, __('Something went wrong while saving the page.'));
$error = true;
}
}
return $resultJson->setData(['messages' => $messages, 'error' => $error]);
}