Hello, Magento developers! In this tutorial, we'll explore how to pass a custom variable from a controller to a PHTML file in Magento 2.4.x. Sometimes, you may need to perform certain operations or calculations in the controller and then display the results in the corresponding template (PHTML) file. Let's dive into this step-by-step guide with practical examples!
Step 1: Create a Custom Controller
First, let's create a custom controller where we'll define the custom variable and its value. For example, create a file named CustomController.php
under the following directory:
app/code/[Vendor]/[Module]/Controller/Index
Step 2: Define the Custom Variable in the Controller
In the CustomController.php
, implement the custom logic and define the variable you want to pass to the PHTML file. For example:
<?php
namespace [Vendor]\[Module]\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class CustomController extends Action
{
protected $resultPageFactory;
public function __construct(Context $context, PageFactory $resultPageFactory)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
// Custom logic to calculate or fetch the value for the variable
$customValue = "Hello, Magento 2!";
// Pass the custom variable to the PHTML file
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->set(__('Custom Page'));
$resultPage->getLayout()->getBlock('custom.block')->setData('custom_variable', $customValue);
return $resultPage;
}
}
Step 3: Create the PHTML File
Now, let's create the corresponding PHTML file where we'll use the custom variable. For example, create a file named custom.phtml
under the following directory:
app/code/[Vendor]/[Module]/view/frontend/templates
Step 4: Access the Custom Variable in the PHTML File
In the custom.phtml
file, you can access the custom variable passed from the controller using $block
object. For example:
<!-- app/code/[Vendor]/[Module]/view/frontend/templates/custom.phtml -->
<div>
<h1><?php echo $block->getData('custom_variable'); ?></h1>
</div>
Step 5: Create the Layout File
Next, create a layout file to specify the custom controller's action and the PHTML file to be rendered. For example, create a file named customcontroller_index_custom.xml
under the following directory:
app/code/[Vendor]/[Module]/view/frontend/layout
Add the following content to the customcontroller_index_custom.xml
file:
<?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">
<body>
<referenceContainer name="content">
<block class="[Vendor]\[Module]\Block\CustomBlock" name="custom.block" template="[Vendor]_[Module]::custom.phtml" />
</referenceContainer>
</body>
</page>
Step 6: Test the Custom Controller and PHTML File
Now, visit the custom controller's URL (e.g., http://your_magento_site/customcontroller/index/custom
) in your browser. The custom variable value should be displayed in the rendered PHTML file.
Congratulations! 🎉 You've successfully passed a custom variable from a controller to a PHTML file in Magento 2.4