Hello Magento Devs,
In today’s tutorial, we are going to see how we can get the Base URL path dynamically in any XML File.
In some cases, you are required to get the Base URL and pass it into the XML file.
Instead of passing hardcoded values, we can dynamically get these values and pass them to the XML.
Suppose, You are adding custom breadcrumbs to your account section and you are required to pass the base URL to generate the URL.
You can get a dynamic Base URL using {{baseUrl}} variable in XML.
Let’s consider below example :
/app/code/Jigar/MyAccountBreadCrumbs/view/frontend/layout/customer_account.xml
<?xml version="1.0"?>
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="breadcrumbs">
<action method="addCrumb">
<argument name="crumbName" xsi:type="string">home</argument>
<argument name="crumbInfo" xsi:type="array">
<item name="title" xsi:type="string">Home</item>
<item name="label" xsi:type="string">Home</item>
<item name="link" xsi:type="string">{{baseUrl}}</item>
</argument>
</action>
<action method="addCrumb">
<argument name="crumbName" xsi:type="string">my-account</argument>
<argument name="crumbInfo" xsi:type="array">
<item name="title" xsi:type="string">My Account</item>
<item name="label" xsi:type="string">My Account</item>
<item name="link" xsi:type="string">{{baseUrl}}customer/account</item>
</argument>
</action>
</referenceBlock>
</body>
</page>
As shown in the above example, I have prepared the breadcrumbs URL by adding dynamic {{baseUrl}}.
So it will generate the below URL accordingly.
for Home: https://example.com/first_store_view/
for My Account: https://example.com/first_store_view/customer/account
That’s it for today’s tutorial, See you in the next blog.
Thanks, Keep Supporting !!
Leave a Comment