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

Using function from controller in anothercontroller

Hello World!

I got two Controllers: JsonController and MapController.

In the JsonController I got a public function namend "getJson($param,$id)". This functions calls other private function, depending on the $param.

My MapController needs information about some planets, so I use

    $jsonController = new \Pegasus2540\Frontend\Controllers\JsonController();

    $json = $jsonController->getJson("planets", 1);

in the showAction() of my MapController to get the information.

My getJson() function looks like this

public function getJSON($param, $id) {
    echo "You would like to get: ".$param."-".$id;
    switch ($param) {
        case("planets"):
            $data = $this->getPlanets($id);
            break;
        case("solarsystem"):
            $data = $this->getSolarsystem($id);
            break;
        default:
            break;
    }

    echo json_encode($data);
    return json_encode($data);
}

Now, when I call the map in my browser, I expect something like "You would like to get planets-1" and my json.

But it seems like it doenst work. No echo, no json.

edited Mar '16

Are you sure your routes are working? I tested your exact scenario with two random controllers and it works. Perhaps try adding exit; after your first echo in getJson method.

In general a better solution would be to move the code from your json controller into library/helper file or a model if it is db related. This way you will be able to use it anywhere you want without violating MVC principles.



920

Yes, I am sure. When I call the Jsoncontroller like this MYURL/json/get/planets/1 it works.

I will try the library/helper variant. :)

edited Mar '16

Create service and put getJSON there. And then access this service in your controller. You shouldnt call method from one controller in other controller.



920
edited Mar '16

Okay, I changed it to this now:

I created a folder called library inside the root of my applicataion. there is a connector.php:

namespace Pegasus2540\Library;

class Connector {
public function hello($string) { return "Hello ".$string; } }

In my Module.php I register the Namespace like this:

    $loader->registerNamespaces([
        'Pegasus2540\Frontend\Controllers' => __DIR__.'/controllers/',
        'Pegasus2540\Library'    => '/PATH TO APP/game/library/',
    ]);  

In my MapController:

    $c = new \Pegasus2540\Library\Connector;
    echo $c->hello("World");

Now I get a WhiteScreen. No view is loaded and no echo.

I took in the apache error.log and found this.

Unable to load dynamic library '/usr/lib/php5/20121212/phalcon.so' - /usr/lib/php5/20121212/phalcon.so: undefined symbol: php_pdo_get_dbh_ce in Unknown on line 0

Sounds like a clue to me, but googling this error only show me solutions at installing phalcon, but I have already installed phalcon (and it works).

I will google again for this error until I find a solution. I think outsourcing the library will do it, so thanks for your help! :)

phalcon.so should be loaded AFTER pdo, database extensions etc. Best load phalcon.so as last extension. Also this can mean that you dont have pdo extension loaded.