I'm actually following the tutorial of Pierre Fay but i'm working on a page in the admin side but i don't get the correct way to put a second class "Mymodule\Admin_Page\Block\Contactlists" idea someone ?
<?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">
<head>
<title>Module Mymoodule</title>
</head>
<body>
<referenceContainer name="content">
<block class="Magento\Backend\Block\Template" template="Mymodule_AdminPage::nettoyage.phtml"/>
</referenceContainer>
</body>
</page>
Solved! Go to Solution.
check namespace and xml block name
namespace Mymodule\Admin_Page\Block\Adminhtml;
in xml you write
Mymodule\AdminPage\Block\Adminhtml\ContactslistClear difference Admin_Page and AdminPage
<?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">
<head>
<title>Module Mymoodule</title>
</head>
<body>
<referenceContainer name="content">
<block class="Mymodule\Admin_Page\Block\Contactlists" template="Mymodule_AdminPage::nettoyage.phtml"/>
</referenceContainer>
</body>
</page>Now create your block class,
You can extend base class like this,
<?php
namespace Mymodule\AdminPage\Block\Adminhtml;
class Contactlists extends \Magento\Backend\Block\Template
{
public function mycustom(){
//your code
return;
}
}Remove var/generation folder.
I tried but if i delete the :
class="Magento\Backend\Block\Template"
It no longer work probably because he need it in the admin side.
in admin side you need to use Magento\Backend\Block\Template;
I have my block Contactslist
<?php
namespace Mymodule\AdminPage\Adminhtml\Block;
use Magento\Backend\Block\Template;
class Contactslist extends \Magento\Backend\Block\Template
{
public function test()
{
echo 'helloworld';
}
}and my layout
<?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">
<head>
<title>Module Mymodule</title>
</head>
<body>
<referenceContainer name="content">
<block class="Mymodule\AdminPage\Block\Adminhtml\Contactslist" template="Mymodule_AdminPage::nettoyage.phtml"/>
</referenceContainer>
</body>
</page>but at the end i have a whithe page and the log tell me
Fatal error: Cannot redeclare class Mymodule\\AdminPage\\Adminhtml\\Block\\Contactslist in /home/mademonstration/public_html/newmagento3/app/code/Mymodule/AdminPage/Block/Adminhtml/Contactslist.php on line 12
@Antoine553 which is your line no. 12,
You can set class definataion in your block file like this,
<?php
namespace Mymodule\AdminPage\Block\Adminhtml;
use Magento\Backend\Block\Template;
class Contactslist extends Template
{
public function test()
{
echo 'helloworld';
}
}
namespace is wrong
it should be
namespace Mymodule\AdminPage\Block\Adminhtml;
the line 12 is the last one with a "}"
i tried your code but it don't work
i did it but nothing change
actually i have
<?php
namespace Mymodule\Admin_Page\Block\Adminhtml;
use Magento\Backend\Block\Template;
class Contactslist extends \Magento\Backend\Block\Template
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}
public function mycustom(){
echo "test";
}
}