做項目之前,一定要先部署要自己的"基類",非常的重要。打個比方,要訪問會員中心相關的控制器,這類控制器是不是都要有一個“登錄限制”,才讓訪問會員相關的控制器?
一、創建三大基類
原始基類
位置:thinkphp\apps\common\controller\Base.php
作用:Base模塊下的內容,Index模塊,和Admin模塊都可以調用
代碼:
<?php /** * 原始基類 * */ namespace app\Common\controller; use think\Controller; class Base extends Controller{ public function _initialize() { parent::_initialize(); echo '原始基類'; } public function test1(){ return 'test1'; } }
Index模塊基類
位置:thinkphp\apps\common\controller\Base.php
作用:Index模塊下的控制器,都要“繼承基類”並且“調用基類”
代碼:
<?php /** * 前端基類 * */ namespace app\index\controller; use app\Common\controller\Base; class IndexBase extends Base { public function _initialize() { parent::_initialize(); } public function index() { } }
Admin模塊基類
位置:thinkphp\apps\common\controller\Base.php
作用:Admin模塊下的控制器,都要“繼承基類”並且“調用基類”
代碼:
/** * 后台首頁 * */ namespace app\Admin\controller; use app\Admin\controller\AdminBase; class Index extends AdminBase { public function _initialize() { parent::_initialize(); } public function index() { return $this->fetch(); } }
(User模塊基類,如果有會員的話,這個也必須要創建的)
創建基類的主要目的,就是“繼承”與“調用”