Where is local.xml in M2
-
28-12-2020 - |
Question
I'm just starting Magento 2 and I see a lot of things that are gone !
Can somone can explain me this with the best practices of course:
I inquired and I found that: the layout xml are loaded according to the layout handles
ex: if we want add some block to the home page, we should create cms_index_index.xml
and we add the needed block inside right ? so where is exactly the path to create it.
ex2: supposing that i have my module app/code/Am/Prince/view/frontend/layout/prince_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
<body>
<referenceContainer name="content">
<block class="Am\Prince\Block\Block" name="home-block1" template="home-block1.phtml" />
</referenceContainer>
</body>
</page>
If I need to display this block home-block1
also in home page can I declare it in this xml ?
.So where is local.xml like M1 ?, wonderfully we have to declare it once and we put all the changes on it with prefexing the layout handle and the block inside something like:
<cms_index_index>
<reference name="content">
...
</cms_index_index>
Solution
To add your custom block on homepage you need to do this things.
create
cms_index_index.xml
file at following location.app/code/Am/Prince/view/frontend/layout/cms_index_index.xml
Now add block code into that file like this.
<?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="Am\Prince\Block\Block" template="Am_Prince::home-block1.phtml"/> </referenceContainer> </body> </page>
OTHER TIPS
There is no local.xml equivalent for layout in Magento 2. There are, however, a couple of ways to solve your requirement:
- If you're only looking to add the block on 2 pages then separate layout files should be used. For example cms_index_index.xml to add it to the homepage.
- If you want the block to appear on every page in your website you can create a default.xml in your module and reference your block in there. The path for this would be app/code/[Vendor]/[Module]/view/frontend/layout/default.xml