0

My aim is to generate a few frontend views with an phtml for each view. I developed a module for that.

File: Routes.xml

<?xml version="1.0"?>
<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="micuenta" frontName="micuenta">
            <module name="MyVendor_Intranet"/>
        </route>
    </router>
</config>

File: Controller/Index/index.php

/**
 * Constructor
 *
 * @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);
}

/**
* Mi Cuenta.
*
* @return \Magento\Framework\View\Result\PageFactory
*/
public function execute()
{
    $resultPage = $this->resultPageFactory->create();
    $resultPage->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0', true);
    return $resultPage;
}}

But I get an apache error:

[Mon Mar 16 17:54:22.638389 2020] [:error] [pid 4074] [client 46.27.23.194:62372] PHP Fatal error: Uncaught TypeError: Argument 1 passed to MyVendor\Intranet\Controller\Index\Index::__construct() must be an instance of Magento\Framework\App\Action\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /var/vhosts/algsa.es/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /var/vhosts/algsa.es/app/code/Auren/Intranet/Controller/Index/index.php:15\nStack trace:\n#0 /var/vhosts/algsa.es/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Auren\Intranet\Controller\Index\Index->__construct(Object(Magento\Framework\ObjectManager\ObjectManager))\n#1 /var/vhosts/algsa.es/vendor/magento/framework/ObjectManager/Factory/Compiled.php(88): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Auren\\Intranet\\...', Array)\n#2 /var/vhosts/algsa.es/vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Compiled->create('Auren\\Intranet\\...', Array)\n#3 /var/vhosts/algsa.es/ve in /var/vhosts/algsa.es/app/code/Auren/Intranet/Controller/Index/index.php on line 15

My page gets a blank view and apache log registers the error.

PD: Very similar approach from How call Magento 2 module by url (or add route in module)?

0

I think you have done right but just forgot to clear everything in generated/code folder.

It is the place where the new dependency injection is generated and located.

Just try and enjoy.

0

try below code

File: etc/frontend/routes.xml

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

File: Controller/Index/index.php

namespace MyVendor\Intranet\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    /**
     * @var \Magento\Framework\Controller\Result\ForwardFactory
     */
    protected $_pageFactory;

    public function __construct(
        Context $context,
        PageFactory $pageFactory
    )
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }
    public function execute()
    {

        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->_pageFactory->create();
        return $resultPage;

    }
}

Create all the required files for the module like layout, Block

Then hit below commands

sudo rm -rf var/*
sudo rm -rf pub/static/frontend/*
sudo rm -rf generated/*

sudo php bin/magento setup:upgrade
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento ca:cl
sudo php bin/magento ca:fl
sudo chmod -R 777 /var/www/html/(project name)/
  • create all the required files for the module. – Devidas Mar 16 '20 at 17:37
  • I already had that, it is the same code, don't see the difference. But I also tried without success. – Ángel Carlos del Pozo Muela Mar 16 '20 at 18:14
  • i just update my ans please check – Devidas Mar 17 '20 at 6:49
  • I tried that and same apache error – Ángel Carlos del Pozo Muela Mar 17 '20 at 11:20

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.