I want to create a new custom Block and Block phtml file same as the Customer Index Block for use by the customer, when the customer login the customer just clicks on the left navigation link and access the phtml file.
I have added the custom link already in the below XML file:
app\design\frontend\Vendor\Theme\Magento_Customer\layout/customer_account.xml
<block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-account-myinformation-link">
<arguments>
<argument name="label" xsi:type="string" translate="true">my information</argument>
<argument name="path" xsi:type="string">customer/account/myinformation</argument>
</arguments>
</block>
After clicking on this link, I want to call Block to get customer data like email, name mobile, address, etc.
Please help me if anyone has created the Custom Block and Custom phtml file in the Magento_Customer module.
3 Answers 3
Create a Module Namspace/Customer
Create xml view/frontend/layout/customer_account_myinformation.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
<referenceContainer name="content">
<block class="NameSpace\Customer\Block\Account\MyInformation" name="myinformation" template="account/myinformation.phtml"></block>
</referenceContainer>
</body>
</page>
Create phtml file view/frontend/templates/account/myinformation.phtml
Add Your Code in phtml
Create Controller Controller/Account/MyInformation.php
<?php
namespace Namespace\Customer\Controller\Account;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Result\PageFactory;
/**
* For MyInformation Controller
*/
class MyInformation extends Action
{
protected $resultPageFactory;
protected $coreRegistry;
/**
* Index constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
\Magento\Framework\Registry $coreRegistry,
PageFactory $resultPageFactory,
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|\Magento\Framework\View\Result\Page
*/
public function execute()
{
$this->initiateCustomer();
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('My Information'));
return $resultPage;
}
}// end class
create Block Block\Account\MyInformation.php
<?php
namespace NameSpace\Customer\Block\Account;
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\Session;
class MyInformation extends Template
{
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
protected $customerSession;
protected $url;
/**
* Cancel constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
Template\Context $context,
array $data = [],
Session $customerSession,
) {
$this->customerSession = $customerSession;
parent::__construct($context, $data);
}
/**
* Retrieve current order model instance
*
* @return \Magento\Sales\Model\Order
*/
public function getCustomer()
{
return $this->customerSession->getCustomer();
}
}//end class
create routes.xml 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="namespace_customer">
<module name="Namespace_Customer"/>
</route>
</router>
</config>
Hope it will help you. Please vote us if you like our answer.
I think you need to extend Magento_Customer by creating a new controller customer/account/myinformation in your module with adding configuration.
<?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="customer" frontName="customer">
<module name="Magento_Customer" />
</route>
</router>
</config>
Next, you can create a layout and blocks.
Add a new link to My Account: https://webkul.com/blog/account-navigation-link-magento2/
-
thanks for your answer. Could you please share the complete answer here. I am still stuck at this module. I will accept and upvote you answer.mageDev0688– mageDev06882017年05月08日 06:01:38 +00:00Commented May 8, 2017 at 6:01
-
@mageDev0688 I found answer early that wrote additional information :) Try this magento.stackexchange.com/a/121359/40609Mykhailo Shatilov– Mykhailo Shatilov2017年05月08日 13:02:39 +00:00Commented May 8, 2017 at 13:02
-
thanks I have created the module, but right now it's giving 404 error.mageDev0688– mageDev06882017年05月15日 11:16:12 +00:00Commented May 15, 2017 at 11:16
Follow bellow steps
Step: 1 (module.xml) app/code/Ccc/HelloWorld/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="Ccc_HelloWorld" setup_version="1.0.1">
</module>
</config>
Step: 2 app/code/Ccc/HelloWorld/composer.json
{
"name": "ccc/helloworld",
"description": "Ccc HelloWorld",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"amasty/base": "*"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"Commercial"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Ccc\\HelloWorld\\": ""
}
}
}
Step: 3 (routes.xml) app/code/Ccc/HelloWorld/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="helloworld" frontName="helloworld">
<module name="Ccc_HelloWorld" />
</route>
</router>
</config>
Step: 4 (registration.php) app/code/Ccc/HelloWorld/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Ccc_HelloWorld',
__DIR__
);
Step: 5 (Index.php) app/code/Ccc/HelloWorld/Controller/Index/Index.php
<?php
namespace Ccc\HelloWorld\Controller\Index;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
/** * @param \Magento\Framework\App\Action\Context $context */
public function __construct(\Magento\Framework\App\Action\Context $context,PageFactory $resultPageFactory)
{
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
return $resultPage;
}
}
Step: 6 (HelloWorld.php) app/code/Ccc/HelloWorld/Block/HelloWorld.php
<?php
namespace Ccc\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}
Step: 7 (helloworld_index_index.xml) app/code/Ccc/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Ccc\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml">
</block>
</referenceContainer>
</body>
</page>
Step: 8 (helloworld.phtml) app/code/Ccc/HelloWorld/view/frontend/templates/helloworld.phtml
<?php
echo 'Successful! This is a simple module in Magento 2.0';
?>
Step: 9 Execute the commands
bin/magento module:enable Ccc_HelloWorld
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy
Explore related questions
See similar questions with these tags.