4

I am trying to add a new action to the existing module Magento_Customer.

My module has the following namespace Custom_AccountReferral

I need to add a new action customer/account/referral/

I have create a custom module and it works.

  1. Create a new controller inside my module directory AccountReferral/Controller/Account the file with name Referral.php
  2. Create the routes.xml file inside etc/frontend/ directory

With the following content

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="customer" frontName="customer">
            <module name="Custom_AccountReferral" before="Magento_Customer"/>
        </route>
    </router>
</config>
  1. Also tried to add the following line to the di.xml file

    preference for="Magento\Customer\Controller\Account\Referral" type="Custom\AccountReferral\Controller\Account\Referral"/>

But unfortunately it still gives me the 404 page

Whoops, our bad...

Please suggest how to add a new action to existing module routes.

Thanks.

UPDATE

The content of the Referral file

namespace Custom\AccountReferral\Controller\Account;
class Referral extends \Magento\Customer\Controller\AbstractAccount {

    protected $customerRepository;

    protected $dataObjectHelper;
    protected $session;

    protected $resultPageFactory;
    public function __construct(
        Context $context,
        Session $customerSession,
        PageFactory $resultPageFactory,
        CustomerRepositoryInterface $customerRepository,
        DataObjectHelper $dataObjectHelper
    ) {
        $this->session            = $customerSession;
        $this->resultPageFactory  = $resultPageFactory;
        $this->customerRepository = $customerRepository;
        $this->dataObjectHelper   = $dataObjectHelper;
        parent::__construct( $context );
    }

    public function execute() {
        $resultPage = $this->resultPageFactory->create();

        $block = $resultPage->getLayout()->getBlock( 'customer_account_dashboard_referral_edit' );
        if ( $block ) {
            $block->setRefererUrl( $this->_redirect->getRefererUrl() );
        }

        $data               = $this->session->getCustomerFormData( true );
        $customerId         = $this->session->getCustomerId();
        $customerDataObject = $this->customerRepository->getById( $customerId );
        if ( ! empty( $data ) ) {
            $this->dataObjectHelper->populateWithArray(
                $customerDataObject,
                $data,
                \Magento\Customer\Api\Data\CustomerInterface::class
            );
        }
        $resultPage->getConfig()->getTitle()->set( __( 'Referral Settings' ) );

        return $resultPage;
    }
}

This is just an example, I haven't added any logic yet, because it is not accessible.

  • Why name your action Pgp you have this action in directory ? – Evgeniy Kapelko Jun 29 '18 at 20:34
  • @EvgeniyKapelko sorry this was a type, I have edited my question – solderingiron Jun 29 '18 at 21:11
  • show me Referral.php – Evgeniy Kapelko Jun 29 '18 at 21:15
  • @EvgeniyKapelko, I have updated my answer – solderingiron Jun 29 '18 at 21:22
  • Where is namespace? – Evgeniy Kapelko Jun 29 '18 at 21:24
1

You need to inherit this way; Replace this code .

namespace Custom\AccountReferral\Controller\Account;

use Magento\Customer\Controller\Account\Referral;

class extends Referral 
{
 ...
}
| improve this answer | |
  • mmm can you explain better why? – Raul Sanchez Mar 28 '19 at 14:14
1

Starting on as you say that your module works, your controller should be in this path :

app/code/{Vendor}/{Module}/Controller/Account/Custom // Account/Custom is for exemple

The content should be something like this:

<?php

namespace Vendor\Module\Controller\Account;

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

    protected $_registry;
    protected $_pageConfig;

    public function __construct(
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Page\Config $pageConfig
    ) {
        $this->_registry = $registry;
        $this->_pageConfig = $pageConfig;
        parent::__construct($context);
    }


    public function execute() {
        echo "My controller works fine !";
    }
}

Now you can call your controller like this : baseurl/frontname/foldercontroller/filecontroller

IMPORTANT:

To add a new action in core module Magento_Customer you need to extend first that module then you add what you need, I see that you are using customer as frontName in you xml and this frontName 'custom' is already reserved ! you can use it again if you extend his module.

See this to how extend an exsting core module : Magento 2 how to extend core module

Good luck.

| improve this answer | |

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.