Phalcon 3 RuntimeException:尝试在非对象上调用方法 get
Phalcon 3 RuntimeException: Trying to call method get on a non-object
我正在尝试使用 Phalcon 3 编写库代码,但在调用 Cookies 服务时出现错误。
有人能帮帮我吗?
我得到的错误是:
RuntimeException: Trying to call method get on a non-object
下一个是我的代码:
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;
});
authController.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();
}
您在控制器中调用 $this->cookie
,但在您的 DI 中您使用了 cookies
。
所以你需要使用$this->cookies
。
此外,您不需要在控制器中重新创建 cookie 对象。那么使用 DI 有什么意义呢?
我正在尝试使用 Phalcon 3 编写库代码,但在调用 Cookies 服务时出现错误。
有人能帮帮我吗?
我得到的错误是:
RuntimeException: Trying to call method get on a non-object
下一个是我的代码:
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;
});
authController.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();
}
您在控制器中调用 $this->cookie
,但在您的 DI 中您使用了 cookies
。
所以你需要使用$this->cookies
。
此外,您不需要在控制器中重新创建 cookie 对象。那么使用 DI 有什么意义呢?