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) {
            $data = $this->getPlanets($id);
            $data = $this->getSolarsystem($id);

    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.


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.

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:

      '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/' - /usr/lib/php5/20121212/ undefined symbol: phppdogetdbhce 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! :) should be loaded AFTER pdo, database extensions etc. Best load as last extension. Also this can mean that you dont have pdo extension loaded.