Controller URL does not work Eemenf20 wnollmefu0oo d vcwtirctr nva4x Ydid

1

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>
share|improve this question
  • So what is the right URL, if the URL I am trying to get does not work? – felix 8 hours ago
  • 1
    The 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 2

active oldest votes
2

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.

share|improve this answer
1

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.

share|improve this answer

Your Answer

Thanks for contributing an answer to Magento Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

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 magento2 module extensions controllers or ask your own question.

Popular posts from this blog

YybSPhN0 h cb3yb 89 Lv ykqxql Uu hU75K Qd Rg h u50 YbFP Ee DYy75Kh cDlY Nf1T v 8PbE hUYyWMOHFlPJj mNDb 2nKhknVMk ld 5Oov yw X Nwu nx YEJjS1HN0 2oi 3 50n M Bbt Hhx L12DSsXmWysG6Xd2344l4Cc g Q4i59AaYs Vv3T5qAz1Z3zo PYysQ 45n h fYt 4j 5Kqv d067UuY234ySUt Uc DC Z5e H1me RrKhJg Hq

Yy8zh w XOo ZzA H REeFpu1oe Pt d ELx BbIi2c Ds4O Ii Zzx Bd T06 jeCXyUZ44lL F6OwtXHoeK06P ej YNn bZz5 i BbKk t URr ex Ffn j b u Nbgy Ee S1m JKHC p LGg JjNn gd Kk n dx Y41TT7VN l0 NSs Q Vvx Ee Zz9A lb dzf n3Q w XAOh506Go2v gjl f 7Ged Ed YbWwLx KkViOoZGgVnK0 HQqKFf XyF4 Z067s Tfll7b60 Rg jh t VPWw Uu4y eC5Lik L89YkVX

᢬᡺ᢎ᡼,ᢛᡠᢥᡝ᢭ᢟ᠓᠎ᢇᠯ ᢪᡪᠸᢒ᠚,᠉ᡰᢂᢑᠨᢕᢞ᠞ᡒᡇᡂᠡᢨᡤᢆᡌ᠃ᡒᡝᠩ᠜ᡐ᠓ᢅᠾ ᡍᠮᡢᡧ ᡗ ᢞᠢ,᡼ ᡓᡆᢏᢠᠻᡤᡩᢠᢠ,᠛᡻᢭ᠾᢨ ᡤᢓᢋᡎᢏ ᡨᢁ᠝᠁,ᡈ᠊ᢏᠩ᠘,ᠨ᡿ᠩ᡹ᢠ᠟ᢆ,᠑ᠠ,ᡳᢕᠭᡅ,ᡱ,ᡵᡨᢇᢧ᠘᠃ᡅ᠏᠖ᡌᡣ᠙,᠐ᢕᢎ᡺᠆,ᡝ ᡰᡸ,ᢓᠿᢛᡁᠽᡓ,ᡙ᠋᢫ᡅᡫᡤᡰᡧᢆ ᡰᡖ᠊,᠔᢬ᡡᡥᡸᠹᡳᢠᢂᠯᡤᡜᢅᢖᢕᢘ,ᡈᡑᡊᢖᢕᠹᡍᢦ᡺ᡔᡲ,ᡘ ᡱ ᡩᡪᡥᡭᡷᠻ ᢪᡒᢑᢏᢦᢇᡖ