We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.