Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to revive an old project of mine. It was built with phalconphp 2 or 3 (I don't even remember which version, but quite old). There, I could include a call to the loop inside the controller, so a different controller action's output could be embedded into the output, just by calling

PHP
$this->app->request(['controller' => 'OtherController', 'action' => 'action', 'params' => []])

But now using Phalcon 5, this doesn't work anymore ("Access to undefined property app"), and I couldn't find anything about this in the docs.
How can I make it work?

What I have tried:

I tried to use $this->dispatcher, but got an infinite loop. So it doesn't work.
Posted
Updated 28-Aug-23 9:23am
v2

The error message is telling you that there is no variable named app defined anywhere. So you need to look at the rest of your code to find out why.
 
Share this answer
 
You need to use the appropriate 'use' statement at the top of your controller in Phalcon 5 -
PHP
use Phalcon\Mvc\Dispatcher;


Inside your controller action, you can use the Dispatcher to forward the request to another controller's action and capture its output, your code will be something like the following, similar but with changes from the older Phalcon versions -
PHP
use Phalcon\Mvc\Dispatcher;

public function yourAction()
{
    //Get the dispatcher instance...
    $dispatcher = new Dispatcher();

    //Forward your request to a different controller's action...
    $forwardedResponse = $dispatcher->forward([
        'controller' => 'OtherController',
        'action' => 'otherAction', //Change this to your newly needed action...
        'params' => [] //You can pass parameters here as needed...
    ]);

    //Get the output...
    $embeddedOutput = $forwardedResponse->getContent();

    //Your current action logic here...
    
    //Now you can use $embeddedOutput where it is needed...    
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900