Retrieving Magento Page URL Key for Conditional Statements
Magento is a powerful e-commerce platform that allows you to manage your online store efficiently. One of the common requirements while developing custom themes or plugins is to retrieve the URL key of the current page for use in conditional statements.
Understanding URL Keys in Magento
The URL key in Magento is generated based on the title of the CMS page, category, product, etc., and it represents the friendly URL for that specific resource. The URL key helps identify the page uniquely within your store’s catalog structure.
You can find the URL key field while editing any CMS page or other resources in the Magento admin panel under the “URL Key” tab, typically placed next to the “Title” field:

Retrieving the Current Page’s URL Key
To retrieve the URL key of the current page within a Magento template, you can use the built-in $this->getRequest()->getPathInfo() method.
Here’s how to do it:
<?php $currentUrlKey = $this->getRequest()->getPathInfo(); ?>
The getPathInfo() method returns the path component of the current request as a string. It includes the URL key of the current page, along with any additional path information.
For example, if you are on a CMS page with the URL key “about-us” and no sub-paths or parameters, $currentUrlKey will contain “/about-us”.
Using the URL Key in Conditional Statements
Once you have retrieved the URL key, you can use it to create conditional statements within your template file. For instance, let’s say you want to display a special message only on the “contact-us” page:
<?php if ($currentUrlKey == "/contact-us"): ?>
<p class="special-message">Get in touch with us today!</p>
<?php endif; ?>
In this example, the if statement checks whether the current URL key matches exactly “/contact-us”. If it does, the special message will be displayed on that specific page.
Handling Sub-paths and Parameters
If your pages have sub-paths or parameters in the URL (e.g., “about-us/sub-page” or “product/category/123”), you can use explode() to separate the components:
<?php $urlKeyParts = explode("/", $this->getRequest()->getPathInfo()); ?>
Now, $urlKeyParts will be an array containing each component of the URL path. You can access specific parts using their index.
For more advanced functionality like regular expressions or complex conditional logic, consider using Magento’s view helpers and controllers to separate business logic from your templates.
Conclusion
Retrieving the URL key of a Magento page is straightforward once you understand how to access it through the request object. By utilizing this information in conditional statements within your templates, you can create dynamic and personalized user experiences for your store visitors.
Remember that properly managing URLs and organizing your catalog structure can significantly improve both SEO performance and user navigation within your Magento store.
For more advanced customizations and best practices in Magento development, consider exploring the extensive resources available at Laravel Company. They offer expert guides on various aspects of e-commerce development using platforms like Magento and Laravel.