1

I would like to create own controller with own frontend view.

Here are my files: registration.php:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Unity_CheckOrder',
    __DIR__
);

controller:

namespace Unity\CheckOrder\Controller\Index;

//use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
//    protected $_resultPageFactory;
//
//    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
//    {
//        $this->_resultPageFactory = $resultPageFactory;
//        parent::__construct($context);
//    }

    public function execute()
    {
        echo '<p>You Did It!</p>';
        var_dump(__METHOD__);
       // $resultPage = $this->_resultPageFactory->create();
       // $resultPage->addHandle('check_order');
      //  return $resultPage;
    }
}

Of course I tried also with:

namespace Unity\CheckOrder\Controller\Index;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_resultPageFactory;

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
//        echo '<p>You Did It!</p>';
//        var_dump(__METHOD__);
        $resultPage = $this->_resultPageFactory->create();
        $resultPage->addHandle('check_order');
        return $resultPage;
    }
}

etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Unity_CheckOrder" setup_version="1.0.0">
    </module>
</config>

etc/frontend/routes.xml

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

view/frontend/layout/checkorder_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="1column">
    <head>
        <title>
            Simple Magento 2 Module
        </title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Unity\CheckOrder\Block\Test" name="checkorder" template="Unity_CheckOrder::checkorder.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

view/frontend/templates/checkorder.phtml

<?php $this->getHelloWorldTxt(); ?>

block test.php

<?php
namespace Unity\CheckOrder\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    public function __construct(\Magento\Backend\Block\Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function getHelloWorld()
    {
        return 'Hello World';
    }
}

I also tried with

<?php
namespace Unity\CheckOrder\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    public function getHelloWorld()
    {
        return 'Hello World';
    }
}

After go to url magento2test.localhost/checkorder/ and magento2test.localhost/checkorder/index/index/

I got 404 all the time. Of course I tried :

  • php bin/magento setup:upgrade
  • rm -rf var/cache/* var/page_cache/* var/generation/*
  • php bin/magento cache:clean / flush
  • php bin/magento indexer:reindex
  • php bin/magento setup:di:compile

Module is enabled and visible in Admin Advanced tab

Here is structure of my module: image

  • Did you tried with http://localhost/checkorder/? You should try with ` localhost/yourmagento/checkorder` – Khoa TruongDinh Dec 13 '16 at 14:01
  • localhost = mymagento. To be honest it is magento2test.localhost/checkorder – Konrad Siamro Dec 13 '16 at 14:03
  • have you added router.php controller? – chirag Dec 13 '16 at 14:06
  • No. Is it necessary? Isn't Router.php used for handling CMS pages? It sets module name (module front name) to “cms”, controller name (controller path name) to “page” and action name to “view” – app/code/Magento/Cms/Controller/Page/View.php controller. How should it help? – Konrad Siamro Dec 13 '16 at 14:09
  • Can you show your folder structure or the full path of file? – Khoa TruongDinh Dec 13 '16 at 14:12
0

We should make sure that our controllers, blocks, config, registration files are inside our module folder. In your case, seem that your controller is wrong place.

| improve this answer | |
  • 1
    registration.php was inside module. Controller was outside, however moved Controller into module resolve problem ^^ – Konrad Siamro Dec 13 '16 at 14:35
  • Yes, I have just edit my answer. – Khoa TruongDinh Dec 13 '16 at 14:36
0

Perhaps a stupid question, but did you also enable the module? bin/magento module:enable Unity_CheckOrder. I once was pulling my hair out for almost an hour simply because I forgot to enable the module :-/

| improve this answer | |
  • Yes, module is enabled in app/etc/config.php – Konrad Siamro Dec 13 '16 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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