We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to get content from other controller/action

<?php
// we enter to IndexController->indexAction
class IndexController extends ControllerBase
{

    public function indexAction()
    {
        // take content from first controller 
        $content1 = getContentFromController('IndexController', 'firstAction');
        //take content from second controller
        $content2 = getContentFromController('MyController', 'indexAction');
        echo $content1 . $content2; // i want to take this string "<h1>hello world</h1>"
    }

    public function firstAction()
    {
        echo '<h1>hello ';
    }
}

//another controller
class MyController extends ControllerBase
{

    public function indexAction()
    {
        echo 'world</h1>';
    }
}

Anybody know how i can do it???



1.6k
edited Oct '14

try this

<?php

  // use the getRender method of view   getRender('controllerName', 'actionName', params) 

$content1 = $this->view->getRender('my', ' index', null);
$content2 = $this->view->getRender('index ', ' index', null);

echo $content1,$content2;

//Note : do not disable views on other controllers 


1.1k
edited Oct '14

Thanks, but i couldn't get content by this method. Do you have another idea?



1.6k
edited Oct '14
<?php

//make sure that the view file exists 
//for example "views/index/index.phtml" and it has the getContent() function 

//IndexController  
{
    .
    .
    public function indexAction(){
        echo "data to display";
    }
}

//index.phtml 
echo $this->getContent();