tp5使用initialize

文章描述:

thinkphp5使用构造函数

Base.php

<?php
namespace app\admin\controller;
use think\Controller;
use think\db;
use think\facade\Session;

class Base extends Controller
{
    protected function initialize()
    {

        $username = Session::get('userid');
        if(!$username){
            $this->redirect('/admin.php/login',302);
        }

    }
}

Index.php

<?php
namespace app\admin\controller;
use think\Controller;
use think\db;
use think\facade\Session;

class Index extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        return $this->fetch('index');
    }
}

 

发布时间:2022/04/14

发表评论