Search code examples
phpsymfony1class-visibility

How to render an action from another action in Symfony 1


I am stuck with a Symfony application using version 1.0.17 which can't be upgraded at the moment.

Right now there is a page that contains an iframe which loads a webpage of a different action from the same module. The iframe is causing some design issues as well and some user-experience issues so I want to get rid of it and just render the HTML from the iframe directly in the page.

I just can't figure out how to execute another action and render the view template into a variable that I can assign to the calling action's view.

I've tried getPresentationFor() but that either results in a 404 on the calling page, an exception or fatal error depending on how I try it.

I think I need to put the code in the execute method of the action. The controller is an sfAction object. If I call $this->getController() I get an sfWebController object.

Calling:

$this->getController()->getPresentationFor('module', 'ContactIframeAction');

Results in a blank page; any code after that call does not get executed but if I output something before it I can see it on the page. No errors in the server error log.

Calling:

$this->getController()->getPresentationFor('module', 'ContactIframe');

just causes our 404 page to show so I think the previous call is closer to what I want.

Is there a way to render the output of another action from another action's code?

Thanks.


Solution

  • It's probably

    $this->theContent = $this->getController()->getPresentationFor('module', 'contactIframe'); // minus 'c'
    

    Use :

    // modules/mymodule/actions/actions.class.php
    class myPageActions extends sfActions {
    
        public function executeIndex() {
            // ...
            $this->theContent = $this->getController()->getPresentationFor('module', 'contactIframe');                  
        }
    }
    
    // modules/mymodule/templates/indexSuccess.php
    <?php echo $theContent ?>