創(chuàng)建公用控制器
我們創(chuàng)建好了后臺(tái)驗(yàn)證,但是直接輸入路由地址還是能直接訪問,這需要怎么解決呢?
我們使用__construct構(gòu)造方法,他可以在類被實(shí)例化時(shí)調(diào)用。
CommonController.class.php
<?php namespace Admin\Controller; use Think\Controller; class CommonController extends Controller { public function __construct(){ parent::__construct(); if (!Session('uid')){ $this->error('請(qǐng)先登錄在訪問',U('Login/index')); } } }
我們需要在每個(gè)頁(yè)面繼承這個(gè)CommonController.class.php控制器
這時(shí)候我們直接訪問路由會(huì)提示
這樣我們的公共控制器就完成了。