致命错误:在LanguageComponent上的非对象上调用成员函数read()

时间:2014-06-23 07:35:32

标签: php cakephp cookies

我在CakePHP代码中有这一行:$language = $this->Cookie->read('language');

我收到此错误:

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

这是我的 LanguageComponent.php 代码

<?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');
        }
    }

}

?>

问题出在哪里?

3 个答案:

答案 0 :(得分:3)

错误的父类

比较问题中的代码:

class LanguageComponent extends Object {

any core component

class AuthComponent extends Component {

扩展错误的类意味着没有调用component constructor logicthe way components are loaded on first access将无法使用。

在升级过程中?

这可能是因为语言组件最初是为1.x编写的 - 在2.x发布时组件的父类已更改。如the 2.0 migration guide中所述:

  

Component现在是所有组件所需的基类。

答案 1 :(得分:0)

试试这个     

    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');
        }
    }

}

?>

答案 2 :(得分:-1)

此修订后的代码无需任何警告

controller = $ controller;         if(empty($ languages)){             $ this-&gt; languages = Configure :: read(&#39; Config.languages&#39;);         }         $这 - &gt;设置();     }     公共函数集($ language = null){         $ this-&gt; Cookie = new CookieComponent(new ComponentCollection());         $ saveCookie = false;         if(empty($ language)&amp;&amp; isset($ this-&gt; controller)){             if(!empty($ this-&gt; controller-&gt; params [&#39; named&#39;] [&#39; lang&#39;])){                 $ language = $ this-&gt; controller-&gt; params [&#39; named&#39;] [&#39; lang&#39;];             } elseif(!empty($ this-&gt; controller-&gt; params [&#39; url&#39;] [&#39; lang&#39;])){                 $ language = $ this-&gt; controller-&gt; params [&#39; url&#39;] [&#39; lang&#39;];             }             if(!empty($ language)){                 $ saveCookie = true;             }         }         if(empty($ language)){             $ language = $ this-&gt; Cookie-&gt; read(&#39; language&#39;);             if(empty($ language)){                 $ saveCookie = true;             }         }         if(空($ language)&amp;&amp;!array_key_exists($ language,$ this-&gt; languages)){             $ language = Configure :: read(&#39; Config.language&#39;);         }         配置:: write(&#39; Config.language&#39;,$ language);         if($ saveCookie){             $ this-&gt; Cookie-&gt;写(&#39;语言&#39;,$ language,false,&#39; 1年&#39;);         }     } } ?&GT;