0

i have custom module. i can't call controller in view of my module. this url call

http://localhost/magento2-2.0.0-rc/index.php/namespace/modulename/product/view/custom/

Code file Custom.php in Namespace/Modulename/Controller

namespace Namespace\Modulename\Controller;
class Custom extends \Magento\Framework\App\Action\Action
{
    /**
     * Index action
     *
     * @return $this
     */
    public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('/');
    }
}

This file Custom.php in Namespace/Modulename/Controller/Product/View

<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Namespace\Modulename\Controller\Product\View;


class Custom extends \Namespace\Modulename\Controller\Custom
{

     protected $resultForwardFactory;

    protected $resultPageFactory ;
     protected $_coreRegistry;
    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context, 
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,      
        \Magento\Framework\Registry $registry

    ) {
        $this->resultForwardFactory = $resultForwardFactory;
        $this->resultPageFactory  = $resultPageFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context);
    }   

    public function execute()
    {   
        $data = $this->getRequest()->getPostValue();
        print_r($data); die('xx');
    }
}

Edit routes.xml in etc/frontend

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

4 Answers 4

1

You need declare a route: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/routing.html
....

1
  • yes i have routes.xml in etc/frontend
    – xanka
    Commented Nov 11, 2015 at 4:30
1

Clear cache. Magento caches map of controller actions.

3
1

once ur module is installed upgrade from cli

php D:\xampp\htdocs\magento2a\bin\magento setup:upgrade  (localhost) 

still you have issue then below are the few file is needed 1. module.xml 2. app\code\Sugarcode\Test\etc\frontend\routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="test" frontName="test">
            <module name="Sugarcode_Test" />
        </route>
    </router>
</config>

3. action \app\code\Sugarcode\Test\Controller\Index\Index.php

<?php
namespace Sugarcode\Test\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
        /**
         * @var \Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;

        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
         */
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
        )
        {
            $this->resultPageFactory = $resultPageFactory;
            parent::__construct($context);
        }
    /**
     * Default customer account page
     *
     * @return void
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}
  1. app\code\Sugarcode\Test\view\frontend\layout\test_index_index.xml

        <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Core/etc/layout_single.xsd">
    
            <body>
                <referenceContainer name="content">
                    <block  class=""  name="test" template="Sugarcode_Test::index.phtml"></block>
                </referenceContainer>
    
            </body>
        </layout>
    

5.app\code\Sugarcode\Test\view\frontend\templates\index.phtml

 <?php
    $message = "Hello World";
    ?>
    <div class= "testclass">
        <?php echo $message;    ?>
    </div>
    <div id="test-re-js" data-mage-init='{"testmodulesjsmap":{"bodySelector": "from phtml file"}}' class="">
    testclass
    </div>

url is youdomin.com/test/idex/index

i hope this will help you

0

Try to use this url instead (remove the namespace part)

http://localhost/magento2-2.0.0-rc/index.php/modulename/product/view/custom/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.