Hooks在CodeIgniter 2.X中不起作用

时间:2011-12-10 10:00:54

标签: codeigniter-2

我在config.php中启用了挂钩

$config['enable_hooks'] = TRUE;

这是hook.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|   http://codeigniter.com/user_guide/general/hooks.html
|
*/

$hook['post_controller_constructor'] = array(
    'class' => 'Authorization',
    'function' => 'authorize',
    'filename' => 'authorization.php',
    'file_path' => 'hooks'
);

/* End of file hooks.php */
/* Location: ./application/config/hooks.php */

这是application / hooks /

下的authorization.php文件
<?php 


    class Authorization {

        private $ci;

        function __construct()
        {
            parent::__construct();
            $this->ci = get_instance();
        }

        function authorize()
        {
            echo 'This should be outputed';
        }

    }

?>

但它不起作用。没有人知道为什么?

2 个答案:

答案 0 :(得分:0)

我正在使用Codeigniter 2.1并且它可以工作..但我的钩子文件被称为'MainLoader'并且像这样开始:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MainLoader {

  public function mainLoader()
  {

      $CI =& get_instance();

      echo 'This should be outputed';

      //Whatever you want to do here

  }

我希望它有所帮助: - )

答案 1 :(得分:0)

我想你错过了数组变量旁边的[]。 在你的代码中写道:

$hook['post_controller_constructor'] = array(
    'class' => 'Authorization',
    'function' => 'authorize',
    'filename' => 'authorization.php',
    'file_path' => 'hooks'
);

我认为应该是这样的:

$hook['post_controller_constructor'][] = array(
    'class' => 'Authorization',
    'function' => 'authorize',
    'filename' => 'authorization.php',
    'file_path' => 'hooks'
);

请注意 $ hook [&#39; post_controller_constructor&#39;] 变量旁边的 []

希望这有帮助。谢谢..