Determine problem in CodeIgniter controller

你离开我真会死。 提交于 2020-01-05 04:37:14

问题


I've created a base controller that all of my controllers extend...

class PublicController extends CI_Controller {
    private $js = array(),
            $css = array(),
            $templateVars = array();

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

        //register account
        $this->templateVars['account'] = ($this->account = $this->modelFactory('account'));

        // enable profiler for development
        if (ENVIRONMENT == 'development') {
            $this->output->enable_profiler(true);
            $this->addJs('jquery-min-1.5.2');
        } else {
            $this->addJs('http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js');
        }

        $this->addCss('base');
        $this->addJs('base');
    }


/**
 * Loads and initiates models
 */
protected function modelFactory ($model, $input = array()) {
    $this->load->model($model);

    $class = $model.'Model';
    return new $class($input);
}

The problem here is that I get the error Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\Program Files (x86)\EasyPHP-5.3.6.0\www\site.com\system\core\Common.php on line 328

When I comment out the line $this->templateVars['account'] = the error goes away.... how come this is looping?


回答1:


As a workaround you could add this as the first statement in your __construct method:

 global $onlyonce; if ($onlyonce++) return;

This will prevent multiple instances of your controller to be created. Without knowing the rest of your code, or CodeIgniter, it's likely to assume that your model class itself instantiates your controller. Which in turn creates another model.

A xdebug profiler trace will tell you more. This is not enough code to tell you the exact reason.




回答2:


@webnet it happened the same to me in an CI with modules (HMVC). My controller had the same file name of the model. Changing the name did the trick.



来源:https://stackoverflow.com/questions/5877869/determine-problem-in-codeigniter-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!