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

Undefined Variable $app

Managed to setup the Restful API tutorial on an Ec2 server.

when I access the first function I get the following:

[Sun May 17 07:11:41.406058 2015] [:error] [pid 8393] [client XXX.XXX.XXX.XXX:51964] PHP Notice: Undefined variable: app in /var/XXXX/XXXX/XXXX/index.php on line 13 [Sun May 17 07:11:41.406086 2015] [:error] [pid 8393] [client XXX.XXX.XXX.XXX:51964] PHP Notice: Trying to get property of non-object in /var/XXXX/XXXX/XXXX/index.php on line 13 [Sun May 17 07:11:41.406092 2015] [:error] [pid 8393] [client XXX.XXX.XXX.XXX:51964] PHP Fatal error: Call to a member function executeQuery() on a non-object in /var/XXXX/XXXX/XXXX/index.php on line 13

Code for my index.php is as follows:

use Phalcon\Mvc\Micro; use Phalcon\Http\Response;

$app = new Micro();

//Retrieves all organisations $app->get('/api/organisation', function() {

$phql = "SELECT * FROM XXXXXXXX ORDER BY XXXXXXXXX";
$organisation = $app->modelsManager->executeQuery($phql);

$data = array();
foreach ($organisation as $org) {
    $data[] = array(
        'id'    => $org->XXXXXX,
        'name'  => $org->XXXXXXX,
    );
}

Any help would be much appreciate. I have checked that the compile and install went ok. 

Thanks

iQiniso

echo json_encode($data);

});



58.4k
Accepted
answer
edited May '15

Becasue you not include app in anonymuch function, try again

//Retrieves all organisations p

$app->get('/api/organisation', function() use ($app) {

$phql = "SELECT * FROM XXXXXXXX ORDER BY XXXXXXXXX";
$organisation = $app->modelsManager->executeQuery($phql);

$data = array();
foreach ($organisation as $org) {
    $data[] = array(
        'id'    => $org->XXXXXX,
        'name'  => $org->XXXXXXX,
    );
}

Any help would be much appreciate. I have checked that the compile and install went ok. 

Thanks

iQiniso

echo json_encode($data);

});

By the way refer to https://docs.phalcon.io/en/latest/reference/tutorial-rest.html

Hello Thien!

Thanks for you quick response. I am still learning and appreciate you taking the time to respond so quickly.

Thanks and have a great week further.

Tobius



58.4k

I'm glad to hear that , also you can checkout tips phalcon here https://phalcontip.com/

Awesome! Looks like I am going to be up till 4am tonight.

Really nice framework. Really easy to work with and speeds development like I cannot believe.