Determine problem in CodeIgniter controller

2019-08-07 09:50发布

问题:

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.