我在 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/