I have the problem that i always get the error 404 not found, if I want to get the url of my controller. This is my filestructure:
And this is the URL I am trying to get loaded: http://localhost/shoppingcart/index/index (edit) also tried: http://localhost/post/index/index
If this URL does not work, which URL should work for my controller?
My base URL is localhost
Whis is my controller:
<?php
namespace MassiveArt\ShoppingCart\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;
class Post extends Action
{
protected $formKey;
protected $cart;
protected $product;
public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
FormKey $formKey,
Cart $cart,
Product $product) {
$this->formKey = $formKey;
$this->resultJsonFactory = $resultJsonFactory;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
$result = $this->resultJsonFactory->create();
$productId = $this->getRequest()->getParam('productId');
try {
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product' => $productId,
'qty' => 1
);
$product = $this->product->load($productId);
$this->cart->addProduct($product, $params);
$this->cart->save();
$result->setData(['message' => __("Product is added in cart")]);
return $result;
} catch(\Exception $e) {
$result->setData(['error' => __($e->getMessage())]);
return $result;
}
}
}
This is my routes.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<!--define id for frontend route is “standard”-->
<router id="standard">
<!--Route name-->
<route id="post" frontName="post">
<!--Module name-->
<module name="MassiveArt_ShoppingCart" />
</route>
</router>
</config>