Search code examples
magento

get Request Params magento


I am new with Magento "Can anyone have example code or tutorial, help me : In the Dev_Banner module (i already created)enter image description here, create a controller for the page banner detail with the address banner/index/view/id/1 Where 1 is the banner_id of any banner, the request in the controller can load the banner model"


Solution

  • You can Create the Controller as follows -

    First Create route -

    routes.xml

    The location of the routes.xml file in a module, either etc/frontend or etc/adminhtml

    based on the area you are working and inside the routes

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="admin">
            <route id="banner" frontName="banner">
                <module name="Dev_Banner"/>
            </route>
        </router>
    </config>
    

    After Creating the routes.xml you need to add the Controller Directory and then Adminhtml Directory then Index Directory and then View.php

    Controller/Adminhtml/Index/View.php

    Inside View Controller add the code -

        <?php
        
        namespace Dev\Banner\Controller\Adminhtml\Index;
        
        class View extends \Magento\Backend\App\Action
        {
    
            public function __construct(
                \Magento\Backend\App\Action\Context $context,
                \Magento\Framework\View\Result\PageFactory $resultPageFactory,
                \Magento\Framework\App\Request\Http $request
            ) {
                parent::__construct($context);
                $this->resultPageFactory = $resultPageFactory;
                $this->request = $request;
            }
        
            public function execute()
            {
                $id= $this->request->getParam('id');
                //Do Your Logic Here
                $resultPage = $this->resultPageFactory->create();
                $resultPage->getConfig()->getTitle()->prepend(__('Sales Order Import'));
                return $resultPage;
            }
        }