Magento Create New Controller

In Magento, creating a new controller involves several steps. Controllers are responsible for handling requests and processing actions in the MVC (Model-View-Controller) architecture of Magento. Here’s a basic guide on how to create a new controller in Magento:

  1. Create a Module:
    If you haven’t already created a custom module, you’ll need to do so. Modules in Magento encapsulate functionality and can contain controllers, models, blocks, and other elements.

    Create a directory structure like this in your Magento installation:

    app/code/[Vendor]/[Module]
    

    Replace [Vendor] with your company or organization name, and [Module] with the name of your module.

  2. Create the Controller File:
    Inside your module directory, create a Controller directory, and within that, create a file for your controller. The file should follow the pattern [ControllerName].php. For example, if your controller is for handling product requests, you might create a file named ProductController.php.

    The file should contain a class that extends Magento\Framework\App\Action\Action. Here’s a basic example:

    // app/code/[Vendor]/[Module]/Controller/[ControllerName].php
    
    namespace [Vendor]\[Module]\Controller;
    
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    
    class [ControllerName] extends Action
    {
        protected $resultPageFactory;
    
        public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
        {
            parent::__construct($context);
            $this->resultPageFactory = $resultPageFactory;
        }
    
        public function execute()
        {
            $resultPage = $this->resultPageFactory->create();
            return $resultPage;
        }
    }
    
  3. Declare the Controller in the Module’s routes.xml File:
    In the etc directory of your module, create a frontend directory (if it doesn’t exist), and within that, create a routes.xml file. This file is used to declare your controller and define the routing.

    
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="standard">
            <route id="[router_id]" frontName="[router_front_name]">
                <module name="[Vendor]_[Module]" />
            route>
        router>
    config>
    

    Replace [router_id] and [router_front_name] with your desired values.

  4. Access the Controller:
    With these steps completed, you should be able to access your controller using a URL like this:

    http://example.com/[router_front_name]/[controller_name]/[action_name]

    This URL is based on the router ID and controller name you specified.

Remember to clear the Magento cache after making changes to ensure that the system recognizes your new module and controller.



Copyright © 2013-present Magesystem.net All rights reserved.