Hooks 在 CodeIgniter 2.X 中不起作用

发布于 2024-12-20 15:57:46 字数 1235 浏览 2 评论 0原文

我已经在 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';
        }

    }

?>

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

I've enable hooks in config.php

$config['enable_hooks'] = TRUE;

here is the 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 */

and here is the authorization.php file under application/hooks/

<?php 


    class Authorization {

        private $ci;

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

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

    }

?>

but it doesn't work. doesn't anybody know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-12-27 15:57:46

我正在使用 Codigniter 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

  }

我希望它有帮助:-)

I am using Codigniter 2.1 and it works.. but my hook file is called 'MainLoader' and starts like this:

<?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

  }

I hope it helps :-)

顾冷 2024-12-27 15:57:46

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

$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['post_controller_constructor'] 变量旁边的 []

希望这有帮助。谢谢..

I think you missed the [] next to the array variable.
In your code it written :

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

I think it should be like this :

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

note the [] next to the $hook['post_controller_constructor'] variable.

Hope this help. Thank you..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文