扩展CI_Controller /间接修改重载属性 [英] Extending CI_Controller / Indirect modification of overloaded property

查看:276
本文介绍了扩展CI_Controller /间接修改重载属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CI社区似乎没有回答这个问题(或者它是如此明显,它看起来像一个noob问题?),所以我问这里。



我已经看到这种错误很长时间了(错误消息跟着),但我一直使用这个脏解决方法。现在我不想再编辑核心的CI代码,我知道必须有一个优雅的解决方案。



这里是设置。



在我的应用程序/ config / routes我有这个

  $ route ['default_controller' ] =dispatcher; 

application / controllers /中的Dispatcher类定义如下:

  class Dispatcher extends MY_Controller {

function __construct(){
parent :: __ construct
$ this-> load-> helper(array('form','https'));
}


function index(){

#load models
$ this-> load-> model /地图');
$ this-> load-> model('site / langs');
$ this-> load-> model('site / pages');
if($ mapped = $ this-> map-> get_map()){//这是第21行
$ this-> langs-> set_user_lang($ this-> GLO ['show_lang_in_url'],$ this-> GLO ['show_lang_at_home']);
$ red = base_url()。$ this-> GLO ['user_lang_label']。/。$ mapped;
redirect($ red,refresh);
}

...

/ core定义如下:

  class MY_Controller extends CI_Controller {

public function __construct {
parent :: __ construct();
$ this-> GLO = array();


#set defaults:

$ this-> GLO ['deb'] ='';
$ this-> GLO ['baseurl'] = base_url();
...(更多GLO [key] = value)
}

public function __set($ key,$ value){
$ this-> GLO [$ key] = $ value;我使用的每个模型都是这样开始的(相应命名):

每个模型都是这样开始的(相应命名):
}


$ b <

  class Map extends CI_Model {

function __construct(){
parent :: __ construct
}

function get_map(){
.....
return true;
}



现在使用CI 2.1.2,PHP 5.3.3,Apache 2当尝试加载页面时,收到此错误:


遇到PHP错误严重性:通知消息:间接
修改重载属性Langs :: $ GLO没有效果
文件名:site / langs.php行号:82



遇到PHP错误严重性:通知消息:未定义
属性:Dispatcher :: $ map文件名:controllers / dispatcher.php行
编号:21



致命错误:成员函数get_map()on
/home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21


第21行是地图模型中上面标记的那一行

  if($ mapped = $ this - > map-> get_map()){

this:

  $ this-> GLO ['langs'] [] = $ temp; 

在整个代码中,这个GLO数组被引用为$ this-GLO [ key ]。它必须不时地被读和写。如果我删除MY_Controller中的__set函数,我会得到更多的警告。



问题在哪里?



解决方案

我不确定第一个错误 - 但这两个:


遇到PHP错误严重性:注意消息:未定义
属性:Dispatcher :: $ map文件名:controllers / dispatcher.php Line
Number:21



致命错误:调用
/ home / webdev / MC / _WWW中非对象上的成员函数get_map应用程序/ controllers / dispatcher.php第21行


...看起来你没有正确加载你的模型?



更改

  if($ mapped = $ this-> map-> get_map ()){

  $ this-> load-> model('map'); 
if($ mapped = $ this-> map-> get_map()){


the CI community seems to have no answer to this (or maybe it is so obvious, that it looks like a noob question?), so I ask here.

I've been seeing this kind of error for a long time now (error messages follow), however I have always used this "dirty" workaround. Now I do not want to edit the core CI code anymore, and I know there must be a graceful solution to this.

Here is the setup.

In my application/config/routes I have this

$route['default_controller'] = "dispatcher";

The Dispatcher class in application/controllers/ is defined as this:

class Dispatcher extends MY_Controller {

 function __construct() {
  parent::__construct();
  $this->load->helper(array('form','https'));
 }


 function index() {

  # load models
  $this->load->model('site/map');
  $this->load->model('site/langs');
  $this->load->model('site/pages');
  if ( $mapped = $this->map->get_map() ){ // this is the line 21
   $this->langs->set_user_lang( $this->GLO['show_lang_in_url'], $this->GLO['show_lang_at_home'] );
   $red = base_url().$this->GLO['user_lang_label']."/".$mapped;
   redirect($red, "refresh");
   } 

  ...

now MY_Controller resides in application/core and is defined as follows:

class MY_Controller extends CI_Controller {

 public function __construct() {
        parent::__construct();
  $this->GLO = array();   

  #
  # set defaults:
  #
  $this->GLO['deb'] = '';
  $this->GLO['baseurl'] = base_url();
  ... (more GLO[key] = value ) 
}

 public function __set ($key, $value ) {
  $this->GLO[$key] = $value;
 }

Every model I use starts like this (named accordingly):

class Map extends CI_Model {

 function __construct(){
  parent::__construct(); 
 }

 function get_map(){
        .....
        return true;
        }

Now with CI 2.1.2, PHP 5.3.3, Apache 2 on Debian when trying to load a page, I am getting this error:

A PHP Error was encountered Severity: Notice Message: Indirect modification of overloaded property Langs::$GLO has no effect Filename: site/langs.php Line Number: 82

A PHP Error was encountered Severity: Notice Message: Undefined property: Dispatcher::$map Filename: controllers/dispatcher.php Line Number: 21

Fatal error: Call to a member function get_map() on a non-object in /home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21

The line 21 is the one marked above in the Map model

if ( $mapped = $this->map->get_map() ){

the line 82 in the other model looks like this:

$this->GLO['langs'][] = $temp;

Throughout the code, this GLO array is referenced as $this-GLO[key]. It must be read and written from time to time. If I delete the __set function in MY_Controller, I get more of those warnings.

Where is the problem?

Thanks a lot in advance!

解决方案

I'm not sure about the first error - but these two:

A PHP Error was encountered Severity: Notice Message: Undefined property: Dispatcher::$map Filename: controllers/dispatcher.php Line Number: 21

Fatal error: Call to a member function get_map() on a non-object in /home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21

...look like you havent loaded your model correctly?

change

if ( $mapped = $this->map->get_map() ){

to

$this->load->model('map');
if ( $mapped = $this->map->get_map() ){

这篇关于扩展CI_Controller /间接修改重载属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆