pre_controller钩子不会加载docs状态之类的基类? [英] pre_controller hook does not load base classes like docs state?

查看:56
本文介绍了pre_controller钩子不会加载docs状态之类的基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此处的Codeignitor文档: http://ellislab.com/codeigniter /user-guide/general/hooks.html 它指出:



pre_controller
在调用任何控制器之前立即调用。所有基类,路由和安全性检查都已完成。



但是,如果我使用以下方法创建钩子pre_controller钩子:

  $ hook ['pre_controller'] [] = array(
'class'=>'tester',
'function'=>'测试'',
'文件名'=>'tester.php',
'文件路径'=>'模型',
//'参数'=>数组('啤酒' ,'wine','snacks')
);

,文件tester.php为:

 类测试器扩展了CI_Model 
{
公共功能__construct()
{
parent :: __ construct();

$ this->负载->库(迁移);
}

public function test()
{
echo hi;
出口;
}
}

我收到此错误:

 致命错误:在******。php 


为什么不加载 CI_Model ?如果我输入了require_once('system / core / Model.php');在pre_controller定义上方的hooks.php文件中,出现以下错误:

 致命错误:调用成员函数库( )在****。php中的非对象上

因为它实际上没有加载 CI_Model library()之类的功能将无法使用。我如何强制其引导 CI_Model



第一个说 使用post_controller_constructor 会立即显示出来,因为它不能回答问题。在从控制器类运行任何构造函数之前,我需要它加载。我需要从 pre_controller 钩子扩展 CI_Model 类。

解决方案

<简短的答案是CodeIgniter无法按照您希望的方式工作。在您尝试访问模型的阶段,CodeIgniter尚未加载必需的类,并且该类不可用。



查看<$ c $,具体取决于您要实现的目标,可能还有另一种方式-不使用钩子/不使用后面的钩子? c> /system/core/CodeIgniter.php 将显示何时调用每个挂钩以及何时执行其他任务;



如果坚持使用此钩子,则可以添加以下内容: load_class('Model',' core'); 放在模型文件的顶部(在声明类之前),但这将是一个非常肮脏的修正。



确保您的类名称遵循正确的命名约定- tester 应该为 Tester






编辑:由于要在每个调用上运行相同的代码(假设每个控制器调用),因此可能的解决方法是:



扩展核心控制器,并将此新控制器用作所有其他控制器的基本控制器(因为它们将共享相同的功能)。在此新基本控制器的构造函数中,添加要在每个调用上运行的功能。在您的任何控制器中的任何其他代码之前,将调用此构造函数中的代码。



创建以下文件, application / core / MY_Controller。 php

 类MY_Controller扩展CI_Controller {
函数__construct()
{
parent :: __ construct();
//做任何您想做的事-加载模型,调用函数...
}
}

使用 MY_Controller 扩展应用程序/控制器中的每个控制器,而不是 CI_Controller

  class欢迎扩展MY_Controller {
函数__construct( )
{
parent :: __ construct();
}
//您的控制器的其他功能...
}


According to the Codeignitor docs here: http://ellislab.com/codeigniter/user-guide/general/hooks.html it states:

pre_controller Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.

However, if I create a hook pre_controller hook with:

$hook['pre_controller'][] = array(
'class'    => 'tester',
'function' => 'test',
'filename' => 'tester.php',
'filepath' => 'models',
//'params'   => array('beer', 'wine', 'snacks')
);

and the file tester.php is:

class tester extends CI_Model
{
  public function __construct()
  {
    parent::__construct();

    $this->load->library('migration');
  }

  public function test()
  {
    echo "hi";
    exit;
  }
}

I get this error:

Fatal error: Class 'CI_Model' not found in ******.php

Why is it not loading CI_Model? If I put a require_once('system/core/Model.php'); in the hooks.php file above the pre_controller definition, I get this error:

Fatal error: Call to a member function library() on a non-object in ****.php

Since it's not actually loading the CI_Model, functions such as library() would not work. How can I force it to bootstrap the CI_Model.

The first person that says "Use post_controller_constructor" will be shot on sight as that does not answer the question. I need it to load BEFORE it runs any constructor functions from the controller classes. I need access to extend the CI_Model class from the pre_controller hook.

解决方案

The short answer is that CodeIgniter doesn't work the way you want it to. At the stage that you're trying to access a model, CodeIgniter hasn't loaded the required classes and it isn't available. Depending on exactly what you're trying to achieve, there may be another way to achieve this - without using a hook/using a later hook?

Viewing /system/core/CodeIgniter.php will show when each hook is called and when other tasks are performed; loading routing, loading global functions etc.

If you insist on using this hook, then you could add this: load_class('Model', 'core'); at the top of your model file (before you declare the class), but this would be a very dirty fix.

Make sure your class names follow the correct naming convention - tester should be Tester.


Edit: as you want to run the same code on every call (assuming every controller call), this is a possible solution:

Extend the core controller, and use this new controller as a base controller for all other controllers (as they will be sharing the same functionality). In the constructor of this new base controller, add the functionality that you want to run on every call. The code in this constructor will be called before any other code in any of your controllers.

Create the following file, application/core/MY_Controller.php.

class MY_Controller extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        // Do whatever you want - load a model, call a function...
    }
}

Extend every controller in application/controllers, with MY_Controller, rather than CI_Controller.

class Welcome extends MY_Controller {
    function __construct()
    {
        parent::__construct();
    }
    // Your controllers other functions...
}

这篇关于pre_controller钩子不会加载docs状态之类的基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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