php - CodeIgniter post_controller_constructor Hook 运行两次?

标签 php codeigniter hook

我在 application\config\hooks.php 中有这段代码

$hook['post_controller_constructor'] = array(
'class' => 'maintenance',
'function' => 'maintenance',
'filename' => 'maintenance.php',
'filepath' => 'hooks',
'params' => array()
);

这段代码在 application\hooks\maintenance.php 中

class maintenance
{
   var $CI;    
   public function maintenance()
   {
    echo "Test";
   }
}

这段代码在 application\config\config_maintenance.php 中

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

$config['maintenance'] = true;

这是我的 Controller 的样子:

<?php
 class Home extends CI_Controller {
function __construct()
{
    parent::__construct();
    $this->load->model(array('home_model'));

}

function index()
{
    $this->load->view('home');
}
}

当代码运行时,如果我不添加“exit”,“Test”会在页面上回显两次;在 echo 语句之后。这是否意味着“post_controller_constructor”被调用了两次?

我想知道这是为什么,因为根据 CI 文档

post_controller_constructor: Called immediately after your controller is instantiated, but prior to any method calls happening.

最佳答案

好的,问题出在您的 Maintenance 类和您的 Hook 定义上。您调用 Hook maintenance 和函数 maintenance。如果你以类的方式命名一个方法,那么这个方法就是类的构造函数。继续并重命名您的方法:

hooks.php

$hook['post_controller_constructor'] = array(
    'class' => 'maintenance',
    'function' => 'differentName',
    'filename' => 'maintenance.php',
    'filepath' => 'hooks',
    'params' => array()
);

维护.php

class maintenance
{
   var $CI;    
   public function differentName()
   {
      echo "Test";
   }
}

关于php - CodeIgniter post_controller_constructor Hook 运行两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19532893/

相关文章:

php - PHP变量规则

c++ - 用 C++ 创建 TNotifyEvent 以在应用程序级别使用

用于音频处理和路由到默认音频设备的音频 Hook 或自定义音频驱动程序

php - Yii - 从 URL 中删除 'index'

php - 从 SQL 数据库获取信息并使用 PHP 显示的问题

PHP+MySQL : Creating a photo gallery based on views

php - Codeigniter 复杂 MySQL 查询

php - CodeIgniter 和计算数据库中的特定行

php - 获取排除少数用户的特定用户列表

apache - SVN Hook 突然停止工作