Questions tagged [controllers]
Denotes questions about controllers and action methods
943
questions
43
votes
1answer
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 ...
33
votes
4answers
40k views
Magento 2: How to get controller, module, action and router name?
Magento 2: How to get controller, module, action and router name?
29
votes
2answers
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?
23
votes
5answers
32k 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
3answers
15k 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 ...
16
votes
4answers
44k 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\...
12
votes
4answers
20k 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::...
12
votes
5answers
9k 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
1answer
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
1answer
41k 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 (...
11
votes
1answer
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 (...
11
votes
1answer
545 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 ...
11
votes
2answers
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
1answer
698 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 ...
9
votes
3answers
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 (
'...
9
votes
1answer
4k 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/...
8
votes
2answers
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?
8
votes
1answer
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
1answer
12k 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->...
8
votes
2answers
7k 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 ...
8
votes
1answer
5k 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
7
votes
2answers
14k 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';
...
7
votes
4answers
12k 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 ...
7
votes
2answers
16k 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 ...
7
votes
1answer
6k 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()-&...
7
votes
3answers
13k 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 ...
7
votes
2answers
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
3answers
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 ...
7
votes
2answers
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
2answers
2k 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
1answer
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/...
7
votes
2answers
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 ...
7
votes
1answer
17k 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
5answers
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.
...
6
votes
3answers
31k 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 ...
6
votes
5answers
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 ...
6
votes
2answers
19k 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
2answers
12k 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
3answers
13k 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 ...
6
votes
2answers
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
...
6
votes
3answers
22k 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 ...
6
votes
5answers
10k 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
3answers
2k 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" ...
6
votes
3answers
4k 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 ...
6
votes
1answer
5k 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 ...
6
votes
2answers
6k 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\...
6
votes
2answers
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
votes
1answer
2k views
Magento2 Override Contact Post Controller [duplicate]
how to Override contact controller in custom module?
venoder/magento/module-contact/Controller/Index.php
6
votes
2answers
3k views
How to avoid redirection to “enable-cookies” page during controller unit test?
I'm doing a very simple controller test using Ecomdev_PHPUnit:
class Foo_BarWishlist_Test_Controller_IndexController
extends EcomDev_PHPUnit_Test_Case_Controller
{
/**
* @test
* @...
6
votes
1answer
2k views
How to display a phtml in a backend custom menu
I've created a new menu on the backend, called "X" with an item called "Y". When I click on this item, it appears nowhere because I don't know how to display it. Let me show you what I have done.
In ...