本文整理汇总了PHP中Magento\Framework\Controller\Result\JsonFactory类的典型用法代码示例。如果您正苦于以下问题:PHP JsonFactory类的具体用法?PHP JsonFactory怎么用?PHP JsonFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JsonFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: 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);
}
}
示例3: setUp
protected function setUp()
{
$this->productBuilder = $this->getMock('Magento\\Catalog\\Controller\\Adminhtml\\Product\\Builder', ['build'], [], '', false);
$this->product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['addData', 'getSku', 'getTypeId', 'getStoreId', '__sleep', '__wakeup', 'getAttributes', 'setAttributeSetId'])->getMock();
$this->product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple'));
$this->product->expects($this->any())->method('getStoreId')->will($this->returnValue('1'));
$this->product->expects($this->any())->method('getAttributes')->will($this->returnValue([]));
$this->productBuilder->expects($this->any())->method('build')->will($this->returnValue($this->product));
$this->resultPage = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\Page')->disableOriginalConstructor()->getMock();
$resultPageFactory = $this->getMockBuilder('Magento\\Framework\\View\\Result\\PageFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
$resultPageFactory->expects($this->any())->method('create')->willReturn($this->resultPage);
$this->resultForward = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\Forward')->disableOriginalConstructor()->getMock();
$resultForwardFactory = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\ForwardFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
$resultForwardFactory->expects($this->any())->method('create')->willReturn($this->resultForward);
$this->resultPage->expects($this->any())->method('getLayout')->willReturn($this->layout);
$this->resultRedirectFactory = $this->getMock('Magento\\Backend\\Model\\View\\Result\\RedirectFactory', ['create'], [], '', false);
$this->resultRedirect = $this->getMock('Magento\\Backend\\Model\\View\\Result\\Redirect', [], [], '', false);
$this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect);
$this->initializationHelper = $this->getMock('Magento\\Catalog\\Controller\\Adminhtml\\Product\\Initialization\\Helper', [], [], '', false);
$this->productFactory = $this->getMockBuilder('Magento\\Catalog\\Model\\ProductFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->productFactory->expects($this->any())->method('create')->willReturn($this->product);
$this->resultJson = $this->getMock('Magento\\Framework\\Controller\\Result\\Json', [], [], '', false);
$this->resultJsonFactory = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\JsonFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson);
$additionalParams = ['resultRedirectFactory' => $this->resultRedirectFactory];
$this->action = (new ObjectManagerHelper($this))->getObject('Magento\\Catalog\\Controller\\Adminhtml\\Product\\Validate', ['context' => $this->initContext($additionalParams), 'productBuilder' => $this->productBuilder, 'resultPageFactory' => $resultPageFactory, 'resultForwardFactory' => $resultForwardFactory, 'initializationHelper' => $this->initializationHelper, 'resultJsonFactory' => $this->resultJsonFactory, 'productFactory' => $this->productFactory]);
}
示例4: 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());
}
示例5: 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();
}
示例6: 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());
}
示例7: setUp
public function setUp()
{
if (!function_exists('libxml_set_external_entity_loader')) {
$this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
}
$this->customer = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\CustomerInterface', [], '', false, true, true);
$this->customer->expects($this->once())->method('getWebsiteId')->willReturn(2);
$this->customerDataFactory = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterfaceFactory', ['create'], [], '', false);
$this->customerDataFactory->expects($this->once())->method('create')->willReturn($this->customer);
$this->form = $this->getMock('Magento\\Customer\\Model\\Metadata\\Form', [], [], '', false);
$this->request = $this->getMockForAbstractClass('Magento\\Framework\\App\\RequestInterface', [], '', false, true, true, ['getPost']);
$this->response = $this->getMockForAbstractClass('Magento\\Framework\\App\\ResponseInterface', [], '', false);
$this->formFactory = $this->getMock('Magento\\Customer\\Model\\Metadata\\FormFactory', ['create'], [], '', false);
$this->formFactory->expects($this->atLeastOnce())->method('create')->willReturn($this->form);
$this->extensibleDataObjectConverter = $this->getMock('Magento\\Framework\\Api\\ExtensibleDataObjectConverter', [], [], '', false);
$this->dataObjectHelper = $this->getMock('Magento\\Framework\\Api\\DataObjectHelper', [], [], '', false);
$this->dataObjectHelper->expects($this->once())->method('populateWithArray');
$this->customerAccountManagement = $this->getMockForAbstractClass('Magento\\Customer\\Api\\AccountManagementInterface', [], '', false, true, true);
$this->resultJson = $this->getMock('Magento\\Framework\\Controller\\Result\\Json', [], [], '', false);
$this->resultJson->expects($this->once())->method('setData');
$this->resultJsonFactory = $this->getMock('Magento\\Framework\\Controller\\Result\\JsonFactory', ['create'], [], '', false);
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->controller = $objectHelper->getObject('Magento\\Customer\\Controller\\Adminhtml\\Index\\Validate', ['request' => $this->request, 'response' => $this->response, 'customerDataFactory' => $this->customerDataFactory, 'formFactory' => $this->formFactory, 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverter, 'customerAccountManagement' => $this->customerAccountManagement, 'resultJsonFactory' => $this->resultJsonFactory, 'dataObjectHelper' => $this->dataObjectHelper]);
}
示例8: 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;
}
}
示例9: 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;
}
示例10: 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()]);
}
示例11: 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;
}
}
示例12: execute
/**
* Customer logout action
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$lastCustomerId = $this->customerSession->getId();
$this->customerSession->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())->setLastCustomerId($lastCustomerId);
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(['message' => 'Logout Successful']);
}
示例13: execute
/**
* AJAX category validation action
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$response = new \Magento\Framework\DataObject();
$response->setError(0);
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData($response);
return $resultJson;
}
示例14: 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]);
}
示例15: execute
/**
* {@inheritDoc}
*/
public function execute()
{
$this->initLayer();
$items = $this->getItems();
$result = $this->jsonResultFactory->create()->setData($items);
return $result;
}