Questions tagged [controllers]

Denotes questions about controllers and action methods

Filter by
Sorted by
Tagged with
43 votes
1 answer
7k views

Difference between /Controller and /controllers

A question I've been meaning to ask for ages, more theoretical than anything else. I'm fully versed in developing Extensions using the normal Magento module file structure. I've noticed though when ...
34 votes
5 answers
50k views

Magento 2: How to get controller, module, action and router name?

Magento 2: How to get controller, module, action and router name?
  • 1,200
29 votes
2 answers
2k views

Why c is lowercase in controllers of Magento?

Why is c lowercase in controllers folder name? Whereas Model, Block and Helper are starting with an uppercase letter?
  • 293
23 votes
5 answers
33k views

Send data from the controller to phtml

I have this controller: ... public function insertAction() { $something = 'testcase'; $this->loadLayout(); $this->_title($this->__("the title")); $this->renderLayout(); } ....
19 votes
3 answers
16k views

Magento 2: rewrite controller

how can i rewrite a controller (that's actually an action) in Magento 2? I tried, as instructed here like this: I have my own module called Namespace_Module with a di.xml file, that is taken into ...
  • 196k
18 votes
5 answers
57k views

How to get media directory path in a phtml file in magento 2?

Used the below method in order to get the media directory path, but it returns an error. $om = \Magento\Core\Model\ObjectManager::getInstance(); $directoryList = $om->get(\Magento\App\Filesystem\...
  • 1,752
13 votes
5 answers
10k views

Why can't I inject the ProductRepositoryInterface in Magento 2?

I have a controller action into which I would like to inject the product-repository namespace Nosto\Tagging\Controller\Export; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Store\...
12 votes
4 answers
21k views

Magento2 - Custom Controller throws error

I created a module shown in this post. But my own controller doesn't work. I've got this error message: Recoverable Error: Argument 1 passed to MP\MyModule\Controller\Index\CheckUserName::...
  • 1,763
12 votes
1 answer
47k views

How to make a simple ajax call in Magento 2.1.0

I have added a simple button in one of my phtml file. <input type='button' name='emq_zip_btn' class='emq_zip_btn' value='Go'> I have added one custom js file ("emq.js") from a custom module (...
  • 837
12 votes
1 answer
18k views

How to override the controller AccountController

I need to override a method Controller Core/Mage/Customer/controllers/AccountController.php and add a new method. Since this controller to edit is wrong - it should be overridden. According to ...
11 votes
1 answer
4k views

How do I make my custom module pages use SSL?

I've created a Module with some pages and I'm wondering how do I force this pages to use SSL on the url, like the customer login page. Not all of the pages need it, but some do. What comes to mind (...
  • 1,414
11 votes
1 answer
643 views

Using Reserved Words in Magento 2 URLs

Is it possible to create a module in Magento 2, using standard routing, that responds to a URL in the form of http://magento.example.com/namespace_module/return/index That is -- a URL where the ...
  • 43.8k
11 votes
2 answers
4k views

How to get all children blocks in controller Magento 2?

I have to get all children block in controller using parent block name or layout Magento 2. Thanks in advanced.
10 votes
1 answer
16k views

Change page title in controler or block

I have found two ways for changing page title and I don't know which one I should use. 1. Override prepareLayout() in block protected function _prepareLayout() { $this->pageConfig->...
10 votes
1 answer
926 views

The purpose of the http verbs interfaces

In Magento 2.3 there are some interfaces for all the http verbs Magento\Framework\App\Action\HttpPostActionInterface Magento\Framework\App\Action\HttpGetActionInterface, ... All of them are empty ...
  • 196k
9 votes
3 answers
10k views

Creating custom module in magento2 show fatal error

I working on creating custom module in magento 2, but it's not working. Could you please suggest me where i went wrong?. My module code: app/etc/config.php: 'modules' => array ( '...
  • 91
9 votes
1 answer
6k views

What are the Dispatch/Execute() functions and how do they work?

I would like an explanation of the dispatch and execute functions, the differences between them, when they are/should be used and how. Please limit your answer to Magento 2
  • 1,523
9 votes
1 answer
6k views

Magento 2 : Modal Popup using Ajax

How to use model popup using ajax? Can any one help for this. Here below my code. How can i apply in this code model popup? And also post data get in popup. How it possible? app/code/Vendor/Complany/...
  • 1,483
8 votes
2 answers
21k views

How to get block method in controller?

My block file path is app\code\local\Company\Modulename\Block\Product\View\View.php In this file method name is getId() Now how to call call this method in controller?
  • 5,081
8 votes
1 answer
28k views

Reliable way to redirect to last page

Is there a reliable way in magento to redirect to the last page. I have a small form that i want to redirect to the previous page if an error occurred. The form sits on the product page and ...
8 votes
2 answers
8k views

Magento 1 : how to override/rewrite a core controller class?

Note: This is intended as a canonical question that fully explains how controller rewrites work and can be used as duplicate target for more specific "How do I override controller X" or "Why does my ...
7 votes
3 answers
37k views

Magento2 - How to get JSON response from Controller [closed]

I've created my custom controller and I want to return the result formated with JSON. I always get a JSON parse error "JSON.parse: unexpected character at line 1 column 1 of the JSON data" and the ...
  • 1,763
7 votes
3 answers
9k views

magento2.3 controller won't handle post method

This is something I used to be able to do in 2.2, but in 2.3 I can't. A simple controller to handle a POST method, and only receive and send application/json content type. It works for GET/PUT ...
  • 306
7 votes
2 answers
15k views

How to call a controller action outside magento?

We can create php scripts which runs outside magento. And also use magento functionalities by using the following code snippet in it. define('ROOT', ''); $mage_php_url = ROOT.'app/Mage.php'; ...
  • 9,857
7 votes
5 answers
28k views

Magento checkout - getting custom attribute value

I am working in the OnepageController.php What I needed was in the checkout process was to get values of some customer created attributes for the products in the current order. I have been able to ...
7 votes
2 answers
18k views

How to get RefererUrl in magento 2 observer?

How to get RefererUrl (source page url) in Observer file? Actually by using this; $this->_redirect->getRefererUrl(); I am getting Referer Url in magento 2 controller file.But in Observer file ...
  • 1,377
7 votes
3 answers
16k views

Magento 2: How to get data from controller to block/view

I am looking for the right flow of data of how Magento wants me to do it. I have some data which I want to pass to the view. How do I do this the right way? I saw the following answer for Magento 1.x ...
  • 1,308
7 votes
4 answers
13k views

Call action after "Save Config" was clicked in the admin

How can I call an action of my controller in a custom module after "Save Config" was clicked in System --> Configuration --> Catalog? (image) Settings of my module are inside that section, and I need ...
  • 2,172
7 votes
3 answers
32k views

Magento 2 - Send form data to Controller using AJAX and return Result to PHTML

I am working on a simple custom module, where user can input height and the weight/BMI will be shown as output on form submit. The page where user enters input is Calculation Page while output is ...
7 votes
1 answer
7k views

Dynamic create block in magento 2 and send as ajax call response?

I want replacement of following in magento 2.x $block = $this->getLayout()->createBlock('customer/form_login')->setTemplate('persistent/customer/form/login.phtml'); $this->getResponse()-&...
  • 3,048
7 votes
2 answers
17k views

Override a controller in Adminhtml

Currently I'm trying to override the GroupController of Mage_Adminhtml_Customer so I can add some code for saving my custom field in the Customer Groups menu. As you can think it doesn't really work ...
  • 135
7 votes
3 answers
17k views

Magento 2 - Ajax is not calling Controller

I tried to make a simple ajax-call to my controller. Somehow I don't get a valid response. MyCompany/MyModule/Index/GetAjax.php namespace MyCompany\MyModule\Controller\Index; class GetAjax extends ...
  • 604
7 votes
2 answers
15k views

Form Key Value in template and controller

The following function is used to get the form key in templates with type hidden. My customerController.php does not validate the form key with the method _validateFormKey(). When I echo the following ...
7 votes
3 answers
12k views

Custom Block "Invalid Block Type"

I have a custom Block which I'm now trying to break into two sepparate blocks. Here's my folder structure (on app/code/local/MyNamespace/CustomerDashboard): Since I want to break the functions of ...
  • 255
7 votes
2 answers
8k views

Overriding a core controller's preDispatch method

I need to override a core controller's preDispatch method. The core controller inherits from Mage_Checkout_Controller_Action, which itself has a preDispatch method that needs to be called. Since I'm ...
7 votes
2 answers
3k views

"We can't save the customer" error after Migration - Magento 2.3

I have migrated store from Magento 1.7.0.2 to Magento 2.3. When i Create the customer it shows me error "We can't save the customer." I have checked log file and i can see log, "Class Magento\Eav\...
7 votes
1 answer
1k views

Magento 2: Why Does My Custom Controller Generate an Interceptor?

I recently created a basic routing configuration for a test module <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/...
  • 43.8k
7 votes
2 answers
2k views

AccountController override not working on new Action and doing 302 redirection

I am try add new action on AccountController Now: AccountController is override properly But it is whenever hit new Action (ajaxLoginPostAction) is redirect to 302. I add ajaxLoginPost() as open ...
  • 76k
7 votes
1 answer
18k views

Magento + JQuery + Ajax - How do I reload just parts of my custom Module instead of the whole Block?

I was just recently given the task to create a simple Product configurator for our Magento Template in 5 days, where you can choose some attributes and it calculates the price for you, fades over a ...
7 votes
5 answers
4k views

Update price in checkout page without refresh Magento 2

I have a controller where I apply some discount on the checkout page. Now, I can see the price change just after I refresh the page. I search for a solution, to refresh the price using a Js file. ...
  • 2,612
6 votes
2 answers
24k views

Retreive and return response from controller in magento2

I have a problem in which I'm making an AJAX call to a controller. But, I am not able to check whether it is working or not. I also want to return data in response. But, it keeps throwing some errors. ...
6 votes
2 answers
16k views

Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom module

I think my issue might have to do with dependency injection, which I'm still trying to grab the concept. What I would like to do is to be able to get the URL using getUrl() function from Magento\...
6 votes
2 answers
4k views

Magento\Framework\ObjectManager\ObjectManager given

I get the following when using my controller through a front end route. Fatal error: Uncaught TypeError: Argument 1 passed to Namespace\Module\Controller\Index\Index::__construct() must be an ...
  • 263
6 votes
1 answer
6k views

magento 2 run function on every page

I have a function that retrieves a visiting user's country name by doing an ip lookup. I currently have this function running through the execute method in my module's controller. The problem is ...
  • 1,632
6 votes
6 answers
11k views

Programatically clear the cart of a logged-in user

TL;DR When a customer is logged in, Mage::getSingleton('checkout/session')->clear(); doesn't clear their cart. I need something that will. I've asked this as part of another question, but i ...
6 votes
3 answers
3k views

Magento 2.2.5 : Cant go to execute() in Controller

I'm doing a simple CRUD custom module after this module C:\xampp\htdocs\magento\vendor\magento\module-cms\Controller\Adminhtml\Page\Save.php and I have a controller to save/create/add the "students" ...
  • 1,198
6 votes
3 answers
5k views

What is the purpose of isAllowed method and when It is called in Magento 2?

I have seen that below method. protected function _isAllowed() { return $this->_authorization->isAllowed('Custom_Module::moduleslider_banners'); } What exactly it will do? Will it check ...
  • 9,553
6 votes
1 answer
6k views

Add new Action to a existing Magento route - Magento2

Here is my scenario, I want to add a new Action to an existing Magento route, for example under the MyAccount section you can find /sales/order/[view, history, etc]; I want to add an index like an ...
  • 310
6 votes
2 answers
7k views

How to call block method from Controller in magento 2

I want call block methods from controller. My files are below Controller namespace Vendor\ModuleName\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\...
  • 507
6 votes
2 answers
88 views

Where all controller frontnames are stored?

Suppose I've action URL abc/index/save and frontname abc is defined in my config file. When I hit abc/index/save it calls my action method. Where all frontname informations are stored? Does it ...
  • 6,757

1
2 3 4 5
20