1

I have an AJAX request POSTing data to a controller in my Magento module.

$.ajax({
 url: "/page/section/profile?isAjax=true",
 type: "POST",
 data: "profileId=" + profile.id,
 success: function (result) {}
}); 

Starting at http://my-website/store2 I post to the /profile endpoint, where I'm trying to access the current store ID in the following (simplified) controller code:

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Action\Action;
class Profile extends Action
{
 /**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
 private $storeManager;
 /**
 * Profile constructor
 *
 * @param Context $context
 */
 public function __construct(
 Context $context,
 \Magento\Store\Model\StoreManagerInterface $storeManager
 ) {
 $this->storeManager = $storeManager;
 parent::__construct($context);
 }
 public function execute()
 {
 $storeId = $this->storeManager->getStore()->getId(); // returns 1
 $websiteId = $this->getRequest()->getParam('website', 0); // returns 0
 return [$storeId, $websiteId];
 }
}

however this always returns store ID 1 (default) instead of the expected store ID 2.

I am not currently logged in to Magento admin.

I have tried to obtain this data via e.g. $this->getRequest()->getParam('website'), in the controller, but that doesn't seem to help either.

4
  • which data are you trying to get using the getParam function? Commented Apr 9, 2019 at 8:31
  • @magefms I have tried to get 'website'. This returns 0. Commented Apr 9, 2019 at 8:49
  • can you post your controller code Commented Apr 9, 2019 at 8:50
  • check updated answer @strangerpixel Commented Apr 9, 2019 at 9:10

2 Answers 2

0

You can try like this in your controller:

$this->request()->getParam('website',0);

UPDATE:

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Action\Action;
class Profile extends Action
{
 /**
 * @var \Magento\Framework\App\Request\Http
 */
 protected $request;
 /**
 * Profile constructor
 *
 * @param Context $context
 */
 public function __construct(
 Context $context,
 \Magento\Framework\App\Request\Http $request
 ) {
 $this->request= $request;
 parent::__construct($context);
 }
 public function execute()
 {
 return $this->request->getParam('website',0);
 }
}
answered Apr 9, 2019 at 8:54
5
  • Unfortunately that still returns 0 for 'website'. Commented Apr 9, 2019 at 9:17
  • did you run the upgrade and other required commands? Commented Apr 9, 2019 at 9:18
  • how about changing website to store like return $this->request->getParam('store',0); ? Commented Apr 9, 2019 at 9:21
  • @strangerpixel I see your code in your post, it return 0 because you are not injecting \Magento\Framework\App\Request\Http $request in your constructor Commented Apr 9, 2019 at 9:29
  • I have tried it locally, it's the same outcome as $this->getRequest(), which returns a \Magento\Framework\App\RequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it. Commented Apr 9, 2019 at 9:34
0

It turned out I needed to post data to the appropriate store URL, defined in the block beforehand.

Block:

public function getEndpointWithStoreCode()
{
 return $this->storeManager->getStore()->getBaseUrl(
 \Magento\Framework\UrlInterface::URL_TYPE_WEB,
 true
 ) . "/page/section/profile?isAjax=true";
}

Template:

<script type="text/x-magento-init">
{
 "*": {
 "my_module/js/profile" : {
 "profileEndpoint" : "<?= $block->getEndpointWithStoreCode() ?>"
 }
 }
}

JS:

$.ajax({
 url: config.profileEndpoint,
 type: "POST",
 data: "profileId=" + profile.id,
 success: function (result) {}
});

By POSTing directly to /store2/page/section/profile, the right store scope is locked in.

answered Apr 9, 2019 at 13:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.