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

"Call to undefined function getPost()" in basic tutorial

I've completed the basic tutorial successfully on my setup already (a long while ago), and it still works.

I'm building a new site from scratch using this same tutorial as a base and refresher, copying by hand to gain intimate knowledge. So I welcome errors like this, but can't seem to crack this one.

My "SignupController.php" and "public/index.php" bootstrap look identical to the functioning set of counterparts in form, at least to me (except the updated absolute "BASE_PATH" and "APP_PATH" define() 's and references, which haven't given any problems thus far). Switching to the old ".." relative path changed nothing.

For a brief period I noticed that the parenthesis were missing on my " new FactoryDefault();" construction and assignment, and thought naively "Hey that's it!", saved it and refreshed. But my application would have never even redirected if the $di wasn't successfully inserted; and, the same error continues. Surprisingly to me, the interpreter must have figured out what I meant there, thus far.

So I assume "$this" (and the $di) is not being properly made available to my SignupController's registerAction() . But that should all be in the FactoryDefault, beyond my control, assuming the bootstrap loader is working (How could it not be if the views and other controllers are working?).

What kind of typo could cause this error in the Apache error.log ?:

[Wed May 02 07:37:13.334042 2018] [:error] [pid 3845] [client ::1:34984] PHP Fatal error:  Uncaught Error: Call to undefined function getPost() in /var/www/html/LHMSite/app/controllers/SignupController.php:17\nStack trace:\n#0 [internal function]: SignupController->registerAction()\n#1 [internal function]: Phalcon\\Dispatcher->callActionMethod(Object(SignupController), 'registerAction', Array)\n#2 [internal function]: Phalcon\\Dispatcher->dispatch()\n#3 /var/www/html/LHMSite/public/index.php(66): Phalcon\\Mvc\\Application->handle()\n#4 {main}\n  thrown in /var/www/html/LHMSite/app/controllers/SignupController.php on line 17, referer: https://localhost/LHMSite/signup

"Dysfunctional" SignupController.php, Please excuse the unorthodox model classname :

 <?php

use Phalcon\Mvc\Controller;

class SignupController extends Controller
{
    public function indexAction()
    {

    }
    public function registerAction()
    {
        $tutSignup = new Tutsignup();

        // Store and check for errors
        $success = $tutSignup->save(
            $this->request-getPost(),
            [
                "name",
                "email",
            ]
        );
        if ($success) {
            echo "Thanks for registering!";
        } else {
            echo "Sorry, the following problems were generated:";

            $messages = $tutSignup-getMessages();

            foreach ($messages as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }
        $this->view->disable();
    }
}

"Functional" SignupController.php:

<?php

use Phalcon\Mvc\Controller;

class SignupController extends Controller
{
    public function indexAction()
    {

    }

    public function registerAction()
    {
        $user = new Users();

        $success = $user->save(
            $this->request->getPost(),
            [
                "name",
                "email",
            ]
        );

        if ($success) {
            echo "Thanks for registering!";
        } else {
            echo "Sorry, the following problems were generated: ";

            $messages = $user->getMessages();

            foreach ($messages as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }
        $this->view->disable();
    }
}

"Dysfunctional" /public/index.php bootstrap:

<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

// Define some absolute path constraints to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
//... CJE - apparently more could be defined? "..."

$loader = new Loader();

$loader->registerDirs(
    [
        APP_PATH . '/controllers/',
        APP_PATH . '/models/',
    ]
);

$loader->register(); 

//creat a DI

$di = new FactoryDefault();

$di->set(
    'view',
    function () {
        $view = new View();
        $view->setViewsDir(APP_PATH . '/views/');
        return $view;
    }
);

$di->set(
    'url',
    function () {
        $url = new UrlProvider();      
        $url->setBaseUri('/LHMSite/');
        return $url;
    }
);

$di->set(
    'db',
    function () {
        return new DbAdapter(
            [
                'host'     => 'localhost',
                'username' => 'chris_dev',
                'password' => '[PRIVATE]',
                'dbname'   => 'lhm_dev',
            ]
        );
    }
);

$application = new Application($di);
try {
    $response = $application->handle();
    $response->send();
} catch (\Exception $e) {
    echo 'Exception: ', $e->getMessage();
}

Functional /public/index.php bootstrap:

<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

// Register an autoloader
$loader = new Loader();

$loader->registerDirs(
    [
        "../app/controllers/",
        "../app/models/",
    ]
);
// -CJE- registers the autoLoad() function in the Loader object 
// as the official __autoLoad() function
$loader->register();

// Create a DI
$di = new FactoryDefault();

// Setup the view component
$di->set(
    "view",
    function () {
        $view = new View();

        $view->setViewsDir("../app/views/");

        return $view;
    }
);

// Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set(
    "url",
    function () {
        $url = new UrlProvider();

        $url->setBaseUri("/tutorial/");

        return $url;
    }
);

// Setup the database service
$di->set(
    "db",
    function () {
        return new DbAdapter(
            [
                "host"     => "localhost",
                "username" => "chris_dev",
                "password" => "[PRIVATE]",
                "dbname"   => "test_db",
            ]
        );
    }
);

$application = new Application($di);

try {
    // Handle the request
    $response = $application->handle();

    $response->send();
} catch (\Exception $e) {
    echo "Exception: ", $e->getMessage();
}


77.7k
Accepted
answer
edited May '18

You have two typos in the erroneous controller class:

            $this->request-getPost()

should be

            $this->request->getPost()

and

            $messages = $tutSignup-getMessages();

should be

            $messages = $tutSignup->getMessages();

Good luck!

EDIT: Using an IDE with syntax highlighting and auto-completion really helps with problems like these. I can recommend PhpStorm, but it's not free...

Netbeans for PHP is free and it will do the same

Thank you both! *-_-