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

Trying to call method get on a non-object

Hi, I'm trying to write a JWT library with Phalcon 3 and i have some problems when I try to set a cookie with the generated token.

¿Can anyone help me, please?

This is my code:

Loader.php


    $loader->registerDirs(
        [
            $config->application->controllersDir,
            $config->application->modelsDir,
            $config->application->incubatorDir.'Mailer/',
            $config->application->libraryDir.'gAuth/',
        ]
    )->register();

Services.php


    $di->setShared('cookies', function () {
        $cookies = new Cookies();
        $cookies->useEncryption(false);
        return $cookies;
    });

    $di->set('gauth', function(){
        $gauth = new gAuth();
        return $gauth;
    });

myController.php


    public function authAction(){
        $this->gauth->store();
    }

gAuth.php


    use Phalcon\Crypt;
    use Phalcon\Http\Response\Cookies;

    class gAuth
    {
        public    $token;
        protected $cookie;
        private   $secret;
        private   $crypt;

        public function __construct()
        {
            $this->secret = '************;
            $this->crypt = new Crypt();
            $this->cookie = new Cookies();
            $this->cookie->useEncryption(false);
        }

        public function storeToken()
       {
        // here I get the error: Trying to call method get on a non-object
        $this->cookie->set('tkn', '***', time() + 3600, '/', null, 'web.com', null)->send();
        }

The error I get is: RuntimeException: Trying to call method get on a non-object



145.0k
Accepted
answer

Instead of creating cookies and crypt use already one from Di which you registered.