为了想实现个简单的访问前台时候记录下请求的信息。
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\admin\model\Site;
use app\admin\model\Cate;
class Base extends Controller
{
public function _initialize()
{
$cate= new Cate();
$system=Site::get(1);
$procate=$cate->where('cate_type=1 AND pid=0')->select();
$this->assign('procate',$procate);
$this->assign('system',$system);
$myfile = fopen("ip.txt", "a") or die("Unable to open file!");
$txt = "111".PHP_EOL;
fwrite($myfile, $txt);
fclose($myfile);
}
}
一个简单的写入文本里面。发现文本生成了3次
然后取消了写到继承了这个控制器的其他控制器里面
<?php
namespace app\index\controller;
use app\admin\controller\Images;
use app\admin\model\Content;
use think\Controller;
use think\Request;
use app\admin\model\PageModel;
use app\admin\model\Site;
use app\admin\model\Cate;
class Page extends Base
{
public function about(){
$page=new PageModel();
$data=$page->getonePage(1);
$this->assign('data',$data);
$myfile = fopen("ip.txt", "a") or die("Unable to open file!");
$txt = "111".PHP_EOL;
fwrite($myfile, $txt);
fclose($myfile);
return $this->fetch();
}
发现就写入了一次是正常的
然而为什么我用这个构造函数时候 比如index 继承ba
把这个写入放的单独页面的方法里面去他实际上是只是写入了1次。
这算是这个函数BUG吗。
基本上我是用_initialize 当作算一个中间件来看的。发现他运行了不止一次。
最佳答案
