how to call phtml file in controller?

how to call phtml file in controller?

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to call phtml file in controller?

Displaying a Mini Cart in the Topbar Using Magento’s Block System

The Problem: Calling the PHTML File from a Controller

In your current setup, you’ve created a topcart.phtml file located at app/design/frontend/default/mytheme/template/checkout/cart/mycart.phtml. This file contains the HTML and PHP logic for displaying the mini cart in the topbar.

You’re trying to call this block from your controller using:

$sidebar_block = $this->getLayout()->getBlock('mycart');
$sidebar = $sidebar_block->toHtml();
$response['sidebar'] = $sidebar;

However, you’re encountering a “Fatal error: Call to a member function toHtml() on a non-object” issue.

The Solution: Defining the Block in Your Layout

The problem lies in how you’re referencing the block. In Magento, blocks are defined in layout files (XML) and then accessed through their names or IDs.

  1. Create a new layout file: Go to app/design/frontend/default/mytheme/layout and create a new XML file, e.g., mycart.xml.

  2. Define your block: Open the newly created mycart.xml file and add the following content:

<?xml version="1.0"?>
<layout>
    <default>
        <!-- Define your block -->
        <reference name="topbar">
            <block type="checkout/cart_mini" name="mycart" as="mycart" template="checkout/cart/mycart.phtml"/>
        </reference>
    </default>
</layout>

In this XML, we’re defining a new block called mycart of type checkout/cart_mini. We specify the template to use (mycart.phtml) and where it should be placed in the layout (topbar).

  1. Accessing the Block from Your Controller

Now that you’ve defined your block in the layout, you can access it from your controller like this:

$block = $this->getLayout()->createBlock('checkout/cart_mini', 'mycart');
$html = $block->setTemplate('checkout/cart/mycart.phtml')->toHtml();
$response['sidebar'] = $html;

Here, we’re using createBlock to instantiate the block with the specified type and name. We then set the template and call toHtml() to render it.

Why This Works

By defining your block in the layout XML file, you ensure that Magento is aware of its existence and knows where to place it. When you access this block from your controller using the correct method (createBlock), you avoid the “non-object” error because the object actually exists.

Conclusion: Proper Block Usage for Custom Mini Cart Display

To display a custom mini cart in the topbar, always follow these best practices:

  1. Create your PHTML template file.
  2. Define your block in an XML layout file, specifying its type, name, and location.
  3. Access the block from your controller using createBlock instead of getLayout->getBlock.

This approach ensures your code is clean, maintainable, and follows Magento’s intended architecture for block usage.

Remember, you can find more detailed guides on layout XML files and block creation in the official Magento Developer Documentation.

Happy coding!

Checkout Themes Errors

Our network