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

Run executeQuery in function in Micro App

I have followed the API tutorial.

I have everything working.

Now I am creating an authentication function that I will call at the beginning of each api call.

Example:

$app->get('/api/robots', function() use ($app) {
 $auth_data=is_authorized($app);
 ...
 ..

So now in the "index.php" file I have a function titled "is_authorized"

The function runs fine however I cannot execute SQL within this function.

The following example does not execute within the is_authorized function.

$phql = "SELECT * FROM Members ORDER BY id";
            $robots = $app->modelsManager->executeQuery($phql);

While the above will run within my initial api call function it does not run in the is_authorized function. Is there a way to pass "$app" to this function so that I can use it to execute queries?



6.6k
Accepted
answer

This issue was unrelated to Phalcon and more of a issue with my understanding of how to pass by reference in PHP 5.4.

See this post for the reason on why I was running into the issue.

https://stackoverflow.com/questions/8971261/php-5-4-call-time-pass-by-reference-easy-fix-available

Please close the thread if you solved your problem. If you are still looking for a solution take a look at the following code:

class BaseController extends \Phalcon\Mvc\Controller
{
    private function isAuthorized()
    {
        //Here you can execute your PHQL queries
    }

    public function initialize()
    {
        $auth_data = $this->isAuthorized();
        //[...]
    }
}

class RandomController extends BaseController
{
    //Your API actions
}