致命错误:在LanguageComponent上的非对象上调用成员函数read()
I am having this line in my CakePHP code: $language = $this->Cookie->read('language');
and I'm getting this error:
Fatal error: Call to a member function read() on a non-object in
C:\Apache24\htdocs\cake\app\Controller\Component\LanguageComponent.php on line 27
Here is my LanguageComponent.php code
<?php
//App::Import('Component', 'Cookie');
class LanguageComponent extends Object {
public $controller = null;
public $components = array('Cookie');
public $languages = array();
public function initialize($controller) {
$this->controller = $controller;
if (empty($languages)) {
$this->languages = Configure::read('Config.languages');
}
$this->set();
}
public function set($language = null) {
$saveCookie = false;
if (empty($language) && isset($this->controller)) {
if (!empty($this->controller->params['named']['lang'])) {
$language = $this->controller->params['named']['lang'];
} elseif (!empty($this->controller->params['url']['lang'])) {
$language = $this->controller->params['url']['lang'];
}
if (!empty($language)) {
$saveCookie = true;
}
}
if (empty($language)) {
$language = $this->Cookie->read('language');
if (empty($language)) {
$saveCookie = true;
}
}
if (empty($language) && !array_key_exists($language, $this->languages)) {
$language = Configure::read('Config.language');
}
Configure::write('Config.language', $language);
if ($saveCookie) {
$this->Cookie->write('language', $language, false, '1 year');
}
}
}
?>
Where could the problem be?
我在CakePHP代码中有这一行: 我收到此错误: p>
这是我的 LanguageComponent.php strong>代码 p>
问题出在哪里? p>
div> $ language = $ this-&gt; Cookie-&gt ; read('language'); code> p>
致命错误:致电 第27行
code> pre>
&lt;?php
// App :: Import('Component','Cookie “);
class LanguageComponent扩展Object {
public $ controller = null;
public $ components = array('Cookie');
public $ languages = array();
public function initialize($ controller) {
$ this-&gt; controller = $ controller;
if(empty($ languages)){
$ this-&gt; languages = Configure :: read('Config.languages');
}
$ this-&gt; set();
}
公共函数集($ language = null){
$ saveCookie = false;
if(empty($ language)&amp;&amp; isset($ this - &gt;控制器)){
if(!empty($ this-&gt; controller-&gt; params ['named'] ['lang'])){
$ language = $ this-&gt; controller-&gt ; params ['named'] ['lang'];
} elseif(!empty($ this-&gt; controller-&gt; params ['url'] ['lang'])){
$ language = $ this-&gt; controller-&gt; params ['url'] ['lang'];
}
if(!empty($ language)){
$ saveCookie = true;
}
}
if(空($ lan guage)){
$ language = $ this-&gt; Cookie-&gt; read('language');
if(empty($ language)){
$ saveCookie = true;
}
} \ n if(empty($ language)&amp;&amp; !array_key_exists($ language,$ this-&gt; languages)){
$ language = Configure :: read('Config.language');
}
配置:: write('Config.language',$ language );
if($ saveCookie){
$ this-&gt; Cookie-&gt; write('language',$ language,false,'1 year');
}
}
} \ n
?&gt;
code> pre>
Wrong parent class
Compare the code in the question:
class LanguageComponent extends Object {
class AuthComponent extends Component {
Extending the wrong class means that none of the component constructor logic is invoked, and the way components are loaded on first access will not be available.
In the process of upgrading?
This is possibly because the language component was originally written for 1.x - the parent class for components changed when 2.x was released. As mentioned in the 2.0 migration guide:
Component is now the required base class for all components.
Try this
public $controller = null;
public $languages = array();
public $Cookie;
public function initialize($controller) {
$this->controller = $controller;
if (empty($languages)) {
$this->languages = Configure::read('Config.languages');
}
$this->set();
}
public function set($language = null) {
$this->Cookie = new CookieComponent(new ComponentCollection());
$saveCookie = false;
if (empty($language) && isset($this->controller)) {
if (!empty($this->controller->params['named']['lang'])) {
$language = $this->controller->params['named']['lang'];
} elseif (!empty($this->controller->params['url']['lang'])) {
$language = $this->controller->params['url']['lang'];
}
if (!empty($language)) {
$saveCookie = true;
}
}
if (empty($language)) {
$language = $this->Cookie->read('language');
if (empty($language)) {
$saveCookie = true;
}
}
if (empty($language) && !array_key_exists($language, $this->languages)) {
$language = Configure::read('Config.language');
}
Configure::write('Config.language', $language);
if ($saveCookie) {
$this->Cookie->write('language', $language, false, '1 year');
}
}
}
?>
This revised code works without any warning
controller = $controller; if (empty($languages)) { $this->languages = Configure::read('Config.languages'); } $this->set(); } public function set($language = null) { $this->Cookie = new CookieComponent(new ComponentCollection()); $saveCookie = false; if (empty($language) && isset($this->controller)) { if (!empty($this->controller->params['named']['lang'])) { $language = $this->controller->params['named']['lang']; } elseif (!empty($this->controller->params['url']['lang'])) { $language = $this->controller->params['url']['lang']; } if (!empty($language)) { $saveCookie = true; } } if (empty($language)) { $language = $this->Cookie->read('language'); if (empty($language)) { $saveCookie = true; } } if (empty($language) && !array_key_exists($language, $this->languages)) { $language = Configure::read('Config.language'); } Configure::write('Config.language', $language); if ($saveCookie) { $this->Cookie->write('language', $language, false, '1 year'); } } } ?>