How to get content from other controller/action using dispatcher->forward();

From this document http://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

And this example:

<?php

class IndexController extends ControllerBase
{

    public function indexAction()
    {
        $this->tag->setTitle('Home');
        parent::initialize();
    }


    public function page2Action()
    {
        $this->tag->setTitle('Page 2');
        parent::initialize();
    }


    public function page2aAction()
    {
        echo 'This is page 2 a content.';
    }


    public function page3Action($roll = '', $track = '')
    {
        echo 'hi<br>';
        echo $this->dispatcher->getParam('roll') . '<br>';// require defined route.
        echo $this->dispatcher->getParam('track') . '<br>';// require defined route.
        echo $this->dispatcher->getParam('pagename') . '<br>';// require defined route.
        echo '<hr>';
        echo $roll . '<br>';
        echo $track . '<br>';
        echo '<hr>';

        // I think this is like hmvc request.
        $this->dispatcher->forward(
            array(
                'controller' => 'index',
                'action' => 'page2',
            )
        );
        echo $this->dispatcher->getReturnedValue();// nothing come out.

        echo '<hr>';
        $this->dispatcher->forward(
            array(
                'controller' => 'index',
                'action' => 'page2a',
            )
        );

        $this->view->disable();
    }

}

I can use dispatcher->forward() to get content from page2aAction but not in page2Action. If I want to get page content I must always use echo?

You shouldn't reall be using any echoes in your controller. All output is best contained in the views.



3.7k

@Gareth, I already know that i should use views. But my question is how to get content from other controller/action (or method). If i use view, i cannot get content from that controller/action.

As i describe...

If I want to get page content I must always use echo?

Please try it and you will understand what i want and how the result is.



30.4k

Hi,

Well, I've played a bit with your code and I think I understand what you're saying.

Maybe my solution is dirty work, but this is what I'm using: ajax get request on document ready to retrieve output from saying IndexController/page2action.