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.
-
which data are you trying to get using the getParam function?fmsthird– fmsthird2019年04月09日 08:31:19 +00:00Commented Apr 9, 2019 at 8:31
-
@magefms I have tried to get 'website'. This returns 0.strangerpixel– strangerpixel2019年04月09日 08:49:32 +00:00Commented Apr 9, 2019 at 8:49
-
can you post your controller codefmsthird– fmsthird2019年04月09日 08:50:04 +00:00Commented Apr 9, 2019 at 8:50
-
check updated answer @strangerpixelfmsthird– fmsthird2019年04月09日 09:10:55 +00:00Commented Apr 9, 2019 at 9:10
2 Answers 2
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);
}
}
-
Unfortunately that still returns 0 for 'website'.strangerpixel– strangerpixel2019年04月09日 09:17:54 +00:00Commented Apr 9, 2019 at 9:17
-
did you run the upgrade and other required commands?fmsthird– fmsthird2019年04月09日 09:18:38 +00:00Commented Apr 9, 2019 at 9:18
-
how about changing website to store like
return $this->request->getParam('store',0);?fmsthird– fmsthird2019年04月09日 09:21:38 +00:00Commented 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 $requestin your constructorfmsthird– fmsthird2019年04月09日 09:29:00 +00:00Commented 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.strangerpixel– strangerpixel2019年04月09日 09:34:41 +00:00Commented Apr 9, 2019 at 9:34
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.