3

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: enter image description here

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 Schönherr Aug 20 at 9:19
  • 1
    The URL should follow: front_name/controller/action. In your case it will be post/index/index. – Flying Finner Aug 20 at 9:28
  • Tried this URL, but did not work as well – Felix Schönherr Aug 20 at 9:32
  • 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 Aug 20 at 9:36
  • I acctually did, but will try it again and run this commands again – Felix Schönherr Aug 20 at 9:36
3

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.