1

I wrote module in Magento 2 and this is my /etc/frontend/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="test" frontName="test">
            <module name="Arthur_Test" />
        </route>
    </router>
</config>

and I have to access to my module from external script to get some data he generate. That data is in app/code/Arthur/Test/storage/data.txt

I cant get that data by exact url (example.com/app/code/Arthur/Test/storage/data.txt) because of 403 Forbidden so I want to call method in my module. How can I do it ?

url example.com/test/ give me 404 Not Found

1

2 Answers 2

1

You need to learn how to create a module. For starters, I can help you create a basic module:

app/code/Arthur/Test/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Arthur_Test',
    __DIR__
);

app/code/Arthur/Test/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="Arthur_Test" setup_version="1.0.0"/>
</config>

app/code/Arthur/Test/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 frontName="test" id="test">
            <module name="Arthur_Test"/>
        </route>
    </router>
</config>

app/code/Arthur/Test/Controller/Index/Index.php

<?php


namespace Arthur\Test\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

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

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        echo "My controller has been called";die;
        //return $this->resultPageFactory->create();
    }
}
1
  • this is working, i am trying to call my phtml and block that isn't working the way you have done controller i have it too and if i put die; it works as well not going to my phtml class Commented Jun 3, 2020 at 16:02
0

Make router name storage

 <route frontName="storage" id="storage">

The path to controller should be app/code/Arthur/Test/Controller/Storage/Data.php

and the file

<?php namespace Arthur\Test\Controller\Storage;

class Data extends \Magento\Framework\App\Action\Action
{
  echo "test";
}

Now if you call example.com/storage/data it will work

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.