2

I have a default theme folder with all my templates which is inside

  • mysite/app/code/template/frontend/rwd/default.

I have another folder named tree(not inside default theme). I have phtml file inside tree. How do i call that file from controller? right now the path would be

  • mysite/tree/myfile.phtml.

My controller is inside

  • app/code/local/ness/so/indexcontroller.php
6

Thanks for asking on the site. There are a few problems with the way you're trying to do this.

First: your controller, app/code/local/ness/so/indexcontroller.php, should really exist in app/code/local/Ness/So/controllers/IndexController.php -- as that's proper placement for Magento [ref]. The referenced article will also walk you through the process of registering that controller within your Ness_So module.

Your paths are also wrong, both what you reference for rwd/default (so maybe a typo?) and also your custom mysite/tree/myfile.phtml cannot really exist at that location. Instead, it needs to share a similar directory structure as the other theme templates, eg: mysite/app/design/frontend/rwd/default/template/tree/myfile.phtml.

After fixing that comes another learning point. Controllers and templates are separated by another layer in Magento called the block. This is a class which can render and serve the template. The job of the controller is to load the entire layout and trigger the rendering process from the top of the chain [ref].

Now you can alternatively circumvent a lot of that process by creating a block and assigning to it your template all within the controller. So that might look like this:

# File: app/code/local/Ness/So/controllers/IndexController.php
class Ness_So_IndexController
  extends Mage_Core_Controller_Front_Action
{

    // Example URL to hit this action: mysite.com/so/index/index
    public function indexAction()
    {
        // Create a generic template block
        $block = $this->getLayout()->createBlock('core/template');

        // Assign your template to it
        // This path is relative to your current theme (eg: rwd/default/template/...)
        $block->setTemplate('tree/myfile.phtml');

        // Render the template to the browser
        echo $block->toHtml();
    }

}

So while you can do anything you want with Magento using PHP, there are definitely some patterns that will work to your advantage once you learn them.

It sounds like you have a lot to learn about Magento, and that's great! In addition to the resources I've mentioned, here are some more to help you get started:

4
  • all my files needs to be inside rwd? I can not connect to anything outside rwd? – beginner Mar 20 '15 at 1:12
  • You can. Although what I mentioned was the easiest route. The next easiest way to do this is to create a branch from rwd/default, let's say a sibling folder at rwd/mytheme. This would begin to define a new theme. Then, your template would go into rwd/mytheme/template/tree/myfile.phtml -- but depending on your setup you might need to setup a theme fallback scheme [ref]. If not, you will at least have to configure mytheme as the new theme in Magento admin under System > Configuration > Design > Theme. – Rick Buczynski Mar 20 '15 at 1:18
  • gotcha, then how do I use geturl method to access this link, localhost/test/tt/sharepreview.php? – beginner Mar 20 '15 at 1:21
  • Mage_Core_Model_Url::getUrl is used all over Magento, but is only really needed in the context of Magento's routing system. So if your URL is not a standard route, eg: module/controller/action [ref] then you don't even need this method. You can just put an absolute URL inside the template. If it does point to a proper route, then you can do something like this inside the template: <a href="<?php echo $this->getUrl('module/controller/action'); ?>">Link</a> and it will generate the full URL at run-time. – Rick Buczynski Mar 20 '15 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.