Controller URL does not work Eemenf20 wnollmefu0oo d vcwtirctr nva4x Ydid
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>
-
So what is the right URL, if the URL I am trying to get does not work? – felix 8 hours ago
-
1The URL should follow: front_name/controller/action. In your case it will be post/index/index. – Flying Finner 8 hours ago
-
Tried this URL, but did not work as well – felix 8 hours ago
-
Did you run setup upgrade and redepoy i know it sounds like a simple answer but i had the same issue and this resolved it for me – Dava Gordon 8 hours ago
-
I acctually did, but will try it again and run this commands again – felix 8 hours ago
2 Answers
The controller URL follows: front_name/controller/action. So in your case the correct URL will be post/index/index.
Also, you need to change either the controller class name to Index or change the controller file name to Post.php.
Please try by post/index/post.
Controller is called by route_frontname/controller_folder/action_file.
Your controller file have class name Post. Please make sure you have saved it by Post.php.